Cotton code
84 lines · 3.4 KB · SwiftRaw
// Asked of you, as slips of dyed paper — the model under the band.//// The picture (05-home) draws each question that waits on the owner as a torn// slip: the count and what it counts, typed, one row of three. What a slip SAYS// is the core's (`shell.today`'s `parts.asked`),// where it goes is the row's own path, and the one thing decided here is the// paper: which dye, and how the slips break into rows.//// THE DYE BELONGS TO THE QUEUE, not to the position. A queue keeps its colour// when the one before it empties — questions are always the sage slip, invites// the blue one, locked documents the peach one — so the eye learns the slips// the way it learns the caps. A total `switch`, so a new queue is a compile// error rather than a slip with no paper.//// Pure, so `DecideSlipsTests` pins it without a renderer.import CottonKitimport Foundationpublic struct DecideSlip: Sendable, Equatable, Identifiable {    public let key: AskedKey    public let label: String    public let path: String    public let dye: PaperDye    /// The database the slip's row counts in (`AskedItem.database`).    public var database: FeedRowDatabase? = nil    /// One key in two databases is two slips.    public var id: String { FeedRowDatabase.key(database, id: key.rawValue) }    /// Where the slip's tap opens: its path, in its own database.    public var link: DatabaseLink? {        Route.parse(path).map { FeedRowDatabase.link(database, route: $0) }    }}public enum DecideSlips {    /// The band's slips, in the band's own order — the items are already the    /// crate's order, and nothing here re-sorts them.    public static func slips(_ items: [AskedItem]) -> [DecideSlip] {        items.map {            DecideSlip(key: $0.key, label: $0.label, path: $0.path, dye: dye($0.key), database: $0.database)        }    }    /// The slips broken into as few rows as `columns` (the theme's    /// `board_asked_band_columns`: three is what the picture lays, and what a    /// phone's width holds with the longest sentence still wrapping to three    /// typed lines) allows, and BALANCED    /// across them: four slips are two and two, never three and an orphan    /// under them; seven are three, two and two. A longer row always comes    /// first, and no row is more than one slip longer than another.    public static func rows(_ slips: [DecideSlip], columns: Int) -> [[DecideSlip]] {        guard !slips.isEmpty, columns > 0 else { return [] }        let rowCount = (slips.count + columns - 1) / columns        let base = slips.count / rowCount        let longer = slips.count % rowCount        var rows: [[DecideSlip]] = []        var start = 0        for index in 0..<rowCount {            let length = base + (index < longer ? 1 : 0)            rows.append(Array(slips[start..<start + length]))            start += length        }        return rows    }    /// How many cells a row of `rows` is laid across — the first (longest)    /// row's count, so a shorter row keeps its slips that width.    public static func width(of rows: [[DecideSlip]]) -> Int {        rows.first?.count ?? 0    }    /// Each queue's paper.    public static func dye(_ key: AskedKey) -> PaperDye {        switch key {        case .joinRequests: return .pink        case .assistantsAsked: return .sage        case .invites: return .blue        case .passwords: return .peach        case .aiAccess: return .cream        }    }}