Sources/OrgApp/UserConfigTheme.swift

main
orgstar/Sources/OrgApp/UserConfigTheme.swift

44 lines · 2070 bytes

2 symbols in this file
 1import Foundation
 2import OrgConfig
 3import OrgCore
 4import OrgPresentation
 5
 6extension UserConfig {
 7    /// The theme `config.toml` (`text`) sets: its `[theme]` tables' colors over those of
 8    /// `theme-file` (a path relative to `folder`), over the palettes `light-theme` and
 9    /// `dark-theme` name; with problems in all three. Nil when `text` isn't TOML.
10    public static func theme(
11        config text: String, themeFile name: String?, lightTheme: String? = nil, darkTheme: String? = nil, in folder: URL,
12        read: (URL) -> String? = { try? String(contentsOf: $0, encoding: .utf8) }
13    ) -> (theme: ThemeSpec, problems: [String])? {
14        guard let tables = try? TOML.parse(text) else { return nil }
15        var (spec, problems) = ThemeSpec.base(light: lightTheme, dark: darkTheme)
16        if let name, !name.isEmpty {
17            if let themeText = read(folder.appendingPathComponent(name)) {
18                do {
19                    let (read, more) = ThemeSpec.reading(try TOML.parse(themeText), over: spec, file: name)
20                    spec = read
21                    problems += more
22                } catch {
23                    problems.append("\(name): \(error)")
24                }
25            } else {
26                problems.append("config.toml: theme-file \(name) can't be read")
27            }
28        }
29        let (merged, more) = ThemeSpec.reading(tables, over: spec)
30        return (merged, problems + more)
31    }
32
33    /// The file Copy to Config Folder writes.
34    public static let copiedThemeFile = "my-theme.toml"
35
36    /// Writes the two palettes into `folder` as `copiedThemeFile`; false, writing nothing, when
37    /// the file is already there.
38    public static func copyTheme(light: Palette, dark: Palette, to folder: URL) throws -> Bool {
39        let file = folder.appendingPathComponent(copiedThemeFile)
40        guard !FileManager.default.fileExists(atPath: file.path) else { return false }
41        try ThemeSpec.themeFile(light: light, dark: dark).write(to: file, atomically: true, encoding: .utf8)
42        return true
43    }
44}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form