44 lines
1.5 KiB
Swift
44 lines
1.5 KiB
Swift
import Foundation
|
|
import TOML
|
|
|
|
extension PHTheme {
|
|
/// Load a theme from the resolved config directory (see `PHPaths`).
|
|
/// If the file doesn't exist or fails to parse, the execution is interrupted.
|
|
static func load(_ theme: String) throws -> PHTheme {
|
|
let url = PHPaths.themesDirectory.appending(path: "\(theme).toml")
|
|
|
|
if FileManager.default.fileExists(atPath: url.relativePath) {
|
|
return try load(from: url)
|
|
} else {
|
|
let theme = url.lastPathComponent
|
|
throw PHBar.Error(
|
|
"""
|
|
Theme file not found.
|
|
Make sure to have a file named `\(theme)` inside the
|
|
directory: `\(PHPaths.themesDirectory.relativePath)`
|
|
|
|
Tip: If you've never used phbar before, run the `phbar install` command
|
|
to automatically generate the required configuration files.
|
|
"""
|
|
)
|
|
}
|
|
}
|
|
|
|
/// Load a theme from the file at URL.
|
|
/// If the file doesn't exist or fails to parse, the execution is interrupted.
|
|
static private func load(from url: URL) throws -> PHTheme {
|
|
do {
|
|
let data = try Data(contentsOf: url)
|
|
guard let contents = String(data: data, encoding: .utf8) else {
|
|
throw PHBar.Error("content is not UTF8 encoded.")
|
|
}
|
|
let decoder = TOMLDecoder()
|
|
let themeFile = contents.expanded(from: ProcessInfo.processInfo.environment)
|
|
return try decoder.decode(PHTheme.self, from: themeFile)
|
|
} catch {
|
|
let theme = url.lastPathComponent
|
|
throw PHBar.Error("Theme file `\(theme)` not readable", underlyingError: error)
|
|
}
|
|
}
|
|
}
|