refactor: add insert events to keybindview

This commit is contained in:
CJ van den Berg 2025-12-10 17:39:14 +01:00
parent fe03d0dab4
commit b08b162a10
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
3 changed files with 36 additions and 9 deletions

View file

@ -25,6 +25,7 @@ const builtin_keybinds = std.StaticStringMap([]const u8).initComptime(.{
});
pub var enable_match_events: bool = false;
pub var enable_insert_events: bool = false;
var integer_argument: ?usize = null;
var mode_flag: KeybindMode = .normal;
@ -647,6 +648,8 @@ const BindingSet = struct {
globals.insert_command = self.insert_command;
globals.insert_command_id = null;
}
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);
@ -801,6 +804,10 @@ const BindingSet = struct {
_ = tp.self_pid().send_raw(.{ .buf = stream.buffered() }) catch {};
}
fn send_insert_event(self: *const @This(), insert_cmd: []const u8, bytes: []const u8) void {
_ = tp.self_pid().send(.{ "N", get_namespace(), self.config_section, insert_cmd, bytes }) catch {};
}
fn log_keyhints_message() void {
for (globals.current_sequence.items) |item| switch (item.key) {
input.key.left_control, input.key.right_control => return,

View file

@ -3,6 +3,7 @@ const time = @import("std").time;
const Allocator = @import("std").mem.Allocator;
const ArrayList = @import("std").ArrayList;
const Writer = @import("std").Io.Writer;
const hexEscape = @import("std").ascii.hexEscape;
const tp = @import("thespian");
const cbor = @import("cbor");
@ -89,23 +90,37 @@ fn keybind_match(self: *Self, _: tp.pid_ref, m: tp.message) MessageFilter.Error!
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 insert_cmd: []const u8 = undefined;
var bytes: []const u8 = undefined;
var result: Writer.Allocating = .init(self.allocator);
defer result.deinit();
const writer = &result.writer;
if (m.match(.{ "K", tp.extract(&namespace), tp.extract(&section), tp.extract(&key_event), tp.extract_cbor(&cmds) }) catch 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;
writer.print("{s}:{s} {s} => ", .{ namespace, section, key_event }) catch return true;
cbor.toJsonWriter(cmds, writer, .{}) catch return true;
self.append(result.toOwnedSlice() catch return true);
return true;
} else if (m.match(.{ "N", tp.extract(&namespace), tp.extract(&section), tp.extract(&insert_cmd), tp.extract(&bytes) }) catch false) {
var result: Writer.Allocating = .init(self.allocator);
defer result.deinit();
result.writer.print("{s}:{s} insert => [\"{s}\", \"{f}\"] ", .{ namespace, section, insert_cmd, hexEscape(bytes, .lower) }) catch return true;
self.append(result.toOwnedSlice() catch return true);
return true;
}
return false;
}
pub fn append(self: *Self, msg: []const u8) void {
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)).* = .{
(self.buffer.addOne(self.allocator) catch return).* = .{
.time = ts,
.tdiff = tdiff,
.msg = result.toOwnedSlice() catch return true,
.msg = msg,
};
return true;
}
pub fn receive(_: *Self, _: tp.pid_ref, _: tp.message) error{Exit}!bool {

View file

@ -503,6 +503,9 @@ fn receive_safe(self: *Self, from: tp.pid_ref, m: tp.message) !void {
if (try m.match(.{ "K", tp.more }))
return;
if (try m.match(.{ "N", tp.more }))
return;
if (try self.send_widgets(from, m))
return;
@ -2363,8 +2366,10 @@ pub fn set_last_palette(type_: PaletteType, ctx: command.Context) void {
pub fn enable_match_events() void {
keybind.enable_match_events = true;
keybind.enable_insert_events = true;
}
pub fn disable_match_events() void {
keybind.enable_match_events = false;
keybind.enable_insert_events = false;
}