set default keybinds when parsing empty keybind config

This commit is contained in:
David Mo
2025-02-24 19:39:42 -05:00
parent 0321aec68f
commit 8fadb54e65
2 changed files with 24 additions and 12 deletions

View File

@ -247,16 +247,6 @@ pub fn parseIntoField(
inline for (info.Struct.fields) |field| { inline for (info.Struct.fields) |field| {
if (field.name[0] != '_' and mem.eql(u8, field.name, key)) { if (field.name[0] != '_' and mem.eql(u8, field.name, key)) {
// If the value is empty string (set but empty string),
// then we reset the value to the default.
if (value) |v| default: {
if (v.len != 0) break :default;
const raw = field.default_value orelse break :default;
const ptr: *const field.type = @alignCast(@ptrCast(raw));
@field(dst, field.name) = ptr.*;
return;
}
// For optional fields, we just treat it as the child type. // For optional fields, we just treat it as the child type.
// This lets optional fields default to null but get set by // This lets optional fields default to null but get set by
// the CLI. // the CLI.
@ -264,11 +254,27 @@ pub fn parseIntoField(
.Optional => |opt| opt.child, .Optional => |opt| opt.child,
else => field.type, else => field.type,
}; };
const fieldInfo = @typeInfo(Field);
const canHaveDecls = fieldInfo == .Struct or fieldInfo == .Union or fieldInfo == .Enum;
// If the value is empty string (set but empty string),
// then we reset the value to the default.
if (value) |v| default: {
if (v.len != 0) break :default;
// Set default value if possible.
if (canHaveDecls and @hasDecl(Field, "setToDefault")) {
try @field(dst, field.name).setToDefault(alloc);
return;
}
const raw = field.default_value orelse break :default;
const ptr: *const field.type = @alignCast(@ptrCast(raw));
@field(dst, field.name) = ptr.*;
return;
}
// If we are a type that can have decls and have a parseCLI decl, // If we are a type that can have decls and have a parseCLI decl,
// we call that and use that to set the value. // we call that and use that to set the value.
const fieldInfo = @typeInfo(Field); if (canHaveDecls) {
if (fieldInfo == .Struct or fieldInfo == .Union or fieldInfo == .Enum) {
if (@hasDecl(Field, "parseCLI")) { if (@hasDecl(Field, "parseCLI")) {
const fnInfo = @typeInfo(@TypeOf(Field.parseCLI)).Fn; const fnInfo = @typeInfo(@TypeOf(Field.parseCLI)).Fn;
switch (fnInfo.params.len) { switch (fnInfo.params.len) {

View File

@ -5129,6 +5129,12 @@ pub const Keybinds = struct {
errdefer if (copy) |v| alloc.free(v); errdefer if (copy) |v| alloc.free(v);
// Check for special values // Check for special values
if (value.len == 0) {
log.info("config has 'keybind =', using default keybinds", .{});
try self.setToDefault(alloc);
return;
}
if (std.mem.eql(u8, value, "clear")) { if (std.mem.eql(u8, value, "clear")) {
// We don't clear the memory because its in the arena and unlikely // We don't clear the memory because its in the arena and unlikely
// to be free-able anyways (since arenas can only clear the last // to be free-able anyways (since arenas can only clear the last