config: extract into dedicated dir, split into files

This commit is contained in:
Mitchell Hashimoto
2023-09-10 16:17:19 -07:00
parent 8bec01f237
commit b14ba8c022
5 changed files with 1659 additions and 1624 deletions

File diff suppressed because it is too large Load Diff

98
src/config/CAPI.zig Normal file
View File

@ -0,0 +1,98 @@
const std = @import("std");
const cli_args = @import("../cli_args.zig");
const inputpkg = @import("../input.zig");
const global = &@import("../main.zig").state;
const Config = @import("Config.zig");
const log = std.log.scoped(.config);
/// Create a new configuration filled with the initial default values.
export fn ghostty_config_new() ?*Config {
const result = global.alloc.create(Config) catch |err| {
log.err("error allocating config err={}", .{err});
return null;
};
result.* = Config.default(global.alloc) catch |err| {
log.err("error creating config err={}", .{err});
return null;
};
return result;
}
export fn ghostty_config_free(ptr: ?*Config) void {
if (ptr) |v| {
v.deinit();
global.alloc.destroy(v);
}
}
/// Load the configuration from the CLI args.
export fn ghostty_config_load_cli_args(self: *Config) void {
self.loadCliArgs(global.alloc) catch |err| {
log.err("error loading config err={}", .{err});
};
}
/// Load the configuration from a string in the same format as
/// the file-based syntax for the desktop version of the terminal.
export fn ghostty_config_load_string(
self: *Config,
str: [*]const u8,
len: usize,
) void {
config_load_string_(self, str[0..len]) catch |err| {
log.err("error loading config err={}", .{err});
};
}
fn config_load_string_(self: *Config, str: []const u8) !void {
var fbs = std.io.fixedBufferStream(str);
var iter = cli_args.lineIterator(fbs.reader());
try cli_args.parse(Config, global.alloc, self, &iter);
}
/// Load the configuration from the default file locations. This
/// is usually done first. The default file locations are locations
/// such as the home directory.
export fn ghostty_config_load_default_files(self: *Config) void {
self.loadDefaultFiles(global.alloc) catch |err| {
log.err("error loading config err={}", .{err});
};
}
/// Load the configuration from the user-specified configuration
/// file locations in the previously loaded configuration. This will
/// recursively continue to load up to a built-in limit.
export fn ghostty_config_load_recursive_files(self: *Config) void {
self.loadRecursiveFiles(global.alloc) catch |err| {
log.err("error loading config err={}", .{err});
};
}
export fn ghostty_config_finalize(self: *Config) void {
self.finalize() catch |err| {
log.err("error finalizing config err={}", .{err});
};
}
export fn ghostty_config_trigger(
self: *Config,
str: [*]const u8,
len: usize,
) inputpkg.Binding.Trigger {
return config_trigger_(self, str[0..len]) catch |err| err: {
log.err("error finding trigger err={}", .{err});
break :err .{};
};
}
fn config_trigger_(
self: *Config,
str: []const u8,
) !inputpkg.Binding.Trigger {
const action = try inputpkg.Binding.Action.parse(str);
return self.keybind.set.getTrigger(action) orelse .{};
}

1462
src/config/Config.zig Normal file

File diff suppressed because it is too large Load Diff

54
src/config/Wasm.zig Normal file
View File

@ -0,0 +1,54 @@
const std = @import("std");
const wasm = @import("../os/wasm.zig");
const cli_args = @import("../cli_args.zig");
const alloc = wasm.alloc;
const Config = @import("Config.zig");
const log = std.log.scoped(.config);
/// Create a new configuration filled with the initial default values.
export fn config_new() ?*Config {
const result = alloc.create(Config) catch |err| {
log.err("error allocating config err={}", .{err});
return null;
};
result.* = Config.default(alloc) catch |err| {
log.err("error creating config err={}", .{err});
return null;
};
return result;
}
export fn config_free(ptr: ?*Config) void {
if (ptr) |v| {
v.deinit();
alloc.destroy(v);
}
}
/// Load the configuration from a string in the same format as
/// the file-based syntax for the desktop version of the terminal.
export fn config_load_string(
self: *Config,
str: [*]const u8,
len: usize,
) void {
config_load_string_(self, str[0..len]) catch |err| {
log.err("error loading config err={}", .{err});
};
}
fn config_load_string_(self: *Config, str: []const u8) !void {
var fbs = std.io.fixedBufferStream(str);
var iter = cli_args.lineIterator(fbs.reader());
try cli_args.parse(Config, alloc, self, &iter);
}
export fn config_finalize(self: *Config) void {
self.finalize() catch |err| {
log.err("error finalizing config err={}", .{err});
};
}

32
src/config/key.zig Normal file
View File

@ -0,0 +1,32 @@
const std = @import("std");
const Config = @import("Config.zig");
/// Key is an enum of all the available configuration keys. This is used
/// when paired with diff to determine what fields have changed in a config,
/// amongst other things.
pub const Key = key: {
const field_infos = std.meta.fields(Config);
var enumFields: [field_infos.len]std.builtin.Type.EnumField = undefined;
var i: usize = 0;
inline for (field_infos) |field| {
// Ignore fields starting with "_" since they're internal and
// not copied ever.
if (field.name[0] == '_') continue;
enumFields[i] = .{
.name = field.name,
.value = i,
};
i += 1;
}
var decls = [_]std.builtin.Type.Declaration{};
break :key @Type(.{
.Enum = .{
.tag_type = std.math.IntFittingRange(0, field_infos.len - 1),
.fields = enumFields[0..i],
.decls = &decls,
.is_exhaustive = true,
},
});
};