Sources/OrgCore/Agenda/PrefixFormat.swift
85 lines · 4071 bytes
1import Foundation
2
3/// `org-agenda-prefix-format` for one view, compiled as `org-compile-prefix-format` does:
4/// `%c` category, `%t` time, `%l` level, `%s` extra, `%e` effort, `%b` breadcrumbs, `%T` last
5/// tag (Org's regexp lists only lower-case keys but matches `T` under `case-fold-search`),
6/// `%i` icon (none in Orgstar), each with an optional `?` (empty when the value is), a `%-12`-style
7/// width with an optional `.N` limit on categories, and a punctuation character added after a
8/// non-empty value. `%(…)` forms run Lisp; they are empty here.
9public struct PrefixFormat: Sendable, Equatable {
10 static let directiveRegex = try! NSRegularExpression(pattern: "%(\\?)?([-+]?[0-9.]*)([ .;,:!?=|/<>]?)([cltseibT]|\\(.+?\\))")
11
12 enum Piece: Sendable, Equatable {
13 case literal(String)
14 case field(key: Character, optional: Bool, width: String, punctuation: String)
15 }
16
17 let pieces: [Piece]
18 /// `org-prefix-has-time`: times move out of the heading into the prefix.
19 let hasTime: Bool
20 /// `.N` in the category width: categories this long or longer are cut to N - 1.
21 let categoryMaxLength: Int?
22
23 public init(_ format: String) {
24 let regex = Self.directiveRegex
25 let ns = format as NSString
26 var pieces: [Piece] = []
27 var hasTime = false
28 var maxLength: Int?
29 var at = 0
30 for m in regex.matches(in: format, range: NSRange(location: 0, length: ns.length)) {
31 if m.range.location > at { pieces.append(.literal(ns.substring(with: NSRange(location: at, length: m.range.location - at)))) }
32 let key = ns.substring(with: m.range(at: 4))
33 let width = ns.substring(with: m.range(at: 2))
34 if key.hasPrefix("(") {
35 pieces.append(.field(key: "(", optional: false, width: width, punctuation: ""))
36 } else {
37 let k = key.first!
38 if k == "t" { hasTime = true }
39 if k == "c", let dot = width.firstIndex(of: ".") { maxLength = Int(width[width.index(after: dot)...].prefix { $0.isNumber }) }
40 pieces.append(.field(key: k, optional: m.range(at: 1).location != NSNotFound, width: width, punctuation: ns.substring(with: m.range(at: 3))))
41 }
42 at = NSMaxRange(m.range)
43 }
44 if at < ns.length { pieces.append(.literal(ns.substring(from: at))) }
45 self.pieces = pieces
46 self.hasTime = hasTime
47 self.categoryMaxLength = maxLength
48 }
49
50 /// The prefix for these values, by field key.
51 func render(_ values: [Character: String]) -> String {
52 var out = ""
53 for piece in pieces {
54 switch piece {
55 case .literal(let s): out += s
56 case .field(let key, let optional, let width, let punctuation):
57 var value = values[key] ?? ""
58 if key == "c", let max = categoryMaxLength, value.count >= max { value = String(value.prefix(max - 1)) }
59 if value.isEmpty {
60 out += optional ? "" : Self.format("", width)
61 } else {
62 out += Self.format(value + punctuation, width)
63 }
64 }
65 }
66 return out
67 }
68
69 /// Emacs `format` with `%WIDTHs` or `%-WIDTH.PRECISIONs`.
70 static func format(_ s: String, _ spec: String) -> String {
71 var spec = Substring(spec)
72 let left = spec.first == "-"
73 if left || spec.first == "+" { spec = spec.dropFirst() }
74 let parts = spec.split(separator: ".", omittingEmptySubsequences: false)
75 let width = Int(parts.first ?? "") ?? 0
76 var value = s
77 if parts.count > 1, let precision = Int(parts[1]) { value = String(value.prefix(precision)) }
78 let padding = String(repeating: " ", count: max(0, width - displayWidth(value)))
79 return left ? value + padding : padding + value
80 }
81
82 public static let defaults: [String: String] = [
83 "agenda": " %i %-12:c%?-12t% s", "todo": " %i %-12:c", "tags": " %i %-12:c", "search": " %i %-12:c",
84 ]
85}