From 2a40bdabca96c7cc0a99b6c9c878da33d9fc7716 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 3 Mar 2023 09:01:13 -0800 Subject: [PATCH] macos: load config file default file locations --- include/ghostty.h | 2 ++ macos/Sources/GhosttyApp.swift | 4 ++++ src/config.zig | 20 +++++++++++++++++++- src/main.zig | 2 +- 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/include/ghostty.h b/include/ghostty.h index 6da1db805..3302db522 100644 --- a/include/ghostty.h +++ b/include/ghostty.h @@ -221,6 +221,8 @@ int ghostty_init(void); ghostty_config_t ghostty_config_new(); void ghostty_config_free(ghostty_config_t); void ghostty_config_load_string(ghostty_config_t, const char *, uintptr_t); +void ghostty_config_load_default_files(ghostty_config_t); +void ghostty_config_load_recursive_files(ghostty_config_t); void ghostty_config_finalize(ghostty_config_t); ghostty_app_t ghostty_app_new(const ghostty_runtime_config_s *, ghostty_config_t); diff --git a/macos/Sources/GhosttyApp.swift b/macos/Sources/GhosttyApp.swift index 3588f6031..ecb6c9906 100644 --- a/macos/Sources/GhosttyApp.swift +++ b/macos/Sources/GhosttyApp.swift @@ -89,6 +89,10 @@ class GhosttyState: ObservableObject { } self.config = cfg; + // Load our configuration files from the home directory. + ghostty_config_load_default_files(cfg); + ghostty_config_load_recursive_files(cfg); + // TODO: we'd probably do some config loading here... for now we'd // have to do this synchronously. When we support config updating we can do // this async and update later. diff --git a/src/config.zig b/src/config.zig index 6a9ab1a8b..97987020d 100644 --- a/src/config.zig +++ b/src/config.zig @@ -342,7 +342,7 @@ pub const Config = struct { } /// Load and parse the config files that were added in the "config-file" key. - pub fn loadRecursive(self: *Config, alloc: Allocator) !void { + pub fn loadRecursiveFiles(self: *Config, alloc: Allocator) !void { // TODO(mitchellh): we should parse the files form the homedir first // TODO(mitchellh): support nesting (config-file in a config file) // TODO(mitchellh): detect cycles when nesting @@ -708,6 +708,24 @@ pub const CAPI = struct { 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}); diff --git a/src/main.zig b/src/main.zig index 5a256857d..af92726d4 100644 --- a/src/main.zig +++ b/src/main.zig @@ -44,7 +44,7 @@ pub fn main() !void { } // Parse the config files that were added from our file and CLI args. - try config.loadRecursive(alloc); + try config.loadRecursiveFiles(alloc); try config.finalize(); //std.log.debug("config={}", .{config});