Commit ea21f9ec4d
Verified · cmc
Layout: unified · split
Sources/OrgApp/ConfigSync.swift +3 −1
| @@ -117,7 +117,9 @@ public final class ConfigSync { | |||
| 117 | /// The theme from the file read last and its theme file. | 117 | /// The theme from the file read last and its theme file. |
| 118 | func readTheme() { | 118 | func readTheme() { |
| 119 | guard let text, let read = UserConfig.theme( | 119 | guard let text, let read = UserConfig.theme( |
| 120 | config: text, themeFile: defaults.string(forKey: "themeFile"), in: url.deletingLastPathComponent() | 120 | config: text, themeFile: defaults.string(forKey: "themeFile"), |
| 121 | lightTheme: defaults.string(forKey: "themeLight"), darkTheme: defaults.string(forKey: "themeDark"), | ||
| 122 | in: url.deletingLastPathComponent() | ||
| 121 | ) else { return } | 123 | ) else { return } |
| 122 | if read.theme != theme { theme = read.theme } | 124 | if read.theme != theme { theme = read.theme } |
| 123 | problems = configProblems + read.problems | 125 | problems = configProblems + read.problems |
Sources/OrgApp/UserConfig.swift +11 −6
| @@ -24,19 +24,20 @@ public enum UserConfig { | |||
| 24 | } | 24 | } |
| 25 | 25 | ||
| 26 | /// The theme `config.toml` (`text`) sets: its `[theme]` tables' colors over those of | 26 | /// The theme `config.toml` (`text`) sets: its `[theme]` tables' colors over those of |
| 27 | /// `theme-file` (a path relative to `folder`), over the default theme; with problems in | 27 | /// `theme-file` (a path relative to `folder`), over the palettes `light-theme` and |
| 28 | /// both. Nil when `text` isn't TOML. | 28 | /// `dark-theme` name; with problems in all three. Nil when `text` isn't TOML. |
| 29 | public static func theme( | 29 | public static func theme( |
| 30 | config text: String, themeFile name: String?, in folder: URL, | 30 | config text: String, themeFile name: String?, lightTheme: String? = nil, darkTheme: String? = nil, in folder: URL, |
| 31 | read: (URL) -> String? = { try? String(contentsOf: $0, encoding: .utf8) } | 31 | read: (URL) -> String? = { try? String(contentsOf: $0, encoding: .utf8) } |
| 32 | ) -> (theme: ThemeSpec, problems: [String])? { | 32 | ) -> (theme: ThemeSpec, problems: [String])? { |
| 33 | guard let tables = try? TOML.parse(text) else { return nil } | 33 | guard let tables = try? TOML.parse(text) else { return nil } |
| 34 | var spec = ThemeSpec.default | 34 | var (spec, problems) = ThemeSpec.base(light: lightTheme, dark: darkTheme) |
| 35 | var problems: [String] = [] | ||
| 36 | if let name, !name.isEmpty { | 35 | if let name, !name.isEmpty { |
| 37 | if let themeText = read(folder.appendingPathComponent(name)) { | 36 | if let themeText = read(folder.appendingPathComponent(name)) { |
| 38 | do { | 37 | do { |
| 39 | (spec, problems) = ThemeSpec.reading(try TOML.parse(themeText), file: name) | 38 | let (read, more) = ThemeSpec.reading(try TOML.parse(themeText), over: spec, file: name) |
| 39 | spec = read | ||
| 40 | problems += more | ||
| 40 | } catch { | 41 | } catch { |
| 41 | problems.append("\(name): \(error)") | 42 | problems.append("\(name): \(error)") |
| 42 | } | 43 | } |
| @@ -307,6 +308,10 @@ public enum UserConfig { | |||
| 307 | comment: "points a heading is larger than the level below; level 4 and deeper are body size", legacy: ""), | 308 | comment: "points a heading is larger than the level below; level 4 and deeper are body size", legacy: ""), |
| 308 | Setting(section: "theme", key: "theme-file", defaultsKey: "themeFile", kind: .string, fallback: .string(""), | 309 | Setting(section: "theme", key: "theme-file", defaultsKey: "themeFile", kind: .string, fallback: .string(""), |
| 309 | comment: "a theme in its own file in this folder, under the colors set here; colors: see default-theme.toml", legacy: ""), | 310 | comment: "a theme in its own file in this folder, under the colors set here; colors: see default-theme.toml", legacy: ""), |
| 311 | Setting(section: "theme", key: "light-theme", defaultsKey: "themeLight", kind: .string, fallback: .string("default-light"), | ||
| 312 | comment: "the built-in theme for the light appearance: default-light, solarized-light, gruvbox-light, modus-operandi, catppuccin-latte, tomorrow or paper", legacy: ""), | ||
| 313 | Setting(section: "theme", key: "dark-theme", defaultsKey: "themeDark", kind: .string, fallback: .string("default-dark"), | ||
| 314 | comment: "the built-in theme for the dark appearance: default-dark, solarized-dark, gruvbox-dark, modus-vivendi, catppuccin-mocha, nord, dracula, tokyo-night or doom-one", legacy: ""), | ||
| 310 | Setting(section: "orgstar", key: "save", defaultsKey: "saveMode", kind: .choice([("automatic", "automatic"), ("explicit", "explicit")]), | 315 | Setting(section: "orgstar", key: "save", defaultsKey: "saveMode", kind: .choice([("automatic", "automatic"), ("explicit", "explicit")]), |
| 311 | fallback: .string("automatic"), comment: "automatic (1 s after typing stops) or explicit (only with ⌘S)", legacy: "general.save"), | 316 | fallback: .string("automatic"), comment: "automatic (1 s after typing stops) or explicit (only with ⌘S)", legacy: "general.save"), |
| 312 | Setting(section: "orgstar", key: "keymap", defaultsKey: "keymap", kind: .choice([("emacs", "emacs"), ("mac", "mac"), ("doom", "doom")]), | 317 | Setting(section: "orgstar", key: "keymap", defaultsKey: "keymap", kind: .choice([("emacs", "emacs"), ("mac", "mac"), ("doom", "doom")]), |
Sources/OrgPresentation/Palettes.swift +16
| @@ -29,6 +29,22 @@ extension ThemeSpec { | |||
| 29 | "default-" + appearance.rawValue | 29 | "default-" + appearance.rawValue |
| 30 | } | 30 | } |
| 31 | 31 | ||
| 32 | /// The default theme with each appearance's colors from the palette `light-theme` and | ||
| 33 | /// `dark-theme` name; Default where one is unset or unknown, with a problem for unknown. | ||
| 34 | public static func base(light: String?, dark: String?) -> (ThemeSpec, [String]) { | ||
| 35 | var spec = ThemeSpec.default | ||
| 36 | var problems: [String] = [] | ||
| 37 | for (id, appearance) in [(light, Palette.Appearance.light), (dark, .dark)] { | ||
| 38 | guard let id, !id.isEmpty, id != defaultPaletteID(appearance) else { continue } | ||
| 39 | guard let palette = palette(id, appearance) else { | ||
| 40 | problems.append("config.toml: unknown \(appearance.rawValue)-theme \(id)") | ||
| 41 | continue | ||
| 42 | } | ||
| 43 | if appearance == .light { spec.light = palette.colors } else { spec.dark = palette.colors } | ||
| 44 | } | ||
| 45 | return (spec, problems) | ||
| 46 | } | ||
| 47 | |||
| 32 | // Upstream palettes; see THIRD_PARTY.org. Colors darkened from upstream to read at 3:1 on | 48 | // Upstream palettes; see THIRD_PARTY.org. Colors darkened from upstream to read at 3:1 on |
| 33 | // the background: Solarized Light yellow, green and cyan; Catppuccin Latte orange, yellow | 49 | // the background: Solarized Light yellow, green and cyan; Catppuccin Latte orange, yellow |
| 34 | // and green; Tomorrow orange and yellow. | 50 | // and green; Tomorrow orange and yellow. |
Sources/OrgstarMobile/MobileModel.swift +10 −3
| @@ -238,12 +238,19 @@ final class MobileModel { | |||
| 238 | } | 238 | } |
| 239 | 239 | ||
| 240 | private static func readConfig(_ folder: URL?) -> ([String], ThemeSpec) { | 240 | private static func readConfig(_ folder: URL?) -> ([String], ThemeSpec) { |
| 241 | guard let folder else { return ([], .default) } | 241 | let defaults = UserDefaults.standard |
| 242 | guard let folder else { | ||
| 243 | let (base, problems) = ThemeSpec.base(light: defaults.string(forKey: "themeLight"), dark: defaults.string(forKey: "themeDark")) | ||
| 244 | return (problems, base) | ||
| 245 | } | ||
| 242 | guard let text = read(folder.appendingPathComponent("config.toml")) else { | 246 | guard let text = read(folder.appendingPathComponent("config.toml")) else { |
| 243 | return (["No config.toml in \(folder.lastPathComponent); the defaults apply."], .default) | 247 | return (["No config.toml in \(folder.lastPathComponent); the defaults apply."], .default) |
| 244 | } | 248 | } |
| 245 | let problems = UserConfig.apply(text, to: .standard) | 249 | let problems = UserConfig.apply(text, to: defaults) |
| 246 | guard let theme = UserConfig.theme(config: text, themeFile: UserDefaults.standard.string(forKey: "themeFile"), in: folder, read: read) else { | 250 | guard let theme = UserConfig.theme( |
| 251 | config: text, themeFile: defaults.string(forKey: "themeFile"), | ||
| 252 | lightTheme: defaults.string(forKey: "themeLight"), darkTheme: defaults.string(forKey: "themeDark"), in: folder, read: read | ||
| 253 | ) else { | ||
| 247 | return (problems, .default) | 254 | return (problems, .default) |
| 248 | } | 255 | } |
| 249 | return (problems + theme.problems, theme.theme) | 256 | return (problems + theme.problems, theme.theme) |
Tests/OrgAppTests/UserConfigTests.swift +27
| @@ -253,4 +253,31 @@ struct ThemeReadingTests { | |||
| 253 | #expect(bad.problems == ["t.toml: unknown theme color nope", "config.toml: theme.link must be a color such as \"#1f2328\""]) | 253 | #expect(bad.problems == ["t.toml: unknown theme color nope", "config.toml: theme.link must be a color such as \"#1f2328\""]) |
| 254 | #expect(UserConfig.theme(config: "[theme", themeFile: nil, in: folder)?.theme == nil) | 254 | #expect(UserConfig.theme(config: "[theme", themeFile: nil, in: folder)?.theme == nil) |
| 255 | } | 255 | } |
| 256 | |||
| 257 | @Test @MainActor func chosenPalettesSitUnderTheThemeFileAndConfig() throws { | ||
| 258 | let name = "orgstar-palette-\(UUID().uuidString)" | ||
| 259 | let defaults = UserDefaults(suiteName: name)! | ||
| 260 | defer { defaults.removePersistentDomain(forName: name) } | ||
| 261 | let folder = FileManager.default.temporaryDirectory.appendingPathComponent(name) | ||
| 262 | defer { try? FileManager.default.removeItem(at: folder) } | ||
| 263 | try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true) | ||
| 264 | try "[theme.dark]\nlink = \"#000001\"\n".write(to: folder.appendingPathComponent("mine.toml"), atomically: true, encoding: .utf8) | ||
| 265 | try "[theme]\nlight-theme = \"paper\"\ndark-theme = \"nord\"\ntheme-file = \"mine.toml\"\n[theme.dark]\ntodo = \"#000002\"\n" | ||
| 266 | .write(to: folder.appendingPathComponent("config.toml"), atomically: true, encoding: .utf8) | ||
| 267 | let sync = ConfigSync(url: folder.appendingPathComponent("config.toml"), defaults: defaults) | ||
| 268 | sync.start(watch: false) | ||
| 269 | #expect(sync.problems.isEmpty, "\(sync.problems)") | ||
| 270 | #expect(defaults.string(forKey: "themeLight") == "paper" && defaults.string(forKey: "themeDark") == "nord") | ||
| 271 | let nord = try #require(ThemeSpec.palette("nord", .dark)) | ||
| 272 | #expect(sync.theme.light["background"] == ThemeSpec.palette("paper", .light)?.colors["background"]) | ||
| 273 | #expect(sync.theme.dark["background"] == nord.colors["background"]) | ||
| 274 | #expect(sync.theme.dark["link"]?.hex == "#000001") | ||
| 275 | #expect(sync.theme.dark["todo"]?.hex == "#000002") | ||
| 276 | } | ||
| 277 | |||
| 278 | @Test func noThemeSettingsGiveTheDefaultTheme() throws { | ||
| 279 | let folder = FileManager.default.temporaryDirectory | ||
| 280 | let theme = try #require(UserConfig.theme(config: "", themeFile: nil, lightTheme: nil, darkTheme: nil, in: folder)) | ||
| 281 | #expect(theme.theme == ThemeSpec.default && theme.problems.isEmpty) | ||
| 282 | } | ||
| 256 | } | 283 | } |
Tests/OrgPresentationTests/PaletteTests.swift +14
| @@ -60,4 +60,18 @@ struct PaletteTests { | |||
| 60 | } | 60 | } |
| 61 | } | 61 | } |
| 62 | } | 62 | } |
| 63 | |||
| 64 | @Test func baseTakesEachAppearanceFromItsPalette() throws { | ||
| 65 | let (spec, problems) = ThemeSpec.base(light: "paper", dark: "nord") | ||
| 66 | #expect(problems.isEmpty) | ||
| 67 | #expect(spec.light == ThemeSpec.palette("paper", .light)?.colors && spec.dark == ThemeSpec.palette("nord", .dark)?.colors) | ||
| 68 | #expect(ThemeSpec.base(light: nil, dark: nil).0 == ThemeSpec.default) | ||
| 69 | #expect(ThemeSpec.base(light: "", dark: "").0 == ThemeSpec.default) | ||
| 70 | } | ||
| 71 | |||
| 72 | @Test func aPaletteOfTheOtherAppearanceIsAProblem() { | ||
| 73 | let (spec, problems) = ThemeSpec.base(light: "nord", dark: "unknown") | ||
| 74 | #expect(spec == ThemeSpec.default) | ||
| 75 | #expect(problems == ["config.toml: unknown light-theme nord", "config.toml: unknown dark-theme unknown"]) | ||
| 76 | } | ||
| 63 | } | 77 | } |