feat: use TOML for editable color configuration

This commit is contained in:
2026-10-22 15:51:20 +02:00
parent be761b4704
commit c48ef4c018
5 changed files with 79 additions and 61 deletions
+41 -34
View File
@@ -11,10 +11,10 @@ func missingColorConfigurationUsesSunsetWithoutReadingGeneratedTheme() throws {
let loaded = try ColorScheme.load(from: directory)
expectSameColors(loaded, ColorScheme.sunset)
try Data("{}".utf8).write(to: directory.appendingPathComponent("config.json"))
try Data("# Comments-only configuration\n".utf8).write(to: directory.appendingPathComponent("config.toml"))
expectSameColors(try ColorScheme.load(from: directory), ColorScheme.sunset)
try Data(#"{"theme":"sunset"}"#.utf8).write(to: directory.appendingPathComponent("config.json"))
try Data("theme = 'sunset'\n".utf8).write(to: directory.appendingPathComponent("config.toml"))
expectSameColors(try ColorScheme.load(from: directory), ColorScheme.sunset)
}
@@ -22,8 +22,12 @@ func missingColorConfigurationUsesSunsetWithoutReadingGeneratedTheme() throws {
func partialManualColorsOverrideSunsetWithoutChangingOtherFields() throws {
let directory = try colorConfigurationDirectory()
defer { try? FileManager.default.removeItem(at: directory) }
try Data(##"{"colors":{"accent":"#aB1234","error":"#123456"}}"##.utf8)
.write(to: directory.appendingPathComponent("config.json"))
try Data("""
# Personal color overrides
[colors]
accent = '#aB1234' # Keep the hash inside the quoted color
error = "#123456"
""".utf8).write(to: directory.appendingPathComponent("config.toml"))
var expected = ColorScheme.sunset
expected.accent = NSColor(srgbRed: 171.0 / 255, green: 18.0 / 255, blue: 52.0 / 255, alpha: 1)
@@ -35,8 +39,12 @@ func partialManualColorsOverrideSunsetWithoutChangingOtherFields() throws {
func tintColorsLayerBetweenDefaultsAndManualOverrides() throws {
let directory = try colorConfigurationDirectory()
defer { try? FileManager.default.removeItem(at: directory) }
try Data(##"{"theme":"tint","colors":{"accent":"#ffffff","text":"#000000"}}"##.utf8)
.write(to: directory.appendingPathComponent("config.json"))
try Data("""
theme = "tint"
[colors]
accent = "#ffffff"
text = "#000000"
""".utf8).write(to: directory.appendingPathComponent("config.toml"))
try Data(##"{"colors":{"accent":"#ff0000","background":"#123456","glow":"#00ff00"}}"##.utf8)
.write(to: directory.appendingPathComponent("theme.json"))
@@ -52,29 +60,28 @@ func tintColorsLayerBetweenDefaultsAndManualOverrides() throws {
func malformedColorConfigurationReportsItsPathAndReason() throws {
let directory = try colorConfigurationDirectory()
defer { try? FileManager.default.removeItem(at: directory) }
let config = directory.appendingPathComponent("config.json")
let invalid: [(String, String)] = [
(##"{"colors":{"accent":"#12345678"}}"##, "colors.accent"),
(##"{"colors":{"accent":"#fff"}}"##, "colors.accent"),
(##"{"colors":{"accent":"#12gg56"}}"##, "colors.accent"),
(##"{"colors":{"accent":"#123456"}}"##, "colors.accent"),
(#"{"colors":{"accent":"123456"}}"#, "colors.accent"),
(#"{"colors":{"accent":" #123456"}}"#, "colors.accent"),
(#"{"colors":{"accent":42}}"#, "colors.accent"),
(#"{"colors":{"accent":null}}"#, "colors.accent"),
(##"{"colors":{"accnet":"#123456"}}"##, "colors.accnet"),
(#"{"opacity":0.5}"#, "opacity"),
(#"{"colors":[]}"#, "colors must be an object"),
(#"{"colors":null}"#, "colors must be an object"),
(#"{"theme":"unknown"}"#, "theme"),
(#"{"theme":true}"#, "theme"),
(#"{"theme":null}"#, "theme"),
("{", "Invalid JSON"),
("[]", "JSON object"),
("null", "JSON object"),
let config = directory.appendingPathComponent("config.toml")
let invalid: [(String, String?)] = [
("colors.accent = '#12345678'", "colors.accent"),
("colors.accent = '#fff'", "colors.accent"),
("colors.accent = '#12gg56'", "colors.accent"),
("colors.accent = '#123456'", "colors.accent"),
("colors.accent = '123456'", "colors.accent"),
("colors.accent = ' #123456'", "colors.accent"),
("colors.accent = 42", "colors.accent"),
("colors.accnet = '#123456'", "colors.accnet"),
("opacity = 0.5", "opacity"),
("colors = []", "colors"),
("colors = false", "colors"),
("theme = 'unknown'", "theme"),
("theme = true", "theme"),
("[colors", nil),
("colors.accent = '#123456'\ncolors.accent = '#abcdef'", nil),
("[colors]\n[colors]", nil),
("theme = 'sunset'\ntheme = 'tint'", nil),
]
for (json, reason) in invalid {
try Data(json.utf8).write(to: config)
for (toml, reason) in invalid {
try Data(toml.utf8).write(to: config)
expectColorSchemeFailure(in: directory, path: config, reason: reason)
}
}
@@ -84,17 +91,17 @@ func selectedTintRequiresAnExistingValidGeneratedTheme() throws {
let directory = try colorConfigurationDirectory()
defer { try? FileManager.default.removeItem(at: directory) }
let theme = directory.appendingPathComponent("theme.json")
try Data(##"{"theme":"tint","colors":{"accent":"#ffffff"}}"##.utf8)
.write(to: directory.appendingPathComponent("config.json"))
try Data("theme = 'tint'\ncolors.accent = '#ffffff'\n".utf8)
.write(to: directory.appendingPathComponent("config.toml"))
expectColorSchemeFailure(in: directory, path: theme)
let invalid: [(String, String)] = [
("not JSON", "Invalid JSON"),
("[]", "JSON object"),
let invalid: [(String, String?)] = [
("not JSON", nil),
("[]", nil),
(#"{"theme":"sunset"}"#, "theme"),
(##"{"colors":{"accent":"#12345678"}}"##, "colors.accent"),
(##"{"colors":{"glwo":"#123456"}}"##, "colors.glwo"),
(#"{"colors":false}"#, "colors must be an object"),
(#"{"colors":false}"#, "colors"),
]
for (json, reason) in invalid {
try Data(json.utf8).write(to: theme)