feat: add support for arguments to mini/numeric_input modes

This commit is contained in:
CJ van den Berg 2025-08-12 12:04:36 +02:00
parent 1fcec1bab5
commit 63a527726a
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
3 changed files with 41 additions and 15 deletions

View file

@ -1,5 +1,7 @@
const command = @import("command"); const command = @import("command");
const tui = @import("../../tui.zig");
pub const Type = @import("numeric_input.zig").Create(@This()); pub const Type = @import("numeric_input.zig").Create(@This());
pub const create = Type.create; pub const create = Type.create;
@ -7,10 +9,15 @@ pub fn name(_: *Type) []const u8 {
return "goto"; return "goto";
} }
pub fn start(_: *Type) usize {
const editor = tui.get_active_editor() orelse return 1;
return editor.get_primary().cursor.row + 1;
}
pub const preview = goto; pub const preview = goto;
pub const apply = goto; pub const apply = goto;
pub const cancel = goto; pub const cancel = goto;
fn goto(self: *Type) void { fn goto(self: *Type, _: command.Context) void {
command.executeName("goto_line", command.fmt(.{self.input orelse self.start})) catch {}; command.executeName("goto_line", command.fmt(.{self.input orelse self.start})) catch {};
} }

View file

@ -22,16 +22,18 @@ pub fn Create(options: type) type {
buf: [30]u8 = undefined, buf: [30]u8 = undefined,
input: ?usize = null, input: ?usize = null,
start: usize, start: usize,
ctx: command.Context,
commands: Commands = undefined, commands: Commands = undefined,
pub fn create(allocator: Allocator, _: command.Context) !struct { tui.Mode, tui.MiniMode } { pub fn create(allocator: Allocator, ctx: command.Context) !struct { tui.Mode, tui.MiniMode } {
const editor = tui.get_active_editor() orelse return error.NotFound;
const self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self); errdefer allocator.destroy(self);
self.* = .{ self.* = .{
.allocator = allocator, .allocator = allocator,
.start = editor.get_primary().cursor.row + 1, .ctx = .{ .args = try ctx.args.clone(allocator) },
.start = 0,
}; };
self.start = options.start(self);
try self.commands.init(self); try self.commands.init(self);
var mode = try keybind.mode("mini/numeric", allocator, .{ var mode = try keybind.mode("mini/numeric", allocator, .{
.insert_command = "mini_mode_insert_bytes", .insert_command = "mini_mode_insert_bytes",
@ -41,6 +43,7 @@ pub fn Create(options: type) type {
} }
pub fn deinit(self: *Self) void { pub fn deinit(self: *Self) void {
self.allocator.free(self.ctx.args.buf);
self.commands.deinit(); self.commands.deinit();
self.allocator.destroy(self); self.allocator.destroy(self);
} }
@ -92,7 +95,7 @@ pub fn Create(options: type) type {
pub fn mini_mode_cancel(self: *Self, _: Ctx) Result { pub fn mini_mode_cancel(self: *Self, _: Ctx) Result {
self.input = null; self.input = null;
self.update_mini_mode_text(); self.update_mini_mode_text();
options.cancel(self); options.cancel(self, self.ctx);
command.executeName("exit_mini_mode", .{}) catch {}; command.executeName("exit_mini_mode", .{}) catch {};
} }
pub const mini_mode_cancel_meta: Meta = .{ .description = "Cancel input" }; pub const mini_mode_cancel_meta: Meta = .{ .description = "Cancel input" };
@ -102,7 +105,7 @@ pub fn Create(options: type) type {
const newval = if (linenum < 10) 0 else linenum / 10; const newval = if (linenum < 10) 0 else linenum / 10;
self.input = if (newval == 0) null else newval; self.input = if (newval == 0) null else newval;
self.update_mini_mode_text(); self.update_mini_mode_text();
options.preview(self); options.preview(self, self.ctx);
} }
} }
pub const mini_mode_delete_backwards_meta: Meta = .{ .description = "Delete backwards" }; pub const mini_mode_delete_backwards_meta: Meta = .{ .description = "Delete backwards" };
@ -116,7 +119,7 @@ pub fn Create(options: type) type {
else => {}, else => {},
} }
self.update_mini_mode_text(); self.update_mini_mode_text();
options.preview(self); options.preview(self, self.ctx);
} }
pub const mini_mode_insert_code_point_meta: Meta = .{ .arguments = &.{.integer} }; pub const mini_mode_insert_code_point_meta: Meta = .{ .arguments = &.{.integer} };
@ -126,7 +129,7 @@ pub fn Create(options: type) type {
return error.InvalidGotoInsertBytesArgument; return error.InvalidGotoInsertBytesArgument;
self.insert_bytes(bytes); self.insert_bytes(bytes);
self.update_mini_mode_text(); self.update_mini_mode_text();
options.preview(self); options.preview(self, self.ctx);
} }
pub const mini_mode_insert_bytes_meta: Meta = .{ .arguments = &.{.string} }; pub const mini_mode_insert_bytes_meta: Meta = .{ .arguments = &.{.string} };
@ -136,7 +139,7 @@ pub fn Create(options: type) type {
pub const mini_mode_paste_meta: Meta = .{ .arguments = &.{.string} }; pub const mini_mode_paste_meta: Meta = .{ .arguments = &.{.string} };
pub fn mini_mode_select(self: *Self, _: Ctx) Result { pub fn mini_mode_select(self: *Self, _: Ctx) Result {
options.apply(self); options.apply(self, self.ctx);
command.executeName("exit_mini_mode", .{}) catch {}; command.executeName("exit_mini_mode", .{}) catch {};
} }
pub const mini_mode_select_meta: Meta = .{ .description = "Select" }; pub const mini_mode_select_meta: Meta = .{ .description = "Select" };

View file

@ -1,5 +1,8 @@
const cbor = @import("cbor");
const command = @import("command"); const command = @import("command");
const tui = @import("../../tui.zig");
pub const Type = @import("numeric_input.zig").Create(@This()); pub const Type = @import("numeric_input.zig").Create(@This());
pub const create = Type.create; pub const create = Type.create;
@ -7,10 +10,23 @@ pub fn name(_: *Type) []const u8 {
return " tab size"; return " tab size";
} }
pub const preview = goto; pub fn start(self: *Type) usize {
pub const apply = goto; const tab_width = if (tui.get_active_editor()) |editor| editor.tab_width else tui.config().tab_width;
pub const cancel = goto; self.input = tab_width;
return tab_width;
fn goto(self: *Type) void { }
command.executeName("set_tab_width", command.fmt(.{self.input orelse self.start})) catch {};
const default_cmd = "set_editor_tab_width";
pub const cancel = preview;
pub fn preview(self: *Type, _: command.Context) void {
command.executeName(default_cmd, command.fmt(.{self.input orelse self.start})) catch {};
}
pub fn apply(self: *Type, ctx: command.Context) void {
var cmd: []const u8 = undefined;
if (!(ctx.args.match(.{cbor.extract(&cmd)}) catch false))
cmd = default_cmd;
command.executeName(cmd, command.fmt(.{self.input orelse self.start})) catch {};
} }