fix: move_to_char with dynamic bindings

This commit is contained in:
CJ van den Berg 2024-11-18 00:01:23 +01:00
parent 27a54a6091
commit 44c643815a
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 20 additions and 18 deletions

View file

@ -258,6 +258,11 @@
"syntax": "flow", "syntax": "flow",
"on_match_failure": "insert", "on_match_failure": "insert",
"bindings": [ "bindings": [
["ctrl+g", "mini_mode_cancel"],
["ctrl+c", "mini_mode_cancel"],
["ctrl+l", "scroll_view_center_cycle"],
["escape", "mini_mode_cancel"],
["backspace", "mini_mode_cancel"]
] ]
}, },
"mini/file_browser": { "mini/file_browser": {

View file

@ -43,7 +43,9 @@ pub fn create(allocator: Allocator, ctx: command.Context) !struct { tui.Mode, tu
try self.commands.init(self); try self.commands.init(self);
return .{ return .{
.{ .{
.input_handler = try keybind.mode.mini.move_to_char.create(allocator, .{}), .input_handler = try keybind.mode.mini.move_to_char.create(allocator, .{
.insert_command = "mini_mode_insert_bytes",
}),
.event_handler = EventHandler.to_owned(self), .event_handler = EventHandler.to_owned(self),
}, },
.{ .{
@ -74,7 +76,7 @@ pub fn receive(_: *Self, _: tp.pid_ref, _: tp.message) error{Exit}!bool {
return false; return false;
} }
fn execute_operation(self: *Self, c: u32) void { fn execute_operation(self: *Self, ctx: command.Context) command.Result {
const cmd = switch (self.direction) { const cmd = switch (self.direction) {
.left => switch (self.operation) { .left => switch (self.operation) {
.move => "move_to_char_left", .move => "move_to_char_left",
@ -85,10 +87,8 @@ fn execute_operation(self: *Self, c: u32) void {
.select => "select_to_char_right", .select => "select_to_char_right",
}, },
}; };
var buf: [6]u8 = undefined; try command.executeName(cmd, ctx);
const bytes = input.ucs32_to_utf8(&[_]u32{c}, &buf) catch return; try command.executeName("exit_mini_mode", .{});
command.executeName(cmd, command.fmt(.{buf[0..bytes]})) catch {};
command.executeName("exit_mini_mode", .{}) catch {};
} }
const cmds = struct { const cmds = struct {
@ -102,21 +102,18 @@ const cmds = struct {
return error.InvalidArgument; return error.InvalidArgument;
var buf: [6]u8 = undefined; var buf: [6]u8 = undefined;
const bytes = input.ucs32_to_utf8(&[_]u32{code_point}, &buf) catch return error.InvalidArgument; const bytes = input.ucs32_to_utf8(&[_]u32{code_point}, &buf) catch return error.InvalidArgument;
const cmd = switch (self.direction) { return self.execute_operation(command.fmt(.{buf[0..bytes]}));
.left => switch (self.operation) {
.move => "move_to_char_left",
.select => "select_to_char_left",
},
.right => switch (self.operation) {
.move => "move_to_char_right",
.select => "select_to_char_right",
},
};
try command.executeName(cmd, command.fmt(.{buf[0..bytes]}));
try command.executeName("exit_mini_mode", .{});
} }
pub const mini_mode_insert_code_point_meta = .{ .interactive = false }; pub const mini_mode_insert_code_point_meta = .{ .interactive = false };
pub fn mini_mode_insert_bytes(self: *Self, ctx: Ctx) Result {
var bytes: []const u8 = undefined;
if (!try ctx.args.match(.{tp.extract(&bytes)}))
return error.InvalidArgument;
return self.execute_operation(ctx);
}
pub const mini_mode_insert_bytes_meta = .{ .interactive = false };
pub fn mini_mode_cancel(_: *Self, _: Ctx) Result { pub fn mini_mode_cancel(_: *Self, _: Ctx) Result {
command.executeName("exit_mini_mode", .{}) catch {}; command.executeName("exit_mini_mode", .{}) catch {};
} }