From ecd8da89813e7f47dbae9e93ac8429725efb67c7 Mon Sep 17 00:00:00 2001 From: Nikita Bobko Date: Thu, 14 Sep 2023 22:54:11 +0200 Subject: [PATCH] Flatten Config into ConfigRoot --- config-examples/default-config.toml | 1 - src/config/ConfigModel.swift | 6 +-- src/config/defaultConfig.swift | 12 ++--- src/config/parseConfig.swift | 83 ++++++++++++----------------- 4 files changed, 40 insertions(+), 62 deletions(-) diff --git a/config-examples/default-config.toml b/config-examples/default-config.toml index 1ac994ff..4377a3a2 100644 --- a/config-examples/default-config.toml +++ b/config-examples/default-config.toml @@ -1,6 +1,5 @@ # For the whole reference of possible keys and modifiers see: https://github.com/nikitabobko/AeroSpace/blob/main/src/config/keysMap.swift -[config] use-padding-for-nested-containers-with-the-same-orientation = true auto-flatten-containers = true # auto-different-orientation-for-nested-containers = true diff --git a/src/config/ConfigModel.swift b/src/config/ConfigModel.swift index 0743f65c..33e7d37e 100644 --- a/src/config/ConfigModel.swift +++ b/src/config/ConfigModel.swift @@ -1,15 +1,11 @@ import HotKey -struct ConfigRoot { - let config: Config - let modes: [String: Mode] -} - struct Config { let afterStartupCommand: Command let usePaddingForNestedContainersWithTheSameOrientation: Bool let autoFlattenContainers: Bool let floatingWindowsOnTop: Bool + let modes: [String: Mode] } struct Mode { diff --git a/src/config/defaultConfig.swift b/src/config/defaultConfig.swift index 6742a162..e8512c7f 100644 --- a/src/config/defaultConfig.swift +++ b/src/config/defaultConfig.swift @@ -1,10 +1,8 @@ -let defaultConfig = ConfigRoot( - config: Config( - afterStartupCommand: NoOpCommand.instance, - usePaddingForNestedContainersWithTheSameOrientation: false, - autoFlattenContainers: true, - floatingWindowsOnTop: true - ), +let defaultConfig = Config( + afterStartupCommand: NoOpCommand.instance, + usePaddingForNestedContainersWithTheSameOrientation: false, + autoFlattenContainers: true, + floatingWindowsOnTop: true, modes: [ mainModeId: Mode( name: nil, diff --git a/src/config/parseConfig.swift b/src/config/parseConfig.swift index 0a0de00c..a9081a8e 100644 --- a/src/config/parseConfig.swift +++ b/src/config/parseConfig.swift @@ -5,14 +5,14 @@ import HotKey // todo convert all `error` during config parsing to returning defaults and reporting errors to where? Some kind of log? let mainModeId = "main" -var config: ConfigRoot = defaultConfig +var config: Config = defaultConfig func reloadConfig() { let rawConfig = String.fromUrl(FileManager.default.homeDirectoryForCurrentUser.appending(path: ".aerospace.toml")) - config = parseConfigRoot(rawConfig ?? "") + config = parseConfig(rawConfig ?? "") } -private func parseConfigRoot(_ rawToml: String) -> ConfigRoot { +private func parseConfig(_ rawToml: String) -> Config { let rawTable: TOMLTable do { rawTable = try TOMLTable(string: rawToml) @@ -21,20 +21,44 @@ private func parseConfigRoot(_ rawToml: String) -> ConfigRoot { } catch let e { error(e.localizedDescription) } - var config: Config? = nil + var modes: [String: Mode] = defaultConfig.modes + + let key1 = "after-startup-command" + var value1: Command = defaultConfig.afterStartupCommand + + let key2 = "use-padding-for-nested-containers-with-the-same-orientation" + var value2: Bool = defaultConfig.usePaddingForNestedContainersWithTheSameOrientation + + let key3 = "auto-flatten-containers" + var value3: Bool = defaultConfig.autoFlattenContainers + + let key4 = "floating-windows-on-top" + var value4: Bool = defaultConfig.floatingWindowsOnTop + for (key, value) in rawTable { + let backtrace: TomlBacktrace = .root(key) switch key { - case "config": - config = parseConfig(value, .root("config")) + case key1: + value1 = parseCommand(value, backtrace) + case key2: + value2 = parseBool(value, backtrace) + case key3: + value3 = parseBool(value, backtrace) + case key4: + value4 = parseBool(value, backtrace) case "mode": - modes = parseModes(value, .root("mode")) + modes = parseModes(value, backtrace) default: - unknownKeyError(.root(key)) + unknownKeyError(backtrace) } } - return ConfigRoot( - config: config ?? defaultConfig.config, + + return Config( + afterStartupCommand: value1, + usePaddingForNestedContainersWithTheSameOrientation: value2, + autoFlattenContainers: value3, + floatingWindowsOnTop: value4, modes: modes ) } @@ -89,45 +113,6 @@ private func parseBinding(_ raw: String, _ backtrace: TomlBacktrace) -> (NSEvent return (NSEvent.ModifierFlags(modifiers), key) } -private func parseConfig(_ raw: TOMLValueConvertible, _ backtrace: TomlBacktrace) -> Config { - let rawTable = raw.table ?? expectedActualTypeError(expected: .table, actual: raw.type, backtrace) - - let key1 = "after-startup-command" - var value1: Command = defaultConfig.config.afterStartupCommand - - let key2 = "use-padding-for-nested-containers-with-the-same-orientation" - var value2: Bool = defaultConfig.config.usePaddingForNestedContainersWithTheSameOrientation - - let key3 = "auto-flatten-containers" - var value3: Bool = defaultConfig.config.autoFlattenContainers - - let key4 = "floating-windows-on-top" - var value4: Bool = defaultConfig.config.floatingWindowsOnTop - - for (key, value) in rawTable { - let keyBacktrace = backtrace + .key(key) - switch key { - case key1: - value1 = parseCommand(value, keyBacktrace) - case key2: - value2 = parseBool(value, keyBacktrace) - case key3: - value3 = parseBool(value, keyBacktrace) - case key4: - value4 = parseBool(value, keyBacktrace) - default: - unknownKeyError(backtrace + .key(key)) - } - } - - return Config( - afterStartupCommand: value1, - usePaddingForNestedContainersWithTheSameOrientation: value2, - autoFlattenContainers: value3, - floatingWindowsOnTop: value4 - ) -} - private func parseBool(_ raw: TOMLValueConvertible, _ backtrace: TomlBacktrace) -> Bool { raw.bool ?? expectedActualTypeError(expected: .bool, actual: raw.type, backtrace) }