refactor: use command.command_names index and improve logging of commands

We now use the command name index more consistently to allow for
pre-allocating command IDs and better logging when commands are not
found.

This is a major, but hopefully non-breaking, change to command execution.
This commit is contained in:
CJ van den Berg 2025-12-23 13:45:13 +01:00
parent 6c60e5a0df
commit f75cc9b845
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
4 changed files with 57 additions and 53 deletions

View file

@ -349,19 +349,17 @@ const Command = struct {
fn execute(self: *@This()) !void {
const id = self.command_id orelse
command.get_id_cache(self.command, &self.command_id) orelse {
return command.notFoundError(self.command);
};
command.get_id_cache(self.command, &self.command_id);
var buf: [2048]u8 = undefined;
@memcpy(buf[0..self.args.len], self.args);
if (integer_argument) |int_arg| {
if (cbor.match(self.args, .{}) catch false and has_integer_argument(id)) {
integer_argument = null;
try command.execute(id, command.fmt(.{int_arg}));
try command.execute(id, self.command, command.fmt(.{int_arg}));
return;
}
}
try command.execute(id, .{ .args = .{ .buf = buf[0..self.args.len] } });
try command.execute(id, self.command, .{ .args = .{ .buf = buf[0..self.args.len] } });
}
fn execute_const(self: *const @This()) void {
@ -662,11 +660,9 @@ const BindingSet = struct {
if (enable_insert_events)
self.send_insert_event(globals.insert_command, globals.input_buffer.items);
const id = globals.insert_command_id orelse
command.get_id_cache(globals.insert_command, &globals.insert_command_id) orelse {
return tp.exit_error(error.InputTargetNotFound, null);
};
command.get_id_cache(globals.insert_command, &globals.insert_command_id);
if (!builtin.is_test) {
try command.execute(id, command.fmt(.{globals.input_buffer.items}));
try command.execute(id, globals.insert_command, command.fmt(.{globals.input_buffer.items}));
}
}
}