From 30a457158c2e693bee17c98dc095b9906074c09c Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Wed, 17 Sep 2025 22:18:20 +0200 Subject: [PATCH] feat: add goto_offset mini mode and command --- src/tui/mode/mini/goto_offset.zig | 58 +++++++++++++++++++++++++++++++ src/tui/tui.zig | 5 +++ 2 files changed, 63 insertions(+) create mode 100644 src/tui/mode/mini/goto_offset.zig diff --git a/src/tui/mode/mini/goto_offset.zig b/src/tui/mode/mini/goto_offset.zig new file mode 100644 index 0000000..221f8a1 --- /dev/null +++ b/src/tui/mode/mini/goto_offset.zig @@ -0,0 +1,58 @@ +const fmt = @import("std").fmt; +const command = @import("command"); + +const tui = @import("../../tui.zig"); +const Cursor = @import("../../editor.zig").Cursor; + +pub const Type = @import("numeric_input.zig").Create(@This()); +pub const create = Type.create; + +pub const ValueType = struct { + cursor: Cursor = .{}, + offset: usize = 0, +}; + +pub fn name(_: *Type) []const u8 { + return "#goto byte"; +} + +pub fn start(_: *Type) ValueType { + const editor = tui.get_active_editor() orelse return .{}; + return .{ .cursor = editor.get_primary().cursor }; +} + +pub fn process_digit(self: *Type, digit: u8) void { + switch (digit) { + 0...9 => { + if (self.input) |*input| { + input.offset = input.offset * 10 + digit; + } else { + self.input = .{ .offset = digit }; + } + }, + else => unreachable, + } +} + +pub fn delete(self: *Type, input: *ValueType) void { + const newval = if (input.offset < 10) 0 else input.offset / 10; + if (newval == 0) self.input = null else input.offset = newval; +} + +pub fn format_value(_: *Type, input_: ?ValueType, buf: []u8) []const u8 { + return if (input_) |input| + fmt.bufPrint(buf, "{d}", .{input.offset}) catch "" + else + ""; +} + +pub const preview = goto; +pub const apply = goto; +pub const cancel = goto; + +fn goto(self: *Type, _: command.Context) void { + if (self.input) |input| + command.executeName("goto_byte_offset", command.fmt(.{input.offset})) catch {} + else + command.executeName("goto_line_and_column", command.fmt(.{ self.start.cursor.row, self.start.cursor.col })) catch {}; +} diff --git a/src/tui/tui.zig b/src/tui/tui.zig index 0577836..f2ecb77 100644 --- a/src/tui/tui.zig +++ b/src/tui/tui.zig @@ -1038,6 +1038,11 @@ const cmds = struct { } pub const goto_meta: Meta = .{ .description = "Goto line" }; + pub fn goto_offset(self: *Self, ctx: Ctx) Result { + return enter_mini_mode(self, @import("mode/mini/goto_offset.zig"), ctx); + } + pub const goto_offset_meta: Meta = .{ .description = "Goto byte offset" }; + pub fn move_to_char(self: *Self, ctx: Ctx) Result { return enter_mini_mode(self, @import("mode/mini/move_to_char.zig"), ctx); }