Skip to content

Commit

Permalink
Add "top" and "bottom" aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
dpatterbee committed Dec 29, 2024
1 parent 8cbf8d5 commit 3e11476
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion src/input/Binding.zig
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,38 @@ pub const Action = union(enum) {
left,
down,
right,

pub fn parse(input: []const u8) !SplitFocusDirection {
return std.meta.stringToEnum(SplitFocusDirection, input) orelse {
// For backwards compatibility we map "top" and "bottom" onto the enum
// values "up" and "down"
if (std.mem.eql(u8, input, "top")) {
return .up;
} else if (std.mem.eql(u8, input, "bottom")) {
return .down;
} else {
return Error.InvalidFormat;
}
};
}

test "parse" {
const testing = std.testing;

try testing.expectEqual(.previous, try SplitFocusDirection.parse("previous"));
try testing.expectEqual(.next, try SplitFocusDirection.parse("next"));

try testing.expectEqual(.up, try SplitFocusDirection.parse("up"));
try testing.expectEqual(.left, try SplitFocusDirection.parse("left"));
try testing.expectEqual(.down, try SplitFocusDirection.parse("down"));
try testing.expectEqual(.right, try SplitFocusDirection.parse("right"));

try testing.expectEqual(.up, try SplitFocusDirection.parse("top"));
try testing.expectEqual(.down, try SplitFocusDirection.parse("bottom"));

try testing.expectError(error.InvalidFormat, SplitFocusDirection.parse(""));
try testing.expectError(error.InvalidFormat, SplitFocusDirection.parse("green"));
}
};

pub const SplitResizeDirection = enum {
Expand Down Expand Up @@ -515,7 +547,16 @@ pub const Action = union(enum) {
comptime field: std.builtin.Type.UnionField,
param: []const u8,
) !field.type {
return switch (@typeInfo(field.type)) {
const field_info = @typeInfo(field.type);

// Fields can provide a custom "parse" function
if (field_info == .Struct or field_info == .Union or field_info == .Enum) {
if (@hasDecl(field.type, "parse") and @typeInfo(@TypeOf(field.type.parse)) == .Fn) {
return field.type.parse(param);
}
}

return switch (field_info) {
.Enum => try parseEnum(field.type, param),
.Int => try parseInt(field.type, param),
.Float => try parseFloat(field.type, param),
Expand Down

0 comments on commit 3e11476

Please sign in to comment.