Skip to content

Commit

Permalink
Flatten Config into ConfigRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitabobko committed Sep 14, 2023
1 parent 75cdb1e commit ecd8da8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 62 deletions.
1 change: 0 additions & 1 deletion config-examples/default-config.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 1 addition & 5 deletions src/config/ConfigModel.swift
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
12 changes: 5 additions & 7 deletions src/config/defaultConfig.swift
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
83 changes: 34 additions & 49 deletions src/config/parseConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
)
}
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit ecd8da8

Please sign in to comment.