Sources/OrgApp/AgendaViews.swift

5eef7a168632da902fdd0979d57258320e4fdfc2
orgstar/Sources/OrgApp/AgendaViews.swift history · blame · raw

97 lines · 3821 bytes

 1import Foundation
 2import OrgCore
 3
 4/// What the agenda window shows.
 5public enum AgendaMode: Hashable, Sendable {
 6    /// `org-agenda-list`; nil span and start use the settings.
 7    case agenda(span: Int?, startOffset: Int?)
 8    /// `org-todo-list`, optionally for `KWD1|KWD2`.
 9    case todo(keywords: String?)
10    /// `org-tags-view`.
11    case match(String, todoOnly: Bool)
12    /// `org-search-view`.
13    case search(String, todoOnly: Bool)
14}
15
16/// A named view: the built-in ones and those in `views.toml`.
17public struct AgendaViewDefinition: Hashable, Sendable, Identifiable {
18    public let name: String
19    public let mode: AgendaMode
20    public var id: String { name }
21
22    public init(name: String, mode: AgendaMode) {
23        self.name = name
24        self.mode = mode
25    }
26}
27
28/// Reads `views.toml`:
29///
30/// ```toml
31/// [[view]]
32/// name = "Work"
33/// type = "match"        # agenda, todo, match, todo-match, search or todo-search
34/// match = "+work-boss"
35/// ```
36///
37/// `agenda` views take `span` and `start` (days from today), `todo` views `keywords`
38/// (`WAIT|HOLD`); a `search` view's `match` is its search string.
39@MainActor
40public enum AgendaViewLoader {
41    public static var userFile: URL { UserConfig.file("views.toml") }
42
43    public static let builtIn = [
44        AgendaViewDefinition(name: "Agenda", mode: .agenda(span: nil, startOffset: nil)),
45        AgendaViewDefinition(name: "TODO List", mode: .todo(keywords: nil)),
46    ]
47
48    /// The built-in views followed by the user's, and the problems found in the file.
49    public static func load(userFile: URL = userFile) -> (views: [AgendaViewDefinition], problems: [String]) {
50        let interval = Perf.begin("views.load")
51        defer { interval.end() }
52        guard let text = try? String(contentsOf: userFile, encoding: .utf8) else { return (builtIn, []) }
53        let tables: [TOML.Table]
54        do {
55            tables = try TOML.parse(text)
56        } catch {
57            return (builtIn, ["views.toml: \(error)"])
58        }
59        var views = builtIn
60        var problems: [String] = []
61        for table in tables where !(table.name.isEmpty && table.values.isEmpty) {
62            guard table.name == "view", table.isArrayElement else {
63                problems.append("views.toml line \(table.line): expected [[view]]")
64                continue
65            }
66            guard let name = table.values["name"]?.string else {
67                problems.append("views.toml line \(table.line): a view needs a name")
68                continue
69            }
70            let match = table.values["match"]?.string
71            let mode: AgendaMode
72            switch table.values["type"]?.string ?? "agenda" {
73            case "agenda":
74                mode = .agenda(span: table.values["span"]?.integer, startOffset: table.values["start"]?.integer)
75            case "todo":
76                mode = .todo(keywords: table.values["keywords"]?.string)
77            case "match", "todo-match":
78                guard let match else {
79                    problems.append("views.toml line \(table.line): \(name) needs a match")
80                    continue
81                }
82                mode = .match(match, todoOnly: table.values["type"]?.string == "todo-match")
83            case "search", "todo-search":
84                guard let match else {
85                    problems.append("views.toml line \(table.line): \(name) needs a match")
86                    continue
87                }
88                mode = .search(match, todoOnly: table.values["type"]?.string == "todo-search")
89            case let other:
90                problems.append("views.toml line \(table.line): unknown type \(other)")
91                continue
92            }
93            views.append(AgendaViewDefinition(name: name, mode: mode))
94        }
95        return (views, problems)
96    }
97}