// Keys a list and its slips read so that a switch reads as a switch, and an // empty value or an empty list still says something: the slip's toggle drawn // as a switch (`toggle_style`), a column's word for nothing (`placeholder`), a // several-choice column of the week's days (`week`), a list's own empty words // (`empty`) and its "+" for a blank page (`create`). Each is drawn once, for // any table that names it (Alarms was the first: the time large, the name // under it — "Alarm" in pencil when it has none — the days, "Once" when there // are none, and the switch at the edge, faint lettering when it is off). // // The reading lives here, pure, so the words are settled by tests; // `SlipFace` and `RecordListSurface` only draw them. import CottonKit import CottonValues import Foundation /// How a slip draws its toggle. public typealias SlipToggleStyle = DefinitionToggleStyle /// Where the list's one input writes when it is not the list's own table: /// what is typed on this list makes a record of `table`, and the empty field /// says `placeholder`. Nobody types a new account on the Accounts list — they /// say what moved, and that is a transaction. /// /// With `link` (a link column of `table`), a record's PAGE captures too: what /// is typed there makes a record of `table` holding the page's record in that /// column (an entry on its ledger's page). public typealias ListCapture = DefinitionListCapture extension DefinitionListCapture { public init(table: String, placeholder: String? = nil, link: String? = nil, when: SectionWhen? = nil, pageOnly: Bool = false) { self.init(_generated: (), table: table) self.placeholder = placeholder self.link = link self.when = when self.pageOnly = pageOnly } } /// The list's "+": the words it is read as ("New alarm"), opening a blank page /// for a new record — the button an empty list offers too. public typealias ListCreate = DefinitionListCreate extension DefinitionListCreate { public init(label: String, dated: String? = nil, path: String? = nil) { self.init(_generated: (), label: label) self.dated = dated self.path = path } } extension TableDefinition { /// How strongly a switch slip's lettering prints when it is off: still /// the owner's, and plainly not working. public static let offStrength: Double = 0.42 /// Whether the slip draws a switch rather than the check. public var drawsSwitch: Bool { slip.toggleStyle == .switch && column(slip.toggle) != nil } /// Whether a slip's words are struck through: a check that is on (a done /// todo, a hidden tool). A switch never strikes — on is the record at work. public func strikes(_ record: TableRecord) -> Bool { guard slip.toggleStyle == .check else { return false } return record.said?.toggled ?? false } /// A slip slot's words as the slip reads them, else the column's /// placeholder; `placeholder` says which, so a face can draw it in pencil. /// Nil when the column holds nothing (or only spaces) and has no placeholder. public func slipWords( _ key: String?, of record: TableRecord, links: [String: String] = [:] ) -> (text: String, placeholder: Bool)? { let column = column(key) // In the row's own currency, as the slip prints it: VoiceOver said // "₹200.00" for a card printing "$200.00" (walker294 row 55). if let text = FaceText.slipLine(record[key], words: record.columnWords(key), column: column, links: links, currency: record[column?.currency].text, readsPlaceholder: false), !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { return (text, false) } return column?.placeholder.map { ($0, true) } } /// What VoiceOver says for a slip that reads several lines: each slot's /// words, a week's days spelled out ("6:30 am, Wake, Monday, Friday"). public func spoken(_ record: TableRecord, links: [String: String] = [:]) -> String { [slip.title, slip.subtitle, slip.trailing].compactMap { key -> String? in guard let column = column(key) else { return nil } if column.week, let days = FaceText.weekSpoken(record[column.key], column: column) { return days } // The title is said as the slip draws it: a mood with no words of // its own is headed by its day, and VoiceOver said "Happy" for a // card that read "Friday" (walker294 row 77). if key == slip.title, let day = record.said?.slip.weekday { return day } let words = slipWords(key, of: record, links: links)?.text // A long text is said by its first line, as the slip shows it — // never a whole note's body read out (walker 604bcce6). guard column.type == .longtext else { return words } return words.flatMap { $0.split(whereSeparator: \.isNewline).first.map(String.init) } } // A title taken from the body's first line is said once. .reduce(into: [String]()) { said, words in if said.last != words { said.append(words) } } .joined(separator: ", ") } } extension FaceText { /// The days a `week` column holds, by their index in its choices /// (Monday first): a choice word as kept or spelled out ("Monday"), or the /// day's number 1–7 (ISO, Monday 1) as an engine keeps it. Sorted, once each; anything else is /// left out. static func weekDays(_ value: RecordValue, column: ColumnDefinition) -> [Int] { let words = column.choices ?? [] let items: [RecordValue] switch value { case .list(let list): items = list default: items = choices(value).map(RecordValue.text) } let days = items.compactMap { item -> Int? in if let n = item.number, n == n.rounded(), (1...7).contains(Int(n)) { return Int(n) - 1 } guard let text = item.text else { return nil } if let n = Int(text), (1...7).contains(n) { return n - 1 } let word = text.trimmingCharacters(in: .whitespaces).lowercased() // The word as kept ("Mon"), else a longer spelling of it // ("monday", "Tues") — an engine or an assistant may write either. return words.firstIndex { $0.lowercased() == word } ?? (word.count >= 3 ? words.firstIndex { !$0.isEmpty && word.hasPrefix($0.lowercased()) } : nil) } return Set(days.filter { words.indices.contains($0) }).sorted() } /// A `week` column as a slip reads it — the old alarm card's rules, in /// order: all seven are "Every day"; ONE unbroken run of three to six is a /// range with an en dash ("Mon\u{2013}Fri", "Fri\u{2013}Sun"); anything else /// is the days in week order, spaced ("Tue Thu Sat", "Sat Sun"). The week /// does not wrap past Sunday. Nil for no day (the column's placeholder /// says what that means). public static func week(_ value: RecordValue, column: ColumnDefinition) -> String? { let words = column.choices ?? [] let days = weekDays(value, column: column) guard let first = days.first, let last = days.last else { return nil } if days.count == words.count, words.count == 7 { return ShellLines.line("definitions", "switch.every_day") } if days.count >= 3, last - first == days.count - 1 { return "\(words[first])\u{2013}\(words[last])" } return days.map { words[$0] }.joined(separator: " ") } /// A `week` column spelled out for VoiceOver ("Monday, Tuesday") — three /// letters are ambiguous read aloud; nil for no day. public static func weekSpoken(_ value: RecordValue, column: ColumnDefinition) -> String? { let names = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"].map { ShellLines.line("dates", "weekday_names.\($0)") } let days = weekDays(value, column: column).filter { names.indices.contains($0) } return days.isEmpty ? column.placeholder?.lowercased() : days.map { names[$0] }.joined(separator: ", ") } /// A time as the core said it ("6:30 am"), cut into the figures and the /// meridiem a face prints small beside them. Nil for no words. public static func clockParts(_ words: String?) -> (digits: String, meridiem: String?)? { guard let words, !words.isEmpty else { return nil } let parts = words.split(separator: " ", maxSplits: 1).map(String.init) return parts.count == 2 ? (parts[0], parts[1]) : (words, nil) } }