fix: task palette width calculation properly this time

This commit is contained in:
CJ van den Berg 2025-11-10 16:39:45 +01:00
parent 7d740bfa2a
commit 6502989fb8
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 20 additions and 11 deletions

View file

@ -84,6 +84,7 @@ pub fn Create(options: type) type {
.view_rows = get_view_rows(tui.screen()),
.entries = .empty,
};
try self.commands.init(self);
if (self.menu.scrollbar) |scrollbar| scrollbar.style_factory = scrollbar_style;
self.longest_hint = if (@hasDecl(options, "load_entries_with_args"))
try options.load_entries_with_args(self, ctx)
@ -91,7 +92,6 @@ pub fn Create(options: type) type {
try options.load_entries(self);
if (@hasDecl(options, "restore_state"))
options.restore_state(self) catch {};
try self.commands.init(self);
if (@hasDecl(options, "initial_query")) blk: {
const initial_query = options.initial_query(self, self.allocator) catch break :blk;
defer self.allocator.free(initial_query);

View file

@ -27,19 +27,28 @@ pub fn load_entries(palette: *Type) !usize {
defer palette.allocator.free(rsp.buf);
var iter: []const u8 = rsp.buf;
var len = try cbor.decodeArrayHeader(&iter);
var longest: usize = 0;
while (len > 0) : (len -= 1) {
var task: []const u8 = undefined;
if (try cbor.matchValue(&iter, cbor.extract(&task))) {
if (!try cbor.matchValue(&iter, cbor.extract(&task))) return error.InvalidTaskMessageField;
(try palette.entries.addOne(palette.allocator)).* = .{ .label = try palette.allocator.dupe(u8, task) };
} else return error.InvalidTaskMessageField;
longest = @max(longest, task.len);
}
(try palette.entries.addOne(palette.allocator)).* = .{ .label = "", .command = "add_task" };
(try palette.entries.addOne(palette.allocator)).* = .{ .label = "", .command = "palette_menu_delete_item" };
return if (palette.entries.items.len == 0) label.len else blk: {
var longest: usize = 0;
for (palette.entries.items) |item| longest = @max(longest, item.label.len);
break :blk longest + 3;
};
const hints = if (tui.input_mode()) |m| m.keybind_hints else @panic("no keybind hints");
var longest_hint: usize = 0;
longest_hint = @max(longest_hint, try add_palette_command(palette, "add_task", hints));
longest_hint = @max(longest_hint, try add_palette_command(palette, "palette_menu_delete_item", hints));
return longest_hint - @min(longest_hint, longest) + 3;
}
fn add_palette_command(palette: *Type, command_name: []const u8, hints: *const tui.KeybindHints) !usize {
const id = command.get_id(command_name) orelse return 0;
var width: usize = 0;
if (command.get_icon(id)) |icon| width += tui.egc_chunk_width(icon, 0, 1);
if (command.get_description(id)) |desc| width += tui.egc_chunk_width(desc, 0, 1);
if (hints.get(command_name)) |hint| width += tui.egc_chunk_width(hint, 0, 1);
(try palette.entries.addOne(palette.allocator)).* = .{ .label = "", .command = command_name };
return width;
}
pub fn clear_entries(palette: *Type) void {