Cotton code
46 lines · 2.3 KB · SwiftRaw
// A choice column's words: a stored choice as it reads, what a tap on a word// writes, the words offered and the lines under them.import CottonKitimport CottonValuesenum ChoiceWords {    /// A stored choice as it reads: the key `waiting_on` → `Waiting on`. A    /// value that carries capitals of its own is already written, so its case    /// is never changed — `iPhone` is never `IPhone`.    static func label(_ choice: String) -> String {        let spaced = choice.replacingOccurrences(of: "_", with: " ")        guard !choice.contains(where: \.isUppercase) else { return spaced }        return spaced.prefix(1).uppercased() + spaced.dropFirst()    }    /// A choice as its column names it ("3 months" for `days:90`), else as    /// `label` reads it.    static func label(_ choice: String, in column: ColumnDefinition) -> String {        column.labels?[choice] ?? column.choiceNames?[choice] ?? label(choice)    }    /// The words offered: every choice, or, for a column with `moves`, the    /// chosen one and those it may move to. A value the moves do not name    /// (none yet, or one from a newer build) is offered every choice.    static func offered(_ column: ColumnDefinition, chosen: String?) -> [String] {        let choices = column.choices ?? []        guard let chosen, let next = column.moves?[chosen] else { return choices }        return choices.filter { $0 == chosen || next.contains($0) }    }    /// The lines under the words: each offered move's hint ("Paid: Also    /// records the money"); where nothing follows, the chosen value's hint, or    /// that it is where the record ends.    static func notes(_ column: ColumnDefinition, chosen: String?) -> [String] {        // Why the greyed words are greyed ("Never, 1 month: after the next update").        if let note = column.unavailableNote, column.unavailable?.isEmpty == false { return [note] }        guard let moves = column.moves, let chosen, let next = moves[chosen] else { return [] }        if next.isEmpty {            return [column.hints?[chosen] ?? ShellLines.line("editing", "value.is_where_this_ends", ["chosen": "\(label(chosen))"])]        }        return offered(column, chosen: chosen).filter { $0 != chosen }.compactMap { word in            column.hints?[word].map { "\(label(word)): \($0)" }        }    }}