refactor: move hints out of generic palette

This commit is contained in:
CJ van den Berg 2024-12-10 20:08:43 +01:00
parent 8f3b8198e2
commit b3bd6d19b5
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
5 changed files with 31 additions and 15 deletions

View file

@ -4,6 +4,7 @@ const tp = @import("thespian");
const root = @import("root"); const root = @import("root");
const command = @import("command"); const command = @import("command");
const tui = @import("../../tui.zig");
pub const Type = @import("palette.zig").Create(@This()); pub const Type = @import("palette.zig").Create(@This());
pub const label = "Search commands"; pub const label = "Search commands";
@ -13,20 +14,28 @@ pub const description = "command";
pub const Entry = struct { pub const Entry = struct {
label: []const u8, label: []const u8,
name: []const u8, name: []const u8,
hint: []const u8,
id: command.ID, id: command.ID,
used_time: i64, 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| { 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()).* = .{ (try palette.entries.addOne()).* = .{
.label = if (p.meta.description.len > 0) p.meta.description else p.name, .label = if (p.meta.description.len > 0) p.meta.description else p.name,
.name = p.name, .name = p.name,
.hint = hint,
.id = p.id, .id = p.id,
.used_time = 0, .used_time = 0,
}; };
}
}; };
return longest_hint;
} }
pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !void { 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(); defer value.deinit();
const writer = value.writer(); const writer = value.writer();
try cbor.writeValue(writer, entry.label); 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, matches orelse &[_]usize{});
try cbor.writeValue(writer, entry.id); try cbor.writeValue(writer, entry.id);
try palette.menu.add_item_with_handler(value.items, select); try palette.menu.add_item_with_handler(value.items, select);

View file

@ -4,6 +4,7 @@ const tp = @import("thespian");
const root = @import("root"); const root = @import("root");
const command = @import("command"); const command = @import("command");
const tui = @import("../../tui.zig");
pub const Type = @import("palette.zig").Create(@This()); pub const Type = @import("palette.zig").Create(@This());
pub const label = "Search commands"; pub const label = "Search commands";
@ -12,6 +13,7 @@ pub const description = "command";
pub const Entry = struct { pub const Entry = struct {
label: []const u8, label: []const u8,
hint: []const u8,
id: command.ID, id: command.ID,
}; };
@ -20,7 +22,9 @@ pub fn deinit(palette: *Type) void {
palette.allocator.free(entry.label); 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| { for (command.commands.items) |cmd_| if (cmd_) |p| {
var label_ = std.ArrayList(u8).init(palette.allocator); var label_ = std.ArrayList(u8).init(palette.allocator);
const writer = label_.writer(); const writer = label_.writer();
@ -40,11 +44,16 @@ pub fn load_entries(palette: *Type) !void {
try writer.writeAll("}"); try writer.writeAll("}");
} }
const hint = hints.get(p.name) orelse "";
longest_hint = @max(longest_hint, hint.len);
(try palette.entries.addOne()).* = .{ (try palette.entries.addOne()).* = .{
.label = try label_.toOwnedSlice(), .label = try label_.toOwnedSlice(),
.hint = hint,
.id = p.id, .id = p.id,
}; };
}; };
return longest_hint;
} }
pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !void { 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(); defer value.deinit();
const writer = value.writer(); const writer = value.writer();
try cbor.writeValue(writer, entry.label); 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, matches orelse &[_]usize{});
try cbor.writeValue(writer, entry.id); try cbor.writeValue(writer, entry.id);
try palette.menu.add_item_with_handler(value.items, select); try palette.menu.add_item_with_handler(value.items, select);

View file

@ -24,7 +24,7 @@ pub fn deinit(palette: *Type) void {
palette.allocator.free(entry.label); 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); const rsp = try project_manager.request_recent_projects(palette.allocator);
defer palette.allocator.free(rsp.buf); defer palette.allocator.free(rsp.buf);
var iter: []const u8 = 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_) }; (try palette.entries.addOne()).* = .{ .label = try palette.allocator.dupe(u8, name_) };
} else return error.InvalidMessageField; } else return error.InvalidMessageField;
} }
return 1;
} }
pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !void { pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !void {

View file

@ -32,7 +32,6 @@ pub fn Create(options: type) type {
longest: usize = 0, longest: usize = 0,
commands: command.Collection(cmds) = undefined, commands: command.Collection(cmds) = undefined,
entries: std.ArrayList(Entry) = undefined, entries: std.ArrayList(Entry) = undefined,
hints: ?*const tui.KeybindHints = null,
longest_hint: usize = 0, longest_hint: usize = 0,
initial_selected: ?usize = null, initial_selected: ?usize = null,
@ -66,17 +65,11 @@ pub fn Create(options: type) type {
.ctx = self, .ctx = self,
.label = options.label, .label = options.label,
}))).dynamic_cast(InputBox.State(*Self)) orelse unreachable, }))).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()), .view_rows = get_view_rows(tui.current().screen()),
.entries = std.ArrayList(Entry).init(allocator), .entries = std.ArrayList(Entry).init(allocator),
}; };
self.menu.scrollbar.?.style_factory = scrollbar_style; self.menu.scrollbar.?.style_factory = scrollbar_style;
if (self.hints) |hints| { self.longest_hint = try options.load_entries(self);
var iter = hints.iterator();
while (iter.next()) |p|
self.longest_hint = @max(self.longest_hint, p.value_ptr.len);
}
try options.load_entries(self);
if (@hasDecl(options, "restore_state")) if (@hasDecl(options, "restore_state"))
options.restore_state(self) catch {}; options.restore_state(self) catch {};
try self.commands.init(self); try self.commands.init(self);

View file

@ -23,7 +23,9 @@ pub const Match = struct {
}; };
var previous_theme: ?[]const u8 = null; 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; var idx: usize = 0;
previous_theme = tui.current().theme.name; previous_theme = tui.current().theme.name;
for (Widget.themes) |theme| { 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)) { if (previous_theme) |theme_name| if (std.mem.eql(u8, theme.name, theme_name)) {
palette.initial_selected = idx; 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 { pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !void {