fix: simplify goto mode bindings

This commit is contained in:
CJ van den Berg 2024-11-17 23:53:37 +01:00
parent 22cc818ad6
commit 2f0da48c6c
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 33 additions and 19 deletions

View file

@ -242,6 +242,7 @@
"on_match_failure": "insert",
"bindings": [
["ctrl+q", "quit"],
["ctrl+v", "system_paste"],
["ctrl+u", "mini_mode_reset"],
["ctrl+g", "mini_mode_cancel"],
["ctrl+c", "mini_mode_cancel"],
@ -250,17 +251,7 @@
["escape", "mini_mode_cancel"],
["enter", "exit_mini_mode"],
["backspace", "mini_mode_delete_backwards"],
["0", "mini_mode_insert_code_point", 0],
["1", "mini_mode_insert_code_point", 1],
["2", "mini_mode_insert_code_point", 2],
["3", "mini_mode_insert_code_point", 3],
["4", "mini_mode_insert_code_point", 4],
["5", "mini_mode_insert_code_point", 5],
["6", "mini_mode_insert_code_point", 6],
["7", "mini_mode_insert_code_point", 7],
["8", "mini_mode_insert_code_point", 8],
["9", "mini_mode_insert_code_point", 9]
["backspace", "mini_mode_delete_backwards"]
]
},
"mini/move_to_char": {

View file

@ -34,7 +34,9 @@ pub fn create(allocator: Allocator, _: command.Context) !struct { tui.Mode, tui.
try self.commands.init(self);
return .{
.{
.input_handler = try keybind.mode.mini.goto.create(allocator, .{}),
.input_handler = try keybind.mode.mini.goto.create(allocator, .{
.insert_command = "mini_mode_insert_bytes",
}),
.event_handler = EventHandler.to_owned(self),
},
.{
@ -69,6 +71,23 @@ fn goto(self: *Self) void {
command.executeName("goto_line", command.fmt(.{self.input orelse self.start})) catch {};
}
fn insert_char(self: *Self, char: u8) void {
switch (char) {
'0' => {
if (self.input) |linenum| self.input = linenum * 10;
},
'1'...'9' => {
const digit: usize = @intCast(char - '0');
self.input = if (self.input) |x| x * 10 + digit else digit;
},
else => {},
}
}
fn insert_bytes(self: *Self, bytes: []const u8) void {
for (bytes) |c| self.insert_char(c);
}
const cmds = struct {
pub const Target = Self;
const Ctx = command.Context;
@ -103,17 +122,21 @@ const cmds = struct {
if (!try ctx.args.match(.{tp.extract(&keypress)}))
return error.InvalidArgument;
switch (keypress) {
'0' => {
if (self.input) |linenum| self.input = linenum * 10;
},
'1'...'9' => {
const digit: usize = @intCast(keypress - '0');
self.input = if (self.input) |x| x * 10 + digit else digit;
},
'0'...'9' => self.insert_char(@intCast(keypress)),
else => {},
}
self.update_mini_mode_text();
self.goto();
}
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;
self.insert_bytes(bytes);
self.update_mini_mode_text();
self.goto();
}
pub const mini_mode_insert_bytes_meta = .{ .interactive = false };
};