feat: add support for command metadata

This commit is contained in:
CJ van den Berg 2024-09-17 22:57:21 +02:00
parent 6b43dd4f28
commit bdd16f43fb
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
6 changed files with 77 additions and 42 deletions

View file

@ -12,6 +12,7 @@ pub const name = "󱊒 command";
pub const description = "command";
pub const Entry = struct {
label: []const u8,
name: []const u8,
id: command.ID,
used_time: i64,
@ -19,7 +20,13 @@ pub const Entry = struct {
pub fn load_entries(palette: *Type) !void {
for (command.commands.items) |cmd_| if (cmd_) |p| {
(try palette.entries.addOne()).* = .{ .name = p.name, .id = p.id, .used_time = 0 };
if (p.meta.interactive)
(try palette.entries.addOne()).* = .{
.label = if (p.meta.description.len > 0) p.meta.description else p.name,
.name = p.name,
.id = p.id,
.used_time = 0,
};
};
}
@ -27,24 +34,27 @@ pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !v
var value = std.ArrayList(u8).init(palette.allocator);
defer value.deinit();
const writer = value.writer();
try cbor.writeValue(writer, entry.name);
try cbor.writeValue(writer, entry.id);
try cbor.writeValue(writer, entry.label);
try cbor.writeValue(writer, if (palette.hints) |hints| hints.get(entry.name) orelse "" else "");
if (matches) |matches_|
try cbor.writeValue(writer, matches_);
try cbor.writeValue(writer, matches orelse &[_]usize{});
try cbor.writeValue(writer, entry.id);
try palette.menu.add_item_with_handler(value.items, select);
palette.items += 1;
}
fn select(menu: **Type.MenuState, button: *Type.ButtonState) void {
var command_name: []const u8 = undefined;
var unused: []const u8 = undefined;
var command_id: command.ID = undefined;
var iter = button.opts.label;
if (!(cbor.matchString(&iter, &command_name) catch false)) return;
if (!(cbor.matchString(&iter, &unused) catch false)) return;
if (!(cbor.matchString(&iter, &unused) catch false)) return;
var len = cbor.decodeArrayHeader(&iter) catch return;
while (len > 0) : (len -= 1)
cbor.skipValue(&iter) catch break;
if (!(cbor.matchValue(&iter, cbor.extract(&command_id)) catch false)) return;
update_used_time(menu.*.opts.ctx, command_id);
tp.self_pid().send(.{ "cmd", "exit_overlay_mode" }) catch |e| menu.*.opts.ctx.logger.err("navigate", e);
tp.self_pid().send(.{ "cmd", command_name, .{} }) catch |e| menu.*.opts.ctx.logger.err("navigate", e);
tp.self_pid().send(.{ "cmd", command_id, .{} }) catch |e| menu.*.opts.ctx.logger.err("navigate", e);
}
fn sort_by_used_time(palette: *Type) void {