From b3bd6d19b5b07a658357e694fda51054b78eddef Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Tue, 10 Dec 2024 20:08:43 +0100 Subject: [PATCH] refactor: move hints out of generic palette --- src/tui/mode/overlay/command_palette.zig | 15 ++++++++++++--- .../mode/overlay/list_all_commands_palette.zig | 13 +++++++++++-- src/tui/mode/overlay/open_recent_project.zig | 3 ++- src/tui/mode/overlay/palette.zig | 9 +-------- src/tui/mode/overlay/theme_palette.zig | 6 +++++- 5 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/tui/mode/overlay/command_palette.zig b/src/tui/mode/overlay/command_palette.zig index 3eb0c2c..75e0e54 100644 --- a/src/tui/mode/overlay/command_palette.zig +++ b/src/tui/mode/overlay/command_palette.zig @@ -4,6 +4,7 @@ const tp = @import("thespian"); const root = @import("root"); const command = @import("command"); +const tui = @import("../../tui.zig"); pub const Type = @import("palette.zig").Create(@This()); pub const label = "Search commands"; @@ -13,20 +14,28 @@ pub const description = "command"; pub const Entry = struct { label: []const u8, name: []const u8, + hint: []const u8, id: command.ID, used_time: i64, }; -pub fn load_entries(palette: *Type) !void { +pub fn load_entries(palette: *Type) !usize { + const hints = if (tui.current().input_mode) |m| m.keybind_hints else @panic("no keybind hints"); + var longest_hint: usize = 0; for (command.commands.items) |cmd_| if (cmd_) |p| { - if (p.meta.description.len > 0) + if (p.meta.description.len > 0) { + const hint = hints.get(p.name) orelse ""; + longest_hint = @max(longest_hint, hint.len); (try palette.entries.addOne()).* = .{ .label = if (p.meta.description.len > 0) p.meta.description else p.name, .name = p.name, + .hint = hint, .id = p.id, .used_time = 0, }; + } }; + return longest_hint; } pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !void { @@ -34,7 +43,7 @@ pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !v defer value.deinit(); const writer = value.writer(); try cbor.writeValue(writer, entry.label); - try cbor.writeValue(writer, if (palette.hints) |hints| hints.get(entry.name) orelse "" else ""); + try cbor.writeValue(writer, entry.hint); try cbor.writeValue(writer, matches orelse &[_]usize{}); try cbor.writeValue(writer, entry.id); try palette.menu.add_item_with_handler(value.items, select); diff --git a/src/tui/mode/overlay/list_all_commands_palette.zig b/src/tui/mode/overlay/list_all_commands_palette.zig index e00972a..188bfba 100644 --- a/src/tui/mode/overlay/list_all_commands_palette.zig +++ b/src/tui/mode/overlay/list_all_commands_palette.zig @@ -4,6 +4,7 @@ const tp = @import("thespian"); const root = @import("root"); const command = @import("command"); +const tui = @import("../../tui.zig"); pub const Type = @import("palette.zig").Create(@This()); pub const label = "Search commands"; @@ -12,6 +13,7 @@ pub const description = "command"; pub const Entry = struct { label: []const u8, + hint: []const u8, id: command.ID, }; @@ -20,7 +22,9 @@ pub fn deinit(palette: *Type) void { palette.allocator.free(entry.label); } -pub fn load_entries(palette: *Type) !void { +pub fn load_entries(palette: *Type) !usize { + const hints = if (tui.current().input_mode) |m| m.keybind_hints else @panic("no keybind hints"); + var longest_hint: usize = 0; for (command.commands.items) |cmd_| if (cmd_) |p| { var label_ = std.ArrayList(u8).init(palette.allocator); const writer = label_.writer(); @@ -40,11 +44,16 @@ pub fn load_entries(palette: *Type) !void { try writer.writeAll("}"); } + const hint = hints.get(p.name) orelse ""; + longest_hint = @max(longest_hint, hint.len); + (try palette.entries.addOne()).* = .{ .label = try label_.toOwnedSlice(), + .hint = hint, .id = p.id, }; }; + return longest_hint; } pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !void { @@ -52,7 +61,7 @@ pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !v defer value.deinit(); const writer = value.writer(); try cbor.writeValue(writer, entry.label); - try cbor.writeValue(writer, if (palette.hints) |hints| hints.get(command.get_name(entry.id) orelse "") orelse "" else ""); + try cbor.writeValue(writer, entry.hint); try cbor.writeValue(writer, matches orelse &[_]usize{}); try cbor.writeValue(writer, entry.id); try palette.menu.add_item_with_handler(value.items, select); diff --git a/src/tui/mode/overlay/open_recent_project.zig b/src/tui/mode/overlay/open_recent_project.zig index 07f8ddc..ea49b75 100644 --- a/src/tui/mode/overlay/open_recent_project.zig +++ b/src/tui/mode/overlay/open_recent_project.zig @@ -24,7 +24,7 @@ pub fn deinit(palette: *Type) void { palette.allocator.free(entry.label); } -pub fn load_entries(palette: *Type) !void { +pub fn load_entries(palette: *Type) !usize { const rsp = try project_manager.request_recent_projects(palette.allocator); defer palette.allocator.free(rsp.buf); var iter: []const u8 = rsp.buf; @@ -35,6 +35,7 @@ pub fn load_entries(palette: *Type) !void { (try palette.entries.addOne()).* = .{ .label = try palette.allocator.dupe(u8, name_) }; } else return error.InvalidMessageField; } + return 1; } pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !void { diff --git a/src/tui/mode/overlay/palette.zig b/src/tui/mode/overlay/palette.zig index 70593c9..09926c1 100644 --- a/src/tui/mode/overlay/palette.zig +++ b/src/tui/mode/overlay/palette.zig @@ -32,7 +32,6 @@ pub fn Create(options: type) type { longest: usize = 0, commands: command.Collection(cmds) = undefined, entries: std.ArrayList(Entry) = undefined, - hints: ?*const tui.KeybindHints = null, longest_hint: usize = 0, initial_selected: ?usize = null, @@ -66,17 +65,11 @@ pub fn Create(options: type) type { .ctx = self, .label = options.label, }))).dynamic_cast(InputBox.State(*Self)) orelse unreachable, - .hints = if (tui.current().input_mode) |m| m.keybind_hints else null, .view_rows = get_view_rows(tui.current().screen()), .entries = std.ArrayList(Entry).init(allocator), }; self.menu.scrollbar.?.style_factory = scrollbar_style; - if (self.hints) |hints| { - var iter = hints.iterator(); - while (iter.next()) |p| - self.longest_hint = @max(self.longest_hint, p.value_ptr.len); - } - try options.load_entries(self); + self.longest_hint = try options.load_entries(self); if (@hasDecl(options, "restore_state")) options.restore_state(self) catch {}; try self.commands.init(self); diff --git a/src/tui/mode/overlay/theme_palette.zig b/src/tui/mode/overlay/theme_palette.zig index 69a7531..7b82706 100644 --- a/src/tui/mode/overlay/theme_palette.zig +++ b/src/tui/mode/overlay/theme_palette.zig @@ -23,7 +23,9 @@ pub const Match = struct { }; var previous_theme: ?[]const u8 = null; -pub fn load_entries(palette: *Type) !void { + +pub fn load_entries(palette: *Type) !usize { + var longest_hint: usize = 0; var idx: usize = 0; previous_theme = tui.current().theme.name; for (Widget.themes) |theme| { @@ -35,7 +37,9 @@ pub fn load_entries(palette: *Type) !void { if (previous_theme) |theme_name| if (std.mem.eql(u8, theme.name, theme_name)) { palette.initial_selected = idx; }; + longest_hint = @max(longest_hint, theme.name.len); } + return longest_hint; } pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !void {