Sources/OrgApp/AgendaViews.swift
161 lines · 7140 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 /// `org-agenda-list-stuck-projects`; nil uses the `org-stuck-projects` setting.
15 case stuck(StuckProjects?)
16
17 /// The key `org-agenda-prefix-format` and `org-agenda-sorting-strategy` use for this kind
18 /// of view.
19 public var viewKey: String {
20 switch self {
21 case .agenda: "agenda"
22 case .todo: "todo"
23 case .match, .stuck: "tags"
24 case .search: "search"
25 }
26 }
27}
28
29/// A view's own `org-agenda-prefix-format` and `org-agenda-sorting-strategy`, as an
30/// `org-agenda-custom-commands` entry's settings give them.
31public struct AgendaViewSettings: Hashable, Sendable {
32 public var prefixFormat: String?
33 public var sortingStrategy: [String]?
34
35 public init(prefixFormat: String? = nil, sortingStrategy: [String]? = nil) {
36 self.prefixFormat = prefixFormat
37 self.sortingStrategy = sortingStrategy
38 }
39
40 public func apply(to options: AgendaOptions, view: String) -> AgendaOptions {
41 var options = options
42 if let prefixFormat { options.prefixFormat[view] = prefixFormat }
43 if let sortingStrategy { options.sortingStrategy[view] = sortingStrategy }
44 return options
45 }
46}
47
48/// A named view: the built-in ones and those in `views.toml`.
49public struct AgendaViewDefinition: Hashable, Sendable, Identifiable {
50 public let name: String
51 public let mode: AgendaMode
52 public var settings: AgendaViewSettings
53 public var id: String { name }
54
55 public init(name: String, mode: AgendaMode, settings: AgendaViewSettings = AgendaViewSettings()) {
56 self.name = name
57 self.mode = mode
58 self.settings = settings
59 }
60}
61
62/// Reads `views.toml`:
63///
64/// ```toml
65/// [[view]]
66/// name = "Work"
67/// type = "match" # agenda, todo, match, todo-match, search or todo-search
68/// match = "+work-boss"
69/// ```
70///
71/// `agenda` views take `span` and `start` (days from today), `todo` views `keywords`
72/// (`WAIT|HOLD`); a `search` view's `match` is its search string. `stuck` views take
73/// `match`, `keywords`, `tags` and `regexp` as `[org-stuck-projects]` does, each defaulting
74/// to that setting. Any view can set
75/// `prefix-format` and `sorting-strategy` (keys separated by spaces) for itself.
76@MainActor
77public enum AgendaViewLoader {
78 public static var userFile: URL { UserConfig.file("views.toml") }
79
80 public static let builtIn = [
81 AgendaViewDefinition(name: "Agenda", mode: .agenda(span: nil, startOffset: nil)),
82 AgendaViewDefinition(name: "TODO List", mode: .todo(keywords: nil)),
83 AgendaViewDefinition(name: "Stuck Projects", mode: .stuck(nil)),
84 ]
85
86 /// The views' settings by mode; the first view with a mode wins.
87 public static func settings(_ views: [AgendaViewDefinition]) -> [AgendaMode: AgendaViewSettings] {
88 var settings: [AgendaMode: AgendaViewSettings] = [:]
89 for view in views where settings[view.mode] == nil && view.settings != AgendaViewSettings() { settings[view.mode] = view.settings }
90 return settings
91 }
92
93 /// The built-in views followed by the user's, and the problems found in the file.
94 public static func load(userFile: URL = userFile) -> (views: [AgendaViewDefinition], problems: [String]) {
95 let interval = Perf.begin("views.load")
96 defer { interval.end() }
97 guard let text = try? String(contentsOf: userFile, encoding: .utf8) else { return (builtIn, []) }
98 let tables: [TOML.Table]
99 do {
100 tables = try TOML.parse(text)
101 } catch {
102 return (builtIn, ["views.toml: \(error)"])
103 }
104 var views = builtIn
105 var problems: [String] = []
106 for table in tables where !(table.name.isEmpty && table.values.isEmpty) {
107 guard table.name == "view", table.isArrayElement else {
108 problems.append("views.toml line \(table.line): expected [[view]]")
109 continue
110 }
111 guard let name = table.values["name"]?.string else {
112 problems.append("views.toml line \(table.line): a view needs a name")
113 continue
114 }
115 let match = table.values["match"]?.string
116 let mode: AgendaMode
117 switch table.values["type"]?.string ?? "agenda" {
118 case "agenda":
119 mode = .agenda(span: table.values["span"]?.integer, startOffset: table.values["start"]?.integer)
120 case "todo":
121 mode = .todo(keywords: table.values["keywords"]?.string)
122 case "match", "todo-match":
123 guard let match else {
124 problems.append("views.toml line \(table.line): \(name) needs a match")
125 continue
126 }
127 mode = .match(match, todoOnly: table.values["type"]?.string == "todo-match")
128 case "search", "todo-search":
129 guard let match else {
130 problems.append("views.toml line \(table.line): \(name) needs a match")
131 continue
132 }
133 mode = .search(match, todoOnly: table.values["type"]?.string == "todo-search")
134 case "stuck":
135 if ["match", "keywords", "tags", "regexp"].allSatisfy({ table.values[$0] == nil }) {
136 mode = .stuck(nil)
137 } else {
138 var definition = OrgPreferences.stuckProjects()
139 if let match { definition.match = match }
140 if let keywords = table.values["keywords"]?.string { definition.todo = OrgPreferences.words(keywords) }
141 if let tags = table.values["tags"]?.string { definition.tags = OrgPreferences.words(tags) }
142 if let regexp = table.values["regexp"]?.string { definition.regexp = regexp }
143 mode = .stuck(definition)
144 }
145 case let other:
146 problems.append("views.toml line \(table.line): unknown type \(other)")
147 continue
148 }
149 var settings = AgendaViewSettings(prefixFormat: table.values["prefix-format"]?.string)
150 if let strategy = table.values["sorting-strategy"]?.string {
151 let keys = strategy.split(whereSeparator: \.isWhitespace).map(String.init)
152 if let bad = keys.first(where: { !AgendaSort.keys.contains($0) }) {
153 problems.append("views.toml line \(table.line): unknown sorting key \(bad)")
154 }
155 settings.sortingStrategy = keys.filter(AgendaSort.keys.contains)
156 }
157 views.append(AgendaViewDefinition(name: name, mode: mode, settings: settings))
158 }
159 return (views, problems)
160 }
161}