From 8ce6f349f882c9ef86b674214f09a9be395af504 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 8 Mar 2023 08:56:17 -0800 Subject: [PATCH] input: new_split binding, can parse enums --- src/Surface.zig | 6 ++++++ src/config.zig | 10 +++++++++ src/input/Binding.zig | 47 +++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/Surface.zig b/src/Surface.zig index 211a3cedc..b2a7a0998 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -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 = {} }); }, diff --git a/src/config.zig b/src/config.zig index 06adb6910..0e8762bad 100644 --- a/src/config.zig +++ b/src/config.zig @@ -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); diff --git a/src/input/Binding.zig b/src/input/Binding.zig index a788d0c5e..2fb9f0534 100644 --- a/src/input/Binding.zig +++ b/src/input/Binding.zig @@ -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); + } +}