Commit ea21f9ec4d

ea21f9ec4db0c0989490c087c57f8c185c3a97c8

parent: 6005b32424

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-14 13:58 UTC

light-theme and dark-theme settings, under theme-file and config.toml colors

Layout: unified · split

Sources/OrgApp/ConfigSync.swift +3 −1
@@ -117,7 +117,9 @@ public final class ConfigSync {
117117 /// The theme from the file read last and its theme file.
118118 func readTheme() {
119119 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()
121123 ) else { return }
122124 if read.theme != theme { theme = read.theme }
123125 problems = configProblems + read.problems
Sources/OrgApp/UserConfig.swift +11 −6
@@ -24,19 +24,20 @@ public enum UserConfig {
2424 }
2525
2626 /// 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
28 /// both. Nil when `text` isn't TOML.
27 /// `theme-file` (a path relative to `folder`), over the palettes `light-theme` and
28 /// `dark-theme` name; with problems in all three. Nil when `text` isn't TOML.
2929 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,
3131 read: (URL) -> String? = { try? String(contentsOf: $0, encoding: .utf8) }
3232 ) -> (theme: ThemeSpec, problems: [String])? {
3333 guard let tables = try? TOML.parse(text) else { return nil }
34 var spec = ThemeSpec.default
35 var problems: [String] = []
34 var (spec, problems) = ThemeSpec.base(light: lightTheme, dark: darkTheme)
3635 if let name, !name.isEmpty {
3736 if let themeText = read(folder.appendingPathComponent(name)) {
3837 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
4041 } catch {
4142 problems.append("\(name): \(error)")
4243 }
@@ -307,6 +308,10 @@ public enum UserConfig {
307308 comment: "points a heading is larger than the level below; level 4 and deeper are body size", legacy: ""),
308309 Setting(section: "theme", key: "theme-file", defaultsKey: "themeFile", kind: .string, fallback: .string(""),
309310 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: ""),
310315 Setting(section: "orgstar", key: "save", defaultsKey: "saveMode", kind: .choice([("automatic", "automatic"), ("explicit", "explicit")]),
311316 fallback: .string("automatic"), comment: "automatic (1 s after typing stops) or explicit (only with ⌘S)", legacy: "general.save"),
312317 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 {
2929 "default-" + appearance.rawValue
3030 }
3131
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
3248 // Upstream palettes; see THIRD_PARTY.org. Colors darkened from upstream to read at 3:1 on
3349 // the background: Solarized Light yellow, green and cyan; Catppuccin Latte orange, yellow
3450 // and green; Tomorrow orange and yellow.
Sources/OrgstarMobile/MobileModel.swift +10 −3
@@ -238,12 +238,19 @@ final class MobileModel {
238238 }
239239
240240 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 }
242246 guard let text = read(folder.appendingPathComponent("config.toml")) else {
243247 return (["No config.toml in \(folder.lastPathComponent); the defaults apply."], .default)
244248 }
245 let problems = UserConfig.apply(text, to: .standard)
246 guard let theme = UserConfig.theme(config: text, themeFile: UserDefaults.standard.string(forKey: "themeFile"), in: folder, read: read) else {
249 let problems = UserConfig.apply(text, to: defaults)
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 {
247254 return (problems, .default)
248255 }
249256 return (problems + theme.problems, theme.theme)
Tests/OrgAppTests/UserConfigTests.swift +27
@@ -253,4 +253,31 @@ struct ThemeReadingTests {
253253 #expect(bad.problems == ["t.toml: unknown theme color nope", "config.toml: theme.link must be a color such as \"#1f2328\""])
254254 #expect(UserConfig.theme(config: "[theme", themeFile: nil, in: folder)?.theme == nil)
255255 }
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 }
256283}
Tests/OrgPresentationTests/PaletteTests.swift +14
@@ -60,4 +60,18 @@ struct PaletteTests {
6060 }
6161 }
6262 }
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 }
6377}