From 33e36d295e389099d892c2573d508a48b19d9531 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Sun, 27 Oct 2024 11:30:52 +0100 Subject: [PATCH] refactor: move file_browser key bindings to keybinds module --- src/keybind/static/file_browser.zig | 80 ++++++++++++ src/keybind/static/root.zig | 1 + src/tui/mode/mini/file_browser.zig | 196 +++++++++++++--------------- 3 files changed, 175 insertions(+), 102 deletions(-) create mode 100644 src/keybind/static/file_browser.zig diff --git a/src/keybind/static/file_browser.zig b/src/keybind/static/file_browser.zig new file mode 100644 index 0000000..7b01d7a --- /dev/null +++ b/src/keybind/static/file_browser.zig @@ -0,0 +1,80 @@ +const tp = @import("thespian"); +const key = @import("renderer").input.key; +const mod = @import("renderer").input.modifier; +const event_type = @import("renderer").input.event_type; +const command = @import("command"); +const EventHandler = @import("EventHandler"); + +const Allocator = @import("std").mem.Allocator; + +const Mode = @import("root.zig").Mode; + +pub fn create(_: Allocator) !Mode { + return .{ .handler = EventHandler.static(@This()) }; +} + +pub fn receive(_: tp.pid_ref, m: tp.message) error{Exit}!bool { + var evtype: u32 = undefined; + var keypress: u32 = undefined; + var egc: u32 = undefined; + var modifiers: u32 = undefined; + + if (try m.match(.{ "I", tp.extract(&evtype), tp.extract(&keypress), tp.extract(&egc), tp.string, tp.extract(&modifiers) })) { + mapEvent(evtype, keypress, egc, modifiers) catch |e| return tp.exit_error(e, @errorReturnTrace()); + } + return false; +} + +fn mapEvent(evtype: u32, keypress: u32, egc: u32, modifiers: u32) !void { + switch (evtype) { + event_type.PRESS => try mapPress(keypress, egc, modifiers), + event_type.REPEAT => try mapPress(keypress, egc, modifiers), + else => {}, + } +} + +fn mapPress(keypress: u32, egc: u32, modifiers: u32) !void { + const keynormal = if ('a' <= keypress and keypress <= 'z') keypress - ('a' - 'A') else keypress; + return switch (modifiers) { + mod.CTRL => switch (keynormal) { + 'Q' => command.executeName("quit", .{}), + 'V' => command.executeName("system_paste", .{}), + 'U' => command.executeName("mini_mode_reset", .{}), + 'G' => command.executeName("mini_mode_cancel", .{}), + 'C' => command.executeName("mini_mode_cancel", .{}), + 'L' => command.executeName("scroll_view_center", .{}), + 'I' => command.executeName("mini_mode_insert_bytes", command.fmt(.{"\t"})), + key.SPACE => command.executeName("mini_mode_cancel", .{}), + key.BACKSPACE => command.executeName("mini_mode_delete_to_previous_path_segment", .{}), + else => {}, + }, + mod.ALT => switch (keynormal) { + 'V' => command.executeName("system_paste", .{}), + else => {}, + }, + mod.ALT | mod.SHIFT => switch (keynormal) { + 'V' => command.executeName("system_paste", .{}), + else => {}, + }, + mod.SHIFT => switch (keypress) { + key.TAB => command.executeName("mini_mode_reverse_complete_file", .{}), + else => if (!key.synthesized_p(keypress)) + command.executeName("mini_mode_insert_code_point", command.fmt(.{egc})) + else {}, + }, + 0 => switch (keypress) { + key.UP => command.executeName("mini_mode_reverse_complete_file", .{}), + key.DOWN => command.executeName("mini_mode_try_complete_file", .{}), + key.RIGHT => command.executeName("mini_mode_try_complete_file_forward", .{}), + key.LEFT => command.executeName("mini_mode_delete_to_previous_path_segment", .{}), + key.TAB => command.executeName("mini_mode_try_complete_file", .{}), + key.ESC => command.executeName("mini_mode_cancel", .{}), + key.ENTER => command.executeName("mini_mode_select", .{}), + key.BACKSPACE => command.executeName("mini_mode_delete_backwards", .{}), + else => if (!key.synthesized_p(keypress)) + command.executeName("mini_mode_insert_code_point", command.fmt(.{egc})) + else {}, + }, + else => {}, + }; +} diff --git a/src/keybind/static/root.zig b/src/keybind/static/root.zig index 6daa480..1c7b8f0 100644 --- a/src/keybind/static/root.zig +++ b/src/keybind/static/root.zig @@ -2,6 +2,7 @@ pub const mode = struct { pub const mini = struct { pub const goto = @import("goto.zig"); pub const move_to_char = @import("move_to_char.zig"); + pub const file_browser = @import("file_browser.zig"); }; }; diff --git a/src/tui/mode/mini/file_browser.zig b/src/tui/mode/mini/file_browser.zig index 2ea3eea..9eb870a 100644 --- a/src/tui/mode/mini/file_browser.zig +++ b/src/tui/mode/mini/file_browser.zig @@ -7,6 +7,7 @@ const root = @import("root"); const key = @import("renderer").input.key; const mod = @import("renderer").input.modifier; const event_type = @import("renderer").input.event_type; +const keybind = @import("keybind"); const ucs32_to_utf8 = @import("renderer").ucs32_to_utf8; const project_manager = @import("project_manager"); const command = @import("command"); @@ -26,7 +27,9 @@ pub fn Create(options: type) type { entries: std.ArrayList(Entry), complete_trigger_count: usize = 0, matched_entry: usize = 0, + commands: Commands = undefined, + const Commands = command.Collection(cmds); const Self = @This(); const Entry = struct { @@ -43,21 +46,22 @@ pub fn Create(options: type) type { .match = std.ArrayList(u8).init(allocator), .entries = std.ArrayList(Entry).init(allocator), }; + try self.commands.init(self); try tui.current().message_filters.add(MessageFilter.bind(self, receive_path_entry)); try options.load_entries(self); if (@hasDecl(options, "restore_state")) options.restore_state(self) catch {}; return .{ + try keybind.mode.mini.file_browser.create(allocator), .{ - .handler = EventHandler.to_owned(self), - }, - .{ + .event_handler = EventHandler.to_owned(self), .name = options.name(self), }, }; } pub fn deinit(self: *Self) void { + self.commands.deinit(); tui.current().message_filters.remove_ptr(self); self.clear_entries(); self.entries.deinit(); @@ -67,108 +71,10 @@ pub fn Create(options: type) type { self.allocator.destroy(self); } - pub fn receive(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool { - var evtype: u32 = undefined; - var keypress: u32 = undefined; - var egc: u32 = undefined; - var modifiers: u32 = undefined; - - defer { - if (tui.current().mini_mode) |*mini_mode| { - mini_mode.text = self.file_path.items; - mini_mode.cursor = self.file_path.items.len; - } - } - - if (try m.match(.{ "I", tp.extract(&evtype), tp.extract(&keypress), tp.extract(&egc), tp.string, tp.extract(&modifiers) })) { - self.mapEvent(evtype, keypress, egc, modifiers) catch |e| return tp.exit_error(e, @errorReturnTrace()); - } + pub fn receive(_: *Self, _: tp.pid_ref, _: tp.message) error{Exit}!bool { return false; } - fn mapEvent(self: *Self, evtype: u32, keypress: u32, egc: u32, modifiers: u32) !void { - switch (evtype) { - event_type.PRESS => try self.mapPress(keypress, egc, modifiers), - event_type.REPEAT => try self.mapPress(keypress, egc, modifiers), - event_type.RELEASE => try self.mapRelease(keypress, egc, modifiers), - else => {}, - } - } - - fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) !void { - const keynormal = if ('a' <= keypress and keypress <= 'z') keypress - ('a' - 'A') else keypress; - return switch (modifiers) { - mod.CTRL => switch (keynormal) { - 'Q' => self.cmd("quit", .{}), - 'V' => self.cmd("system_paste", .{}), - 'U' => self.file_path.clearRetainingCapacity(), - 'G' => self.cancel(), - 'C' => self.cancel(), - 'L' => self.cmd("scroll_view_center", .{}), - 'I' => self.insert_bytes("\t"), - key.SPACE => self.cancel(), - key.BACKSPACE => self.delete_to_previous_path_segment(), - else => {}, - }, - mod.ALT => switch (keynormal) { - 'V' => self.cmd("system_paste", .{}), - else => {}, - }, - mod.ALT | mod.SHIFT => switch (keynormal) { - 'V' => self.cmd("system_paste", .{}), - else => {}, - }, - mod.SHIFT => switch (keypress) { - key.TAB => self.reverse_complete_file(), - else => if (!key.synthesized_p(keypress)) - self.insert_code_point(egc) - else {}, - }, - 0 => switch (keypress) { - key.UP => self.reverse_complete_file(), - key.DOWN => self.try_complete_file(), - key.TAB => self.try_complete_file(), - key.ESC => self.cancel(), - key.ENTER => self.select(), - key.BACKSPACE => if (self.file_path.items.len > 0) { - self.complete_trigger_count = 0; - self.file_path.shrinkRetainingCapacity(self.file_path.items.len - 1); - }, - else => if (!key.synthesized_p(keypress)) - self.insert_code_point(egc) - else {}, - }, - else => {}, - }; - } - - fn mapRelease(_: *Self, _: u32, _: u32, _: u32) !void {} - - fn insert_code_point(self: *Self, c: u32) !void { - self.complete_trigger_count = 0; - var buf: [32]u8 = undefined; - const bytes = try ucs32_to_utf8(&[_]u32{c}, &buf); - try self.file_path.appendSlice(buf[0..bytes]); - } - - fn insert_bytes(self: *Self, bytes: []const u8) !void { - self.complete_trigger_count = 0; - try self.file_path.appendSlice(bytes); - } - - fn cmd(self: *Self, name_: []const u8, ctx: command.Context) tp.result { - self.complete_trigger_count = 0; - return command.executeName(name_, ctx); - } - - fn cancel(_: *Self) void { - command.executeName("exit_mini_mode", .{}) catch {}; - } - - fn select(self: *Self) void { - options.select(self); - } - fn clear_entries(self: *Self) void { for (self.entries.items) |entry| self.allocator.free(entry.name); self.entries.clearRetainingCapacity(); @@ -332,5 +238,91 @@ pub fn Create(options: type) type { var buf: [256]u8 = undefined; tp.self_pid().send(.{ "message", std.fmt.bufPrint(&buf, fmt, args) catch @panic("too large") }) catch {}; } + + fn update_mini_mode_text(self: *Self) void { + if (tui.current().mini_mode) |*mini_mode| { + mini_mode.text = self.file_path.items; + mini_mode.cursor = self.file_path.items.len; + } + } + + const cmds = struct { + pub const Target = Self; + const Ctx = command.Context; + const Result = command.Result; + + pub fn mini_mode_reset(self: *Self, _: Ctx) Result { + self.complete_trigger_count = 0; + self.file_path.clearRetainingCapacity(); + self.update_mini_mode_text(); + } + pub const mini_mode_reset_meta = .{ .description = "Clear input" }; + + pub fn mini_mode_cancel(_: *Self, _: Ctx) Result { + command.executeName("exit_mini_mode", .{}) catch {}; + } + pub const mini_mode_cancel_meta = .{ .description = "Cancel input" }; + + pub fn mini_mode_delete_to_previous_path_segment(self: *Self, _: Ctx) Result { + self.delete_to_previous_path_segment(); + self.update_mini_mode_text(); + } + pub const mini_mode_delete_to_previous_path_segment_meta = .{ .description = "Delete to previous path segment" }; + + pub fn mini_mode_delete_backwards(self: *Self, _: Ctx) Result { + if (self.file_path.items.len > 0) { + self.complete_trigger_count = 0; + self.file_path.shrinkRetainingCapacity(self.file_path.items.len - 1); + } + self.update_mini_mode_text(); + } + pub const mini_mode_delete_backwards_meta = .{ .description = "Delete backwards" }; + + pub fn mini_mode_try_complete_file(self: *Self, _: Ctx) Result { + self.try_complete_file() catch |e| return tp.exit_error(e, @errorReturnTrace()); + self.update_mini_mode_text(); + } + pub const mini_mode_try_complete_file_meta = .{ .description = "Complete file" }; + + pub fn mini_mode_try_complete_file_forward(self: *Self, ctx: Ctx) Result { + self.complete_trigger_count = 0; + return mini_mode_try_complete_file(self, ctx); + } + pub const mini_mode_try_complete_file_forward_meta = .{ .description = "Complete file forward" }; + + pub fn mini_mode_reverse_complete_file(self: *Self, _: Ctx) Result { + self.reverse_complete_file() catch |e| return tp.exit_error(e, @errorReturnTrace()); + self.update_mini_mode_text(); + } + pub const mini_mode_reverse_complete_file_meta = .{ .description = "Reverse complete file" }; + + pub fn mini_mode_insert_code_point(self: *Self, ctx: Ctx) Result { + var egc: u32 = 0; + if (!try ctx.args.match(.{tp.extract(&egc)})) + return error.InvalidArgument; + self.complete_trigger_count = 0; + var buf: [32]u8 = undefined; + const bytes = try ucs32_to_utf8(&[_]u32{egc}, &buf); + try self.file_path.appendSlice(buf[0..bytes]); + self.update_mini_mode_text(); + } + 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.complete_trigger_count = 0; + try self.file_path.appendSlice(bytes); + self.update_mini_mode_text(); + } + pub const mini_mode_insert_bytes_meta = .{ .interactive = false }; + + pub fn mini_mode_select(self: *Self, _: Ctx) Result { + options.select(self); + self.update_mini_mode_text(); + } + pub const mini_mode_select_meta = .{ .description = "Select" }; + }; }; }