Cotton code
193 lines · 8.0 KB · SwiftRaw
// Keys a long list of things the owner keeps reads, each drawn once for any// table that names it (Photos was the first: the library's "all · photos ·// videos", its months counted "40 photos · 2 videos" with "No date" last, the// Select that deletes several pictures after one question, the menu's// Albums, Duplicates and People to confirm with their counts and the red "3// to review", and an album the owner said "not this" to never listed):////   * `filter` — a row of typed words over the list, one per value of one//     column, narrowing the list through its `where`;//   * `count_words` — how a count of records is said, by one column's value;//   * `months` — `by_month` drawn however short the list, the undated last//     under their own heading;//   * `hidden` — records never listed;//   * `select` and `deletion` — the list's Select, and the words Delete asks//     with (the page's too), the size it frees, and whether it can be undone;//   * `links` — other lists the list's menu opens, each with its count.//// The reading lives here, pure, so the words and the order are settled by// tests; `RecordListSurface` only draws them.import CottonKitimport CottonValuesimport Foundation/// A row of typed words narrowing a list by one column's value.public typealias ListFilter = DefinitionListFilterextension DefinitionListFilter {    public init(column: String, all: String, words: [String: String], order: [String]? = nil, empty: [String: EmptyWords] = [:], tabs: Bool = true) {        self.init(_generated: (), column: column)        self.tabs = tabs        self.all = all        self.words = words        self.order = order        self.empty = empty    }}/// How a count of records is said: each value of `column` with its own/// units ("1 photo", "2 videos"), joined; a value with no units is counted/// as the table's plural.public typealias CountWords = DefinitionCountWordsextension DefinitionCountWords {    public init(column: String, units: [String: ColumnUnits], order: [String]? = nil) {        self.init(_generated: (), column: column, units: units)        self.order = order    }}/// When `by_month` splits the list: over how many records (absent: a dozen,/// and only over more than one month), and the heading over the undated.public typealias ListMonthsLayout = DefinitionListMonthsextension DefinitionListMonths {    public init(over: Int? = nil, undated: String? = nil) {        self.init(_generated: ())        self.over = over        self.undated = undated    }}/// The list's Select: the menu offers it, ticks the records, and the trash/// deletes them after one question (`deletion`).public typealias ListSelect = DefinitionListSelectextension DefinitionListSelect {    public init(verb: DefinitionSelectVerb = .delete) {        self.init(_generated: ())        self.verb = verb    }}/// The words Delete asks with: the sentence under the question for one/// record and for several, the column whose bytes it frees, whether the/// receipt can take it back, and the two buttons.public typealias DeletionWords = DefinitionDeletionWordsextension DefinitionDeletionWords {    public init(        one: String? = nil, other: String? = nil, size: String? = nil, undo: Bool = true,        confirm: String = "Delete", cancel: String? = nil, label: String? = nil, asks: Bool = true, bin: Bool = false    ) {        self.init(_generated: ())        self.one = one        self.other = other        self.size = size        self.undo = undo        self.confirm = confirm        self.cancel = cancel        self.label = label        self.asks = asks        self.bin = bin    }}/// A line of a list's menu that opens an app screen, uncounted — the Tools/// screen's Settings (Spec 9.1).public typealias MenuLine = DefinitionMenuLineextension DefinitionMenuLine {    public init(label: String, path: String) { self.init(_generated: (), label: label, path: path) }}/// Another list the list opens ("Albums · 2 suggested"): the table it counts,/// narrowed by `where` (a value, or an array of values meaning any of them)./// With a `path` (an app route) it is a line of the list's menu. With none it/// is a LIST LINK: drawn on the list itself — alone under the list's records/// when it `lead`s, else in the row of links after its sections — and it opens/// the table's own list narrowed by `where` (`FilteredListOpening`). A table/// this build has no definition for, or a count of none where `hides_empty`,/// is not offered./// Where a list's row of link tiles stands: after its first `after` records.public typealias LinkTiles = DefinitionLinkTilesextension DefinitionLinkTiles {    public init(after: Int) { self.init(_generated: (), after: after) }}public typealias ListLink = DefinitionListLinkextension DefinitionListLink {    public init(        label: String, table: String, where filter: [String: RecordValue] = [:], words: String? = nil, path: String? = nil,        lead: Bool = false, above: Bool = false, review: Bool = false, hidesEmpty: Bool = false    ) {        self.init(_generated: (), label: label, table: table)        self.where = filter        self.words = words        self.path = path        self.lead = lead        self.above = above        self.review = review        self.hidesEmpty = hidesEmpty    }    public var isListLink: Bool { path == nil }    /// The link with its count, as the core says it: in the definition's    /// units when it has them, else `lists.link.count` ("Senders · 12").    @MainActor    public func line(count: Int?) -> String {        guard let count, count > 0 else { return label }        if let units { return CountLines.shared.units(units, n: count) ?? label }        if let words {            return CountLines.shared.line("lists", "link.count_words", n: count, fills: ["label": label, "words": words]) ?? label        }        return CountLines.shared.line("lists", "link.count", n: count, fills: ["label": label]) ?? label    }    public static func shows(count: Int?, takesAdd: Bool) -> Bool {        guard let count else { return false }        return count > 0 || takesAdd    }}extension TableDefinition {    /// Whether the table's list takes an add of its own — its bar writes a    /// record there, or it has a "+" — so a link to it is offered even while    /// it holds none (R12).    public var takesAdd: Bool { listTakesCapture || create != nil }    /// The values the filter row offers, in order, each with its word.    public var filterValues: [(value: String, word: String)] {        guard let filter else { return [] }        let order = filter.order ?? column(filter.column)?.choices ?? filter.words.keys.sorted()        return order.compactMap { value in filter.words[value].map { (value, $0) } }    }    /// Whether a record is never listed (`hidden`).    public func isHidden(_ record: TableRecord) -> Bool {        guard let hidden, let value = record[hidden.column].text else { return false }        return hidden.values.contains(value)    }    /// What this table's delete act is called — in the page's menu, a slip's    /// long press, Select and the question: its `deletion.label` ("Remove"),    /// else "Delete".    public var deleteWord: String { deletion?.label ?? "Delete" }    /// What the act did, said after it: "deleted" for Delete, "removed" for    /// any other word.    public var deletedWord: String { deleteWord == "Delete" ? "deleted" : "removed" }    /// What the list's Select says after deleting: "3 deleted", or, when some    /// were refused, "1 of 4 could not be deleted — still selected, so you    /// can try again."    public static func selectOutcome(deleted: Int, failed: Int, verb: String = ShellLines.line("definitions", "select.deleted")) -> String {        guard failed > 0 else { return ShellLines.line("definitions", "select.outcome", ["n": String(deleted), "verb": verb]) }        let total = deleted + failed        return ShellLines.line("definitions", "select.outcome_failed", ["failed": String(failed), "total": String(total), "verb": verb])    }}