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 Jun 1, 2024
1 parent af8194a commit b6215ee
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 66 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
75 changes: 10 additions & 65 deletions src/config/Config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ const help_strings = @import("help_strings");

const log = std.log.scoped(.config);

// For trimming
const whitespace = " \t";

/// Used on Unixes for some defaults.
const c = @cImport({
@cInclude("unistd.h");
Expand Down Expand Up @@ -642,11 +639,14 @@ 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.
///
/// `split`, `tab`, and `window` are currently only supported on macOS.
/// Valid values are `split`, `tab`, and `window`.
///
/// `split` and `tab` are currently only supported on macOS.
@"window-inherit-working-directory": WindowInheritWorkingDirectory = .{},

/// If true, new windows and tabs will inherit the font size of the previously
Expand Down Expand Up @@ -2721,6 +2721,7 @@ pub const RepeatableFontVariation = struct {
pub fn parseCLI(self: *Self, alloc: Allocator, input_: ?[]const u8) !void {
const input = input_ orelse return error.ValueRequired;
const eql_idx = std.mem.indexOf(u8, input, "=") orelse return error.InvalidValue;
const whitespace = " \t";
const key = std.mem.trim(u8, input[0..eql_idx], whitespace);
const value = std.mem.trim(u8, input[eql_idx + 1 ..], whitespace);
if (key.len != 4) return error.InvalidValue;
Expand Down Expand Up @@ -2983,6 +2984,7 @@ pub const RepeatableCodepointMap = struct {
pub fn parseCLI(self: *Self, alloc: Allocator, input_: ?[]const u8) !void {
const input = input_ orelse return error.ValueRequired;
const eql_idx = std.mem.indexOf(u8, input, "=") orelse return error.InvalidValue;
const whitespace = " \t";
const key = std.mem.trim(u8, input[0..eql_idx], whitespace);
const value = std.mem.trim(u8, input[eql_idx + 1 ..], whitespace);
const valueZ = try alloc.dupeZ(u8, value);
Expand Down Expand Up @@ -3496,68 +3498,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 b6215ee

Please sign in to comment.