make decodeTheme cleaner, still works yayyy

This commit is contained in:
neon443
2025-06-27 18:27:10 +01:00
parent 338957e986
commit c343066fb5

View File

@@ -51,35 +51,26 @@ struct Theme: Hashable, Equatable, Identifiable {
static func decodeTheme(name: String, data: Data?) -> Theme? {
guard let data else { return nil }
guard let string = String(data: data, encoding: .utf8) else { return nil }
if string.contains("plist") {
guard let decoded = try? PropertyListDecoder().decode(ThemeCodable.self, from: data) else { return nil }
return Theme(
name: name,
ansi: decoded.ansi,
foreground: Color(decoded.foreground),
background: Color(decoded.background),
cursor: Color(decoded.cursor),
cursorText: Color(decoded.cursorText),
bold: Color(decoded.bold),
selectedText: Color(decoded.selectedText),
selection: Color(decoded.selection)
)
} else {
guard let decoded = try? JSONDecoder().decode(ThemeCodable.self, from: data) else { return nil }
return Theme(
name: name,
ansi: decoded.ansi,
foreground: Color(decoded.foreground),
background: Color(decoded.background),
cursor: Color(decoded.cursor),
cursorText: Color(decoded.cursorText),
bold: Color(decoded.bold),
selectedText: Color(decoded.selectedText),
selection: Color(decoded.selection)
)
}
let plistDecoder = PropertyListDecoder()
let jsonDecoder = JSONDecoder()
guard let decoded =
(try? plistDecoder.decode(ThemeCodable.self, from: data)) ??
(try? jsonDecoder.decode(ThemeCodable.self, from: data))
else { return nil }
let theme = Theme(
name: name,
ansi: decoded.ansi,
foreground: Color(decoded.foreground),
background: Color(decoded.background),
cursor: Color(decoded.cursor),
cursorText: Color(decoded.cursorText),
bold: Color(decoded.bold),
selectedText: Color(decoded.selectedText),
selection: Color(decoded.selection)
)
return theme
}
}