Skip to content

Commit 26523ea

Browse files
committed
✨ Basic config
1 parent d896016 commit 26523ea

File tree

5 files changed

+78
-3
lines changed

5 files changed

+78
-3
lines changed

Docs/Config.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
"iconize": [
88
{
99
"field": "title.label",
10-
"regex": "Movement Yoga"
10+
"regex": "Movement Yoga",
1111
"icon": "🪷"
1212
}
1313
],
14-
"watch": {
14+
"watcher": {
1515
"hook": {
16-
"path": "/opt/homebrew/bin/sketchybar"
16+
"path": "/opt/homebrew/bin/sketchybar",
1717
"args": ["--trigger", "calendar_changed"]
1818
}
1919
}

Sources/Cli/ShowConfig.swift

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import ArgumentParser
2+
import EventKit
3+
import Foundation
4+
5+
/// `plan config`
6+
///
7+
/// Show config
8+
struct ShowConfig: ParsableCommand {
9+
static var configuration = CommandConfiguration(
10+
commandName: "config",
11+
abstract: "Show config",
12+
shouldDisplay: false
13+
)
14+
15+
mutating func run() {
16+
let config = Loader.readConfig()
17+
StdOut.print("\(String(describing: config))")
18+
}
19+
}

Sources/Config.swift

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
struct Config: Codable {
2+
let iconize: [Rule]?
3+
let watcher: Watcher?
4+
}
5+
6+
struct Rule: Codable {
7+
let field: String
8+
let regex: String
9+
let icon: String
10+
}
11+
12+
struct Watcher: Codable {
13+
let hook: Hook?
14+
}
15+
16+
struct Hook: Codable {
17+
let path: String
18+
let args: [String]
19+
}

Sources/Loader.swift

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import Foundation
2+
3+
enum Loader {
4+
private static let appName = "plan"
5+
private static let defaultConfigHome = FileManager.default
6+
.homeDirectoryForCurrentUser
7+
.appendingPathComponent(".config")
8+
private static let xdgConfigHome = ProcessInfo.processInfo.environment["XDG_CONFIG_HOME"]
9+
.map { URL(fileURLWithPath: $0) }
10+
private static let configHome = xdgConfigHome ?? defaultConfigHome
11+
12+
private static let configDir = configHome.appendingPathComponent(appName)
13+
private static let configUrl = configDir.appendingPathComponent("config.json")
14+
15+
private static func readLocalJSON<T: Codable>(from path: String, as _: T.Type) -> T? {
16+
let url = URL(fileURLWithPath: path)
17+
18+
do {
19+
let data = try Data(contentsOf: url)
20+
let decoder = JSONDecoder()
21+
return try decoder.decode(T.self, from: data)
22+
} catch {
23+
print("Error reading or decoding file at \(path): \(error)")
24+
return nil
25+
}
26+
}
27+
28+
static func readConfig() -> Config? {
29+
if let config: Config = readLocalJSON(from: configUrl.path, as: Config.self) {
30+
return config
31+
} else {
32+
print("Failed to read JSON file.")
33+
return nil
34+
}
35+
}
36+
}

Sources/Plan.swift

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ struct Plan: ParsableCommand {
1313
Add.self,
1414
Calendars.self,
1515
Next.self,
16+
ShowConfig.self,
1617
Today.self,
1718
Usage.self,
1819
Watch.self,

0 commit comments

Comments
 (0)