input: new_split binding, can parse enums

This commit is contained in:
Mitchell Hashimoto
2023-03-08 08:56:17 -08:00
parent 15b7e7fcd7
commit 8ce6f349f8
3 changed files with 61 additions and 2 deletions

View File

@ -940,6 +940,12 @@ pub fn keyCallback(
} else log.warn("runtime doesn't implement gotoTab", .{});
},
.new_split => |direction| {
if (@hasDecl(apprt.Surface, "newSplit")) {
try self.rt_surface.newSplit(direction);
} else log.warn("runtime doesn't implement newSplit", .{});
},
.close_window => {
_ = self.app_mailbox.push(.{ .close = self }, .{ .instant = {} });
},

View File

@ -296,6 +296,16 @@ pub const Config = struct {
.{ .key = .right_bracket, .mods = .{ .super = true, .shift = true } },
.{ .next_tab = {} },
);
try result.keybind.set.put(
alloc,
.{ .key = .d, .mods = .{ .super = true } },
.{ .new_split = .right },
);
try result.keybind.set.put(
alloc,
.{ .key = .d, .mods = .{ .super = true, .shift = true } },
.{ .new_split = .down },
);
{
// Cmd+N for goto tab N
const start = @enumToInt(inputpkg.Key.one);

View File

@ -105,7 +105,20 @@ pub fn parse(input: []const u8) !Binding {
// Cursor keys can't be set currently
Action.CursorKey => return Error.InvalidAction,
else => unreachable,
else => switch (@typeInfo(field.type)) {
.Enum => {
const idx = colonIdx orelse return Error.InvalidFormat;
const param = actionRaw[idx + 1 ..];
const value = std.meta.stringToEnum(
field.type,
param,
) orelse return Error.InvalidFormat;
break :action @unionInit(Action, field.name, value);
},
else => unreachable,
},
}
}
}
@ -167,6 +180,10 @@ pub const Action = union(enum) {
/// Go to the tab with the specific number, 1-indexed.
goto_tab: usize,
/// Create a new split in the given direction. The new split will appear
/// in the direction given.
new_split: SplitDirection,
/// Close the current window or tab
close_window: void,
@ -177,6 +194,13 @@ pub const Action = union(enum) {
normal: []const u8,
application: []const u8,
};
pub const SplitDirection = enum {
right,
down,
// Note: we don't support top or left yet
};
};
/// Trigger is the associated key state that can trigger an action.
@ -286,11 +310,15 @@ test "parse: triggers" {
try testing.expectError(Error.InvalidFormat, parse("a+b=ignore"));
}
test "parse: action" {
test "parse: action invalid" {
const testing = std.testing;
// invalid action
try testing.expectError(Error.InvalidFormat, parse("a=nopenopenope"));
}
test "parse: action no parameters" {
const testing = std.testing;
// no parameters
try testing.expectEqual(
@ -298,6 +326,10 @@ test "parse: action" {
try parse("a=ignore"),
);
try testing.expectError(Error.InvalidFormat, parse("a=ignore:A"));
}
test "parse: action with string" {
const testing = std.testing;
// parameter
{
@ -306,3 +338,14 @@ test "parse: action" {
try testing.expectEqualStrings("A", binding.action.csi);
}
}
test "parse: action with enum" {
const testing = std.testing;
// parameter
{
const binding = try parse("a=new_split:right");
try testing.expect(binding.action == .new_split);
try testing.expectEqual(Action.SplitDirection.right, binding.action.new_split);
}
}