refactor: move_to_char to use new get_char mini mode
This commit is contained in:
parent
28688b49de
commit
b5e591c172
1 changed files with 22 additions and 75 deletions
|
|
@ -1,25 +1,17 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const tp = @import("thespian");
|
const cbor = @import("cbor");
|
||||||
|
|
||||||
const input = @import("input");
|
|
||||||
const keybind = @import("keybind");
|
|
||||||
const command = @import("command");
|
const command = @import("command");
|
||||||
const EventHandler = @import("EventHandler");
|
|
||||||
|
|
||||||
const tui = @import("../../tui.zig");
|
const tui = @import("../../tui.zig");
|
||||||
|
|
||||||
const Allocator = @import("std").mem.Allocator;
|
pub const Type = @import("get_char.zig").Create(@This());
|
||||||
|
pub const create = Type.create;
|
||||||
|
|
||||||
const Self = @This();
|
pub const ValueType = struct {
|
||||||
|
direction: Direction,
|
||||||
const Commands = command.Collection(cmds);
|
operation_command: []const u8,
|
||||||
|
operation: Operation,
|
||||||
allocator: Allocator,
|
};
|
||||||
key: [6]u8 = undefined,
|
|
||||||
direction: Direction,
|
|
||||||
operation_command: []const u8,
|
|
||||||
operation: Operation,
|
|
||||||
commands: Commands = undefined,
|
|
||||||
|
|
||||||
const Direction = enum {
|
const Direction = enum {
|
||||||
left,
|
left,
|
||||||
|
|
@ -32,9 +24,9 @@ const Operation = enum {
|
||||||
extend,
|
extend,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn create(allocator: Allocator, ctx: command.Context) !struct { tui.Mode, tui.MiniMode } {
|
pub fn start(self: *Type) ValueType {
|
||||||
var operation_command: []const u8 = undefined;
|
var operation_command: []const u8 = "move_to_char_left";
|
||||||
_ = ctx.args.match(.{tp.extract(&operation_command)}) catch return error.InvalidMoveToCharArgument;
|
_ = self.ctx.args.match(.{cbor.extract(&operation_command)}) catch {};
|
||||||
|
|
||||||
const direction: Direction = if (std.mem.indexOf(u8, operation_command, "_left")) |_| .left else .right;
|
const direction: Direction = if (std.mem.indexOf(u8, operation_command, "_left")) |_| .left else .right;
|
||||||
var operation: Operation = undefined;
|
var operation: Operation = undefined;
|
||||||
|
|
@ -50,80 +42,35 @@ pub fn create(allocator: Allocator, ctx: command.Context) !struct { tui.Mode, tu
|
||||||
operation = .move;
|
operation = .move;
|
||||||
}
|
}
|
||||||
|
|
||||||
const self = try allocator.create(Self);
|
return .{
|
||||||
errdefer allocator.destroy(self);
|
|
||||||
self.* = .{
|
|
||||||
.allocator = allocator,
|
|
||||||
.direction = direction,
|
.direction = direction,
|
||||||
.operation_command = try allocator.dupe(u8, operation_command),
|
.operation_command = self.allocator.dupe(u8, operation_command) catch @panic("OOM in move_to_char"),
|
||||||
.operation = operation,
|
.operation = operation,
|
||||||
};
|
};
|
||||||
try self.commands.init(self);
|
|
||||||
var mode = try keybind.mode("mini/move_to_char", allocator, .{
|
|
||||||
.insert_command = "mini_mode_insert_bytes",
|
|
||||||
});
|
|
||||||
mode.event_handler = EventHandler.to_owned(self);
|
|
||||||
return .{ mode, .{ .name = self.name() } };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *Self) void {
|
pub fn deinit(self: *Type) void {
|
||||||
self.commands.deinit();
|
self.allocator.free(self.value.operation_command);
|
||||||
self.allocator.free(self.operation_command);
|
|
||||||
self.allocator.destroy(self);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn name(self: *Self) []const u8 {
|
pub fn name(self: *Type) []const u8 {
|
||||||
return switch (self.operation) {
|
return switch (self.value.operation) {
|
||||||
.move => switch (self.direction) {
|
.move => switch (self.value.direction) {
|
||||||
.left => "↶ move",
|
.left => "↶ move",
|
||||||
.right => "↷ move",
|
.right => "↷ move",
|
||||||
},
|
},
|
||||||
.select => switch (self.direction) {
|
.select => switch (self.value.direction) {
|
||||||
.left => " ↶ select",
|
.left => " ↶ select",
|
||||||
.right => " ↷ select",
|
.right => " ↷ select",
|
||||||
},
|
},
|
||||||
.extend => switch (self.direction) {
|
.extend => switch (self.value.direction) {
|
||||||
.left => " ↶ extend",
|
.left => " ↶ extend",
|
||||||
.right => " ↷ extend",
|
.right => " ↷ extend",
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn receive(_: *Self, _: tp.pid_ref, _: tp.message) error{Exit}!bool {
|
pub fn process_egc(self: *Type, egc: []const u8) command.Result {
|
||||||
return false;
|
try command.executeName(self.value.operation_command, command.fmt(.{egc}));
|
||||||
}
|
|
||||||
|
|
||||||
fn execute_operation(self: *Self, ctx: command.Context) command.Result {
|
|
||||||
try command.executeName(self.operation_command, ctx);
|
|
||||||
try command.executeName("exit_mini_mode", .{});
|
try command.executeName("exit_mini_mode", .{});
|
||||||
}
|
}
|
||||||
|
|
||||||
const cmds = struct {
|
|
||||||
pub const Target = Self;
|
|
||||||
const Ctx = command.Context;
|
|
||||||
const Meta = command.Metadata;
|
|
||||||
const Result = command.Result;
|
|
||||||
|
|
||||||
pub fn mini_mode_insert_code_point(self: *Self, ctx: Ctx) Result {
|
|
||||||
var code_point: u32 = 0;
|
|
||||||
if (!try ctx.args.match(.{tp.extract(&code_point)}))
|
|
||||||
return error.InvalidMoveToCharInsertCodePointArgument;
|
|
||||||
var buf: [6]u8 = undefined;
|
|
||||||
const bytes = input.ucs32_to_utf8(&[_]u32{code_point}, &buf) catch return error.InvalidMoveToCharCodePoint;
|
|
||||||
return self.execute_operation(command.fmt(.{buf[0..bytes]}));
|
|
||||||
}
|
|
||||||
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.InvalidMoveToCharInsertBytesArgument;
|
|
||||||
return self.execute_operation(ctx);
|
|
||||||
}
|
|
||||||
pub const mini_mode_insert_bytes_meta: Meta = .{ .arguments = &.{.string} };
|
|
||||||
|
|
||||||
pub fn mini_mode_cancel(_: *Self, _: Ctx) Result {
|
|
||||||
command.executeName("exit_mini_mode", .{}) catch {};
|
|
||||||
}
|
|
||||||
pub const mini_mode_cancel_meta: Meta = .{ .description = "Cancel input" };
|
|
||||||
};
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue