diff --git a/build.zig b/build.zig index ac0c004..da52996 100644 --- a/build.zig +++ b/build.zig @@ -225,12 +225,7 @@ pub fn build_exe( }); const thespian_mod = thespian_dep.module("thespian"); - - const cbor_dep = thespian_dep.builder.dependency("cbor", .{ - .target = target, - .optimize = optimize_deps, - }); - const cbor_mod = cbor_dep.module("cbor"); + const cbor_mod = thespian_dep.module("cbor"); const tracy_dep = if (tracy_enabled) thespian_dep.builder.dependency("tracy", .{ .target = target, diff --git a/build.zig.zon b/build.zig.zon index 284c2cc..7d281a5 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -15,12 +15,12 @@ .hash = "dizzy-1.0.0-AAAAAM1wAAAiDbx_6RwcVEOBk8p2XOu8t9WPNc3K7kBK", }, .thespian = .{ - .url = "https://github.com/neurocyte/thespian/archive/42a98a44e7590fd6e952b75523e5c0f4fa0e6c97.tar.gz", - .hash = "thespian-0.0.1-owFOjnUTBgBUlBtQ-SbN_6S7bXE6e2mKmCgk4A-RGBMA", + .url = "https://github.com/neurocyte/thespian/archive/e3921691dc5d949ee38719251d67841f88dbabfd.tar.gz", + .hash = "thespian-0.0.1-owFOjsnnBgDTBCdPHQhp3Ir3hLZI5pVLOrlIa8JtGJAb", }, .themes = .{ - .url = "https://github.com/neurocyte/flow-themes/releases/download/master-ac2e3fe2df3419b71276f86fa9c45fd39d668f23/flow-themes.tar.gz", - .hash = "N-V-__8AAEtaFwAjAHCmWHRCrBxL7uSG4hQiIsSgS32Y67K6", + .url = "https://github.com/neurocyte/flow-themes/releases/download/master-59bf204551bcb238faddd06779063570e7e6d431/flow-themes.tar.gz", + .hash = "N-V-__8AAM9UFwCaITo5LqgOpcfd4SXnFhuwJ4rEuZ253yt6", }, .fuzzig = .{ .url = "https://github.com/fjebaker/fuzzig/archive/44c04733c7c0fee3db83672aaaaf4ed03e943156.tar.gz", diff --git a/src/LSP.zig b/src/LSP.zig index 1f8666a..171bbb1 100644 --- a/src/LSP.zig +++ b/src/LSP.zig @@ -227,7 +227,6 @@ const Process = struct { UnsupportedType, ExitNormal, ExitUnexpected, - InvalidMapType, }); fn receive_safe(self: *Process, from: tp.pid_ref, m: tp.message) Error!void { diff --git a/src/Project.zig b/src/Project.zig index 54b59a3..2309c82 100644 --- a/src/Project.zig +++ b/src/Project.zig @@ -26,7 +26,7 @@ const Self = @This(); const OutOfMemoryError = error{OutOfMemory}; const SpawnError = (OutOfMemoryError || error{ThespianSpawnFailed}); -pub const InvalidMessageError = error{ InvalidMessage, InvalidMessageField, InvalidTargetURI, InvalidMapType }; +pub const InvalidMessageError = error{ InvalidMessage, InvalidMessageField, InvalidTargetURI }; pub const StartLspError = (error{ ThespianSpawnFailed, Timeout, InvalidLspCommand } || LspError || OutOfMemoryError || cbor.Error); pub const LspError = (error{ NoLsp, LspFailed } || OutOfMemoryError); pub const ClientError = (error{ClientFailed} || OutOfMemoryError); @@ -131,7 +131,7 @@ pub fn restore_state(self: *Self, data: []const u8) !void { var iter: []const u8 = data; _ = cbor.matchValue(&iter, tp.string) catch {}; _ = cbor.decodeArrayHeader(&iter) catch |e| switch (e) { - error.InvalidArrayType => return self.restore_state_v0(data), + error.InvalidType => return self.restore_state_v0(data), else => return tp.trace(tp.channel.debug, .{ "restore_state", "unknown format", data }), }; self.persistent = true; @@ -191,18 +191,7 @@ pub fn restore_state_v1(self: *Self, data: []const u8) !void { } } -pub fn restore_state_v0(self: *Self, data: []const u8) error{ - OutOfMemory, - IntegerTooLarge, - IntegerTooSmall, - InvalidType, - TooShort, - InvalidFloatType, - InvalidArrayType, - InvalidPIntType, - JsonIncompatibleType, - NotAnObject, -}!void { +pub fn restore_state_v0(self: *Self, data: []const u8) error{ OutOfMemory, IntegerTooLarge, IntegerTooSmall, InvalidType, TooShort }!void { tp.trace(tp.channel.debug, .{"restore_state_v0"}); defer self.sort_files_by_mtime(); var name: []const u8 = undefined; diff --git a/src/main.zig b/src/main.zig index 00091d8..ffcb840 100644 --- a/src/main.zig +++ b/src/main.zig @@ -671,34 +671,6 @@ pub fn list_keybind_namespaces(allocator: std.mem.Allocator) ![]const []const u8 return result.toOwnedSlice(); } -pub fn read_theme(allocator: std.mem.Allocator, theme_name: []const u8) ?[]const u8 { - const file_name = get_theme_file_name(theme_name) catch return null; - var file = std.fs.openFileAbsolute(file_name, .{ .mode = .read_only }) catch return null; - defer file.close(); - return file.readToEndAlloc(allocator, 64 * 1024) catch null; -} - -pub fn write_theme(theme_name: []const u8, content: []const u8) !void { - const file_name = try get_theme_file_name(theme_name); - var file = try std.fs.createFileAbsolute(file_name, .{ .truncate = true }); - defer file.close(); - return file.writeAll(content); -} - -pub fn list_themes(allocator: std.mem.Allocator) ![]const []const u8 { - var dir = try std.fs.openDirAbsolute(try get_theme_directory(), .{ .iterate = true }); - defer dir.close(); - var result = std.ArrayList([]const u8).init(allocator); - var iter = dir.iterateAssumeFirstIteration(); - while (try iter.next()) |entry| { - switch (entry.kind) { - .file, .sym_link => try result.append(try allocator.dupe(u8, std.fs.path.stem(entry.name))), - else => continue, - } - } - return result.toOwnedSlice(); -} - pub fn get_config_dir() ![]const u8 { return get_app_config_dir(application_name); } @@ -751,9 +723,6 @@ fn get_app_config_dir(appname: []const u8) ConfigDirError![]const u8 { var keybind_dir_buffer: [std.posix.PATH_MAX]u8 = undefined; std.fs.makeDirAbsolute(try std.fmt.bufPrint(&keybind_dir_buffer, "{s}/{s}", .{ config_dir, keybind_dir })) catch {}; - var theme_dir_buffer: [std.posix.PATH_MAX]u8 = undefined; - std.fs.makeDirAbsolute(try std.fmt.bufPrint(&theme_dir_buffer, "{s}/{s}", .{ config_dir, theme_dir })) catch {}; - return config_dir; } @@ -899,28 +868,6 @@ pub fn get_keybind_namespace_file_name(namespace_name: []const u8) ![]const u8 { return try std.fmt.bufPrint(&local.file_buffer, "{s}/{s}.json", .{ dir, namespace_name }); } -const theme_dir = "themes"; - -fn get_theme_directory() ![]const u8 { - const local = struct { - var dir_buffer: [std.posix.PATH_MAX]u8 = undefined; - }; - const a = std.heap.c_allocator; - if (std.process.getEnvVarOwned(a, "FLOW_THEMES_DIR") catch null) |dir| { - defer a.free(dir); - return try std.fmt.bufPrint(&local.dir_buffer, "{s}", .{dir}); - } - return try std.fmt.bufPrint(&local.dir_buffer, "{s}/{s}", .{ try get_app_config_dir(application_name), theme_dir }); -} - -pub fn get_theme_file_name(theme_name: []const u8) ![]const u8 { - const dir = try get_theme_directory(); - const local = struct { - var file_buffer: [std.posix.PATH_MAX]u8 = undefined; - }; - return try std.fmt.bufPrint(&local.file_buffer, "{s}/{s}.json", .{ dir, theme_name }); -} - fn restart() noreturn { var executable: [:0]const u8 = std.mem.span(std.os.argv[0]); var is_basename = true; diff --git a/src/renderer/vaxis/renderer.zig b/src/renderer/vaxis/renderer.zig index 5cfb232..32af142 100644 --- a/src/renderer/vaxis/renderer.zig +++ b/src/renderer/vaxis/renderer.zig @@ -52,11 +52,6 @@ pub const Error = error{ CodepointTooLarge, TtyInitError, TtyWriteError, - InvalidFloatType, - InvalidArrayType, - InvalidPIntType, - JsonIncompatibleType, - NotAnObject, } || std.Thread.SpawnError; pub fn init(allocator: std.mem.Allocator, handler_ctx: *anyopaque, no_alternate: bool, _: *const fn (ctx: *anyopaque) void) Error!Self { diff --git a/src/shell.zig b/src/shell.zig index c4ee5ca..05aa4d9 100644 --- a/src/shell.zig +++ b/src/shell.zig @@ -21,11 +21,6 @@ pub const Error = error{ IntegerTooSmall, InvalidType, TooShort, - InvalidFloatType, - InvalidArrayType, - InvalidPIntType, - JsonIncompatibleType, - NotAnObject, }; pub const OutputHandler = fn (context: usize, parent: tp.pid_ref, arg0: []const u8, output: []const u8) void; diff --git a/src/tui/tui.zig b/src/tui/tui.zig index 9339dd1..49d6e11 100644 --- a/src/tui/tui.zig +++ b/src/tui/tui.zig @@ -44,7 +44,6 @@ commands: Commands = undefined, logger: log.Logger, drag_source: ?*Widget = null, theme_: Widget.Theme, -parsed_theme: ?std.json.Parsed(Widget.Theme), idle_frame_count: usize = 0, unrendered_input_events_count: usize = 0, init_timer: ?tp.timeout, @@ -102,7 +101,7 @@ fn init(allocator: Allocator) InitError!*Self { var conf, const conf_bufs = root.read_config(@import("config"), allocator); defer root.free_config(allocator, conf_bufs); - const theme_, const parsed_theme = get_theme_by_name(allocator, conf.theme) orelse get_theme_by_name(allocator, "dark_modern") orelse return error.UnknownTheme; + const theme_ = get_theme_by_name(conf.theme) orelse get_theme_by_name("dark_modern") orelse return error.UnknownTheme; conf.theme = theme_.name; conf.whitespace_mode = try allocator.dupe(u8, conf.whitespace_mode); conf.input_mode = try allocator.dupe(u8, conf.input_mode); @@ -136,7 +135,6 @@ fn init(allocator: Allocator) InitError!*Self { .theme_ = theme_, .no_sleep = tp.env.get().is("no-sleep"), .query_cache_ = try syntax.QueryCache.create(allocator, .{}), - .parsed_theme = parsed_theme, }; instance_ = self; defer instance_ = null; @@ -689,19 +687,6 @@ fn refresh_input_mode(self: *Self) command.Result { self.input_mode_ = new_mode; } -fn set_theme_by_name(self: *Self, name: []const u8) !void { - const old = self.parsed_theme; - defer if (old) |p| p.deinit(); - self.theme_, self.parsed_theme = get_theme_by_name(self.allocator, name) orelse { - self.logger.print("theme not found: {s}", .{name}); - return; - }; - self.config_.theme = self.theme_.name; - self.set_terminal_style(); - self.logger.print("theme: {s}", .{self.theme_.description}); - try save_config(); -} - const cmds = struct { pub const Target = Self; const Ctx = command.Context; @@ -724,19 +709,32 @@ const cmds = struct { var name: []const u8 = undefined; if (!try ctx.args.match(.{tp.extract(&name)})) return tp.exit_error(error.InvalidSetThemeArgument, null); - return self.set_theme_by_name(name); + self.theme_ = get_theme_by_name(name) orelse { + self.logger.print("theme not found: {s}", .{name}); + return; + }; + self.config_.theme = self.theme_.name; + self.set_terminal_style(); + self.logger.print("theme: {s}", .{self.theme_.description}); + try save_config(); } pub const set_theme_meta: Meta = .{ .arguments = &.{.string} }; pub fn theme_next(self: *Self, _: Ctx) Result { - const name = get_next_theme_by_name(self.theme_.name); - return self.set_theme_by_name(name); + self.theme_ = get_next_theme_by_name(self.theme_.name); + self.config_.theme = self.theme_.name; + self.set_terminal_style(); + self.logger.print("theme: {s}", .{self.theme_.description}); + try save_config(); } pub const theme_next_meta: Meta = .{ .description = "Next color theme" }; pub fn theme_prev(self: *Self, _: Ctx) Result { - const name = get_prev_theme_by_name(self.theme_.name); - return self.set_theme_by_name(name); + self.theme_ = get_prev_theme_by_name(self.theme_.name); + self.config_.theme = self.theme_.name; + self.set_terminal_style(); + self.logger.print("theme: {s}", .{self.theme_.description}); + try save_config(); } pub const theme_prev_meta: Meta = .{ .description = "Previous color theme" }; @@ -969,13 +967,6 @@ const cmds = struct { } pub const open_keybind_config_meta: Meta = .{ .description = "Edit key bindings" }; - pub fn open_custom_theme(self: *Self, _: Ctx) Result { - const file_name = try self.get_or_create_theme_file(self.allocator); - try tp.self_pid().send(.{ "cmd", "navigate", .{ .file = file_name } }); - self.logger.print("restart flow to use changed theme", .{}); - } - pub const open_custom_theme_meta: Meta = .{ .description = "Customize theme" }; - pub fn run_async(self: *Self, ctx: Ctx) Result { var iter = ctx.args.buf; var len = try cbor.decodeArrayHeader(&iter); @@ -1182,38 +1173,33 @@ pub fn theme() *const Widget.Theme { return ¤t().theme_; } -pub fn get_theme_by_name(allocator: std.mem.Allocator, name: []const u8) ?struct { Widget.Theme, ?std.json.Parsed(Widget.Theme) } { - if (load_theme_file(allocator, name) catch null) |parsed_theme| { - std.log.info("loaded theme from file: {s}", .{name}); - return .{ parsed_theme.value, parsed_theme }; - } - +pub fn get_theme_by_name(name: []const u8) ?Widget.Theme { for (Widget.themes) |theme_| { if (std.mem.eql(u8, theme_.name, name)) - return .{ theme_, null }; + return theme_; } return null; } -fn get_next_theme_by_name(name: []const u8) []const u8 { +pub fn get_next_theme_by_name(name: []const u8) Widget.Theme { var next = false; for (Widget.themes) |theme_| { if (next) - return theme_.name; + return theme_; if (std.mem.eql(u8, theme_.name, name)) next = true; } - return Widget.themes[0].name; + return Widget.themes[0]; } -fn get_prev_theme_by_name(name: []const u8) []const u8 { +pub fn get_prev_theme_by_name(name: []const u8) Widget.Theme { var prev: ?Widget.Theme = null; for (Widget.themes) |theme_| { if (std.mem.eql(u8, theme_.name, name)) - return (prev orelse Widget.themes[Widget.themes.len - 1]).name; + return prev orelse Widget.themes[Widget.themes.len - 1]; prev = theme_; } - return Widget.themes[Widget.themes.len - 1].name; + return Widget.themes[Widget.themes.len - 1]; } pub fn find_scope_style(theme_: *const Widget.Theme, scope: []const u8) ?Widget.Theme.Token { @@ -1356,32 +1342,3 @@ pub fn render_match_cell(self: *renderer.Plane, y: usize, x: usize, theme_: *con cell.set_style(theme_.editor_match); _ = self.putc(&cell) catch {}; } - -fn get_or_create_theme_file(self: *Self, allocator: std.mem.Allocator) ![]const u8 { - const theme_name = self.theme_.name; - if (root.read_theme(allocator, theme_name)) |content| { - allocator.free(content); - } else { - var buf = std.ArrayList(u8).init(self.allocator); - defer buf.deinit(); - try std.json.stringify(self.theme_, .{ .whitespace = .indent_2 }, buf.writer()); - try root.write_theme( - theme_name, - buf.items, - ); - } - return try root.get_theme_file_name(theme_name); -} - -fn load_theme_file(allocator: std.mem.Allocator, theme_name: []const u8) !?std.json.Parsed(Widget.Theme) { - return load_theme_file_internal(allocator, theme_name) catch |e| { - std.log.err("loaded theme from file failed: {}", .{e}); - return e; - }; -} -fn load_theme_file_internal(allocator: std.mem.Allocator, theme_name: []const u8) !?std.json.Parsed(Widget.Theme) { - _ = std.json.Scanner; - const json_str = root.read_theme(allocator, theme_name) orelse return null; - defer allocator.free(json_str); - return try std.json.parseFromSlice(Widget.Theme, allocator, json_str, .{ .allocate = .alloc_always }); -}