From 5febf537a787e863ec2cce6e81019869dcf50183 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Wed, 10 Dec 2025 16:28:02 +0100 Subject: [PATCH] refactor: make keybindview show key event and make it pretty --- src/keybind/keybind.zig | 14 ++++++++++---- src/tui/keybindview.zig | 33 ++++++++++++++++----------------- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/keybind/keybind.zig b/src/keybind/keybind.zig index 2ca6d50..d22c9fb 100644 --- a/src/keybind/keybind.zig +++ b/src/keybind/keybind.zig @@ -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; diff --git a/src/tui/keybindview.zig b/src/tui/keybindview.zig index 3e5c3f3..4e2f532 100644 --- a/src/tui/keybindview.zig +++ b/src/tui/keybindview.zig @@ -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(§ion), 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(§ion), 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; }