refactor: make keybindview show key event and make it pretty

This commit is contained in:
CJ van den Berg 2025-12-10 16:28:02 +01:00
parent e9da2d5cbe
commit 5febf537a7
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 26 additions and 21 deletions

View file

@ -675,7 +675,7 @@ const BindingSet = struct {
const key_event = input.KeyEvent.from_message(event, keypress, keypress_shifted, text, modifiers);
if (self.process_key_event(key_event) catch |e| return tp.exit_error(e, @errorReturnTrace())) |binding| {
if (enable_match_events)
self.send_match_event(binding);
self.send_match_event(key_event, binding);
for (binding.commands) |*cmd| try cmd.execute();
}
} else if (try m.match(.{"F"})) {
@ -774,13 +774,19 @@ const BindingSet = struct {
}
}
fn send_match_event(self: *const @This(), binding: *const Binding) void {
fn send_match_event(self: *const @This(), key_event: KeyEvent, binding: *const Binding) void {
var buf: [tp.max_message_size]u8 = undefined;
var stream: std.Io.Writer = .fixed(&buf);
cbor.writeArrayHeader(&stream, 4) catch return;
var key_event_buf: [256]u8 = undefined;
var key_event_str: std.Io.Writer = .fixed(&key_event_buf);
key_event_str.print("{f}", .{key_event}) catch return;
cbor.writeArrayHeader(&stream, 5) catch return;
cbor.writeValue(&stream, "K") catch return;
cbor.writeValue(&stream, self.name) catch return;
cbor.writeValue(&stream, get_namespace()) catch return;
cbor.writeValue(&stream, self.config_section) catch return;
cbor.writeValue(&stream, key_event_str.buffered()) catch return;
cbor.writeArrayHeader(&stream, binding.commands.len) catch return;
for (binding.commands) |cmd| {
cbor.writeArrayHeader(&stream, 2) catch return;

View file

@ -26,7 +26,7 @@ const Self = @This();
const Entry = struct {
time: i64,
tdiff: i64,
msg: []u8,
msg: []const u8,
};
const Buffer = ArrayList(Entry);
@ -84,28 +84,27 @@ fn output_tdiff(self: *Self, tdiff: i64) !void {
}
}
fn append(self: *Self, msg: []const u8) !void {
fn keybind_match(self: *Self, _: tp.pid_ref, m: tp.message) MessageFilter.Error!bool {
var namespace: []const u8 = undefined;
var section: []const u8 = undefined;
var key_event: []const u8 = undefined;
var cmds: []const u8 = undefined;
if (!(m.match(.{ "K", tp.extract(&namespace), tp.extract(&section), tp.extract(&key_event), tp.extract_cbor(&cmds) }) catch false)) return false;
var result: Writer.Allocating = .init(self.allocator);
defer result.deinit();
const writer = &result.writer;
writer.print("{s}:{s} {s} -> ", .{ namespace, section, key_event }) catch return true;
cbor.toJsonWriter(cmds, writer, .{}) catch return true;
const ts = time.microTimestamp();
const tdiff = if (self.buffer.items.len > 0) ts -| self.buffer.items[self.buffer.items.len - 1].time else 0;
(try self.buffer.addOne(self.allocator)).* = .{
.time = ts,
.tdiff = tdiff,
.msg = try self.allocator.dupeZ(u8, msg),
.msg = result.toOwnedSlice() catch return true,
};
}
fn keybind_match(self: *Self, _: tp.pid_ref, m: tp.message) MessageFilter.Error!bool {
var namespace: []const u8 = undefined;
var section: []const u8 = undefined;
var cmds: []const u8 = undefined;
if (!(m.match(.{ "K", tp.extract(&namespace), tp.extract(&section), tp.extract_cbor(&cmds) }) catch false)) return false;
var result: Writer.Allocating = .init(self.allocator);
defer result.deinit();
const writer = &result.writer;
cbor.toJsonWriter(m.buf, writer, .{}) catch return true;
self.append(result.written()) catch return true;
return true;
}