Skip to content

Commit

Permalink
config: use the standard 'packed struct' behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
jparise committed May 30, 2024
1 parent f4e23f5 commit fbabc96
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 62 deletions.
2 changes: 1 addition & 1 deletion src/build/fish_completions.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub const fish_completions = comptimeGenerateFishCompletions();

fn comptimeGenerateFishCompletions() []const u8 {
comptime {
@setEvalBranchQuota(13000);
@setEvalBranchQuota(14000);
var counter = std.io.countingWriter(std.io.null_writer);
try writeFishCompletions(&counter.writer());

Expand Down
68 changes: 7 additions & 61 deletions src/config/Config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -642,9 +642,12 @@ keybind: Keybinds = .{},
/// focused, the default working directory will be used (the `working-directory`
/// option).
///
/// Valid values are `split`, `tab`, and `window`. You can specify multiple
/// values using a comma-delimited string (`tab` or `split,window`). You
/// can also set this to `true` (always inherit) or `false` (never inherit).
/// The format of this is a list of values to enable separated by commas. If
/// you prefix a value with `no-` then it is disabled. If you omit a feature,
/// its default value is used, so you must explicitly disable features you don't
/// want.
///
/// Valid values are `split`, `tab`, and `window`.
///
/// `split`, `tab`, and `window` are currently only supported on macOS.
@"window-inherit-working-directory": WindowInheritWorkingDirectory = .{},
Expand Down Expand Up @@ -3496,68 +3499,11 @@ pub const WindowNewTabPosition = enum {
end,
};

/// Options for inheriting the working directory of the previous window.
/// See window-inherit-working-directory
pub const WindowInheritWorkingDirectory = packed struct {
const Self = @This();

split: bool = true,
tab: bool = true,
window: bool = true,

pub fn parseCLI(self: *Self, _: Allocator, input: ?[]const u8) !void {
const value = input orelse return error.ValueRequired;
const fields = @typeInfo(Self).Struct.fields;

if (std.mem.eql(u8, value, "true")) {
self.* = .{ .split = true, .tab = true, .window = true };
return;
}

if (std.mem.eql(u8, value, "false")) {
self.* = .{ .split = false, .tab = false, .window = false };
return;
}

// Enable all of the fields named in the comma-separated value.
self.* = .{ .split = false, .tab = false, .window = false };
var iter = std.mem.splitSequence(u8, value, ",");
loop: while (iter.next()) |part_raw| {
const part = std.mem.trim(u8, part_raw, whitespace);

inline for (fields) |field| {
assert(field.type == bool);
if (std.mem.eql(u8, field.name, part)) {
@field(self, field.name) = true;
continue :loop;
}
}

// No field matched
return error.InvalidValue;
}
}

test "parseCLI" {
const testing = std.testing;
var arena = ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();

var p: Self = .{};
try p.parseCLI(alloc, "true");
try testing.expectEqual(Self{ .split = true, .tab = true, .window = true }, p);

try p.parseCLI(alloc, "false");
try testing.expectEqual(Self{ .split = false, .tab = false, .window = false }, p);

try p.parseCLI(alloc, "tab");
try testing.expectEqual(Self{ .split = false, .tab = true, .window = false }, p);

try p.parseCLI(alloc, "split,window");
try testing.expectEqual(Self{ .split = true, .tab = false, .window = true }, p);

try testing.expectError(error.InvalidValue, p.parseCLI(alloc, "unknown"));
}
};

/// See grapheme-width-method
Expand Down

0 comments on commit fbabc96

Please sign in to comment.