refactor: goto minimode into a reusable numeric input minimode

This commit is contained in:
CJ van den Berg 2025-08-11 14:07:11 +02:00
parent e886b7064a
commit 1632061144
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
3 changed files with 155 additions and 134 deletions

View file

@ -328,7 +328,7 @@
["right_control", "palette_menu_activate_quick"]
]
},
"mini/goto": {
"mini/numeric": {
"press": [
["ctrl+q", "quit"],
["ctrl+v", "system_paste"],
@ -338,7 +338,7 @@
["ctrl+l", "scroll_view_center_cycle"],
["ctrl+space", "mini_mode_cancel"],
["escape", "mini_mode_cancel"],
["enter", "exit_mini_mode"],
["enter", "mini_mode_select"],
["backspace", "mini_mode_delete_backwards"]
]
},

View file

@ -1,140 +1,16 @@
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 keybind = @import("keybind");
const command = @import("command");
const EventHandler = @import("EventHandler");
const tui = @import("../../tui.zig");
pub const Type = @import("numeric_input.zig").Create(@This());
pub const create = Type.create;
const Allocator = @import("std").mem.Allocator;
const fmt = @import("std").fmt;
const Self = @This();
const name = "goto";
const Commands = command.Collection(cmds);
allocator: Allocator,
buf: [30]u8 = undefined,
input: ?usize = null,
start: usize,
commands: Commands = undefined,
pub fn create(allocator: Allocator, _: command.Context) !struct { tui.Mode, tui.MiniMode } {
const editor = tui.get_active_editor() orelse return error.NotFound;
const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = .{
.allocator = allocator,
.start = editor.get_primary().cursor.row + 1,
};
try self.commands.init(self);
var mode = try keybind.mode("mini/goto", allocator, .{
.insert_command = "mini_mode_insert_bytes",
});
mode.event_handler = EventHandler.to_owned(self);
return .{ mode, .{ .name = name } };
pub fn name(_: *Type) []const u8 {
return "goto";
}
pub fn deinit(self: *Self) void {
self.commands.deinit();
self.allocator.destroy(self);
}
pub const preview = goto;
pub const apply = goto;
pub const cancel = goto;
pub fn receive(self: *Self, _: tp.pid_ref, _: tp.message) error{Exit}!bool {
self.update_mini_mode_text();
return false;
}
fn update_mini_mode_text(self: *Self) void {
if (tui.mini_mode()) |mini_mode| {
mini_mode.text = if (self.input) |linenum|
(fmt.bufPrint(&self.buf, "{d}", .{linenum}) catch "")
else
"";
mini_mode.cursor = tui.egc_chunk_width(mini_mode.text, 0, 1);
}
}
fn goto(self: *Self) void {
fn goto(self: *Type) 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;
const Meta = command.Metadata;
const Result = command.Result;
pub fn mini_mode_reset(self: *Self, _: Ctx) Result {
self.input = null;
self.update_mini_mode_text();
}
pub const mini_mode_reset_meta: Meta = .{ .description = "Clear input" };
pub fn mini_mode_cancel(self: *Self, _: Ctx) Result {
self.input = null;
self.update_mini_mode_text();
self.goto();
command.executeName("exit_mini_mode", .{}) catch {};
}
pub const mini_mode_cancel_meta: Meta = .{ .description = "Cancel input" };
pub fn mini_mode_delete_backwards(self: *Self, _: Ctx) Result {
if (self.input) |linenum| {
const newval = if (linenum < 10) 0 else linenum / 10;
self.input = if (newval == 0) null else newval;
self.update_mini_mode_text();
self.goto();
}
}
pub const mini_mode_delete_backwards_meta: Meta = .{ .description = "Delete backwards" };
pub fn mini_mode_insert_code_point(self: *Self, ctx: Ctx) Result {
var keypress: usize = 0;
if (!try ctx.args.match(.{tp.extract(&keypress)}))
return error.InvalidGotoInsertCodePointArgument;
switch (keypress) {
'0'...'9' => self.insert_char(@intCast(keypress)),
else => {},
}
self.update_mini_mode_text();
self.goto();
}
pub const mini_mode_insert_code_point_meta: Meta = .{ .arguments = &.{.integer} };
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.InvalidGotoInsertBytesArgument;
self.insert_bytes(bytes);
self.update_mini_mode_text();
self.goto();
}
pub const mini_mode_insert_bytes_meta: Meta = .{ .arguments = &.{.string} };
pub fn mini_mode_paste(self: *Self, ctx: Ctx) Result {
return mini_mode_insert_bytes(self, ctx);
}
pub const mini_mode_paste_meta: Meta = .{ .arguments = &.{.string} };
};

View file

@ -0,0 +1,145 @@
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 keybind = @import("keybind");
const command = @import("command");
const EventHandler = @import("EventHandler");
const tui = @import("../../tui.zig");
const Allocator = @import("std").mem.Allocator;
const fmt = @import("std").fmt;
pub fn Create(options: type) type {
return struct {
const Self = @This();
const Commands = command.Collection(cmds);
allocator: Allocator,
buf: [30]u8 = undefined,
input: ?usize = null,
start: usize,
commands: Commands = undefined,
pub fn create(allocator: Allocator, _: command.Context) !struct { tui.Mode, tui.MiniMode } {
const editor = tui.get_active_editor() orelse return error.NotFound;
const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = .{
.allocator = allocator,
.start = editor.get_primary().cursor.row + 1,
};
try self.commands.init(self);
var mode = try keybind.mode("mini/numeric", allocator, .{
.insert_command = "mini_mode_insert_bytes",
});
mode.event_handler = EventHandler.to_owned(self);
return .{ mode, .{ .name = options.name(self) } };
}
pub fn deinit(self: *Self) void {
self.commands.deinit();
self.allocator.destroy(self);
}
pub fn receive(self: *Self, _: tp.pid_ref, _: tp.message) error{Exit}!bool {
self.update_mini_mode_text();
return false;
}
fn update_mini_mode_text(self: *Self) void {
if (tui.mini_mode()) |mini_mode| {
mini_mode.text = if (self.input) |linenum|
(fmt.bufPrint(&self.buf, "{d}", .{linenum}) catch "")
else
"";
mini_mode.cursor = tui.egc_chunk_width(mini_mode.text, 0, 1);
}
}
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;
const Meta = command.Metadata;
const Result = command.Result;
pub fn mini_mode_reset(self: *Self, _: Ctx) Result {
self.input = null;
self.update_mini_mode_text();
}
pub const mini_mode_reset_meta: Meta = .{ .description = "Clear input" };
pub fn mini_mode_cancel(self: *Self, _: Ctx) Result {
self.input = null;
self.update_mini_mode_text();
options.cancel(self);
command.executeName("exit_mini_mode", .{}) catch {};
}
pub const mini_mode_cancel_meta: Meta = .{ .description = "Cancel input" };
pub fn mini_mode_delete_backwards(self: *Self, _: Ctx) Result {
if (self.input) |linenum| {
const newval = if (linenum < 10) 0 else linenum / 10;
self.input = if (newval == 0) null else newval;
self.update_mini_mode_text();
options.preview(self);
}
}
pub const mini_mode_delete_backwards_meta: Meta = .{ .description = "Delete backwards" };
pub fn mini_mode_insert_code_point(self: *Self, ctx: Ctx) Result {
var keypress: usize = 0;
if (!try ctx.args.match(.{tp.extract(&keypress)}))
return error.InvalidGotoInsertCodePointArgument;
switch (keypress) {
'0'...'9' => self.insert_char(@intCast(keypress)),
else => {},
}
self.update_mini_mode_text();
options.preview(self);
}
pub const mini_mode_insert_code_point_meta: Meta = .{ .arguments = &.{.integer} };
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.InvalidGotoInsertBytesArgument;
self.insert_bytes(bytes);
self.update_mini_mode_text();
options.preview(self);
}
pub const mini_mode_insert_bytes_meta: Meta = .{ .arguments = &.{.string} };
pub fn mini_mode_paste(self: *Self, ctx: Ctx) Result {
return mini_mode_insert_bytes(self, ctx);
}
pub const mini_mode_paste_meta: Meta = .{ .arguments = &.{.string} };
pub fn mini_mode_select(self: *Self, _: Ctx) Result {
options.apply(self);
command.executeName("exit_mini_mode", .{}) catch {};
}
pub const mini_mode_select_meta: Meta = .{ .description = "Select" };
};
};
}