Sources/OrgApp/OrgPreferences.swift
59 lines · 3127 bytes
1import Foundation
2import OrgCore
3
4/// The Org settings `config.toml` holds, read from user defaults with the file's fallbacks, for
5/// both apps.
6public enum OrgPreferences {
7 public static let defaultTodoKeywords = "TODO(t) PROJ(p) LOOP(r) STRT(s) WAIT(w) HOLD(h) IDEA(i) | DONE(d) KILL(k)"
8
9 /// TODO keywords for files without `#+TODO`, and alphabetical lists.
10 public static func orgSettings(_ defaults: UserDefaults = .standard) -> OrgSettings {
11 let line = defaults.string(forKey: "todoKeywords") ?? defaultTodoKeywords
12 var settings = OrgSettings.default.withTodoKeywords(line) ?? .default
13 settings.listAllowAlphabetical = bool("alphabeticalLists", defaults)
14 return settings
15 }
16
17 /// What commands run from the agenda and capture follow: logging and tags.
18 public static func options(_ defaults: UserDefaults = .standard) -> EditingOptions {
19 EditingOptions(
20 tagsColumn: integer("tagsColumn", defaults), insertHeadingRespectContent: bool("headingAfterSubtree", defaults),
21 listAllowAlphabetical: bool("alphabeticalLists", defaults), fillColumn: integer("fillColumn", defaults),
22 logDone: LogHow(rawValue: string("logDone", defaults)), logIntoDrawer: string("logIntoDrawer", defaults).nonEmpty,
23 logReschedule: LogHow(rawValue: string("logReschedule", defaults)), logRedeadline: LogHow(rawValue: string("logRedeadline", defaults))
24 )
25 }
26
27 public static func agendaSpan(_ defaults: UserDefaults = .standard) -> Int { integer("agendaSpan", defaults) }
28 public static func agendaStartOffset(_ defaults: UserDefaults = .standard) -> Int { integer("agendaStartOffset", defaults) }
29 public static func agendaSubfolders(_ defaults: UserDefaults = .standard) -> Bool { bool("agendaSubfolders", defaults) }
30 public static func reminders(_ defaults: UserDefaults = .standard) -> Bool { bool("reminders", defaults) }
31 /// `appt-message-warning-time`, in minutes.
32 public static func reminderLead(_ defaults: UserDefaults = .standard) -> Int { integer("reminderLead", defaults) }
33
34 private static func fallback(_ key: String) -> TOML.Value? {
35 UserConfig.settings.first { $0.defaultsKey == key }?.fallback
36 }
37
38 private static func bool(_ key: String, _ defaults: UserDefaults) -> Bool {
39 defaults.object(forKey: key) as? Bool ?? fallback(key)?.bool ?? false
40 }
41
42 private static func string(_ key: String, _ defaults: UserDefaults) -> String {
43 defaults.string(forKey: key) ?? fallback(key)?.string ?? ""
44 }
45
46 /// Integers, and day offsets stored as integers with `"-3d"` as the fallback.
47 private static func integer(_ key: String, _ defaults: UserDefaults) -> Int {
48 if let value = defaults.object(forKey: key) as? Int { return value }
49 switch fallback(key) {
50 case .integer(let value)?: return value
51 case .string(let text)?: return Int(text.trimmingCharacters(in: CharacterSet(charactersIn: "+d"))) ?? 0
52 default: return 0
53 }
54 }
55}
56
57private extension String {
58 var nonEmpty: String? { isEmpty ? nil : self }
59}