feat: add goto_offset mini mode and command
This commit is contained in:
parent
18cd62ba7e
commit
30a457158c
2 changed files with 63 additions and 0 deletions
58
src/tui/mode/mini/goto_offset.zig
Normal file
58
src/tui/mode/mini/goto_offset.zig
Normal file
|
@ -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 {};
|
||||||
|
}
|
|
@ -1038,6 +1038,11 @@ const cmds = struct {
|
||||||
}
|
}
|
||||||
pub const goto_meta: Meta = .{ .description = "Goto line" };
|
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 {
|
pub fn move_to_char(self: *Self, ctx: Ctx) Result {
|
||||||
return enter_mini_mode(self, @import("mode/mini/move_to_char.zig"), ctx);
|
return enter_mini_mode(self, @import("mode/mini/move_to_char.zig"), ctx);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue