// The app's own voice — what it says when no record speaks — as the core // resolved it (`shell.lines`, `crates/cotton-ffi/src/methods/shell_lines.rs`). // // THE SHELL'S LINES ARE ROWS (architect, 21 Oct 2026): every such sentence is // a row of the shown database's `lines` table, the kit's words for one it has // none of. The core resolves each set once per database and again after a // sync (`ShellLines.refresh`, CottonKit); a screen reads the set this process // last received. // // Before the core has answered (a refusal, a first join, an extension with // no core, a face drawn in Loom), or for a set an older core does not ship, a // line is the kit's copy snapshot bundled here (`Resources/lines/.json`, // every kit line file copied byte for byte, held so by // `the_apps_line_snapshot_is_the_kits_sets`) // — never a literal written here, and never a line file this code opens // itself. It lives in CottonValues, with no core, so a face says its words // the same in the app and in Loom (Loom, 28 Oct 2026), and the Mac's host // code reads the same lines through CottonKit. No composition: a line is its // template with its `{placeholders}` filled by typed values. import Foundation public enum ShellLines { // No list of set names lives here: the sets are the kit's files, and the // core names them (`SHIPPED`) — the app reads every set in one call and // keeps what comes, and the bundled sets are whatever the snapshot holds. A list here was a copy of the kit's file list, and the guard // `NoTableInGenericSurfacesTests` read "bin" and "backup" in it as tables. /// One set: each line's words by its dotted path. typealias Lines = [String: String] /// Each set as the core last resolved it, by set id. private static let received = Received() /// Whose words these are: this app's name, as its manifest /// (`flavour.json`) says it, read once. A test sets a business's to read /// the lines as its customer would. nonisolated(unsafe) public static var speaker: String? = bundledName(of: .main) /// Each set as the bundled snapshot has it, read once: every file in the /// bundle's `lines` folder is one set, named by its file. private static let bundled: [String: Lines] = { let urls = CottonValuesResources.bundle?.urls(forResourcesWithExtension: "json", subdirectory: "lines") ?? [] var sets: [String: Lines] = [:] for url in urls { guard let data = try? BundledJSON.read(url), let set = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any] else { continue } sets[url.deletingPathExtension().lastPathComponent] = flat(set) } return sets }() /// The line at dotted `path` of `set`, its `{name}`s replaced by `fills`. /// A line neither the core nor the bundled kit has is said as its own /// path, so a missing line shows where it is missing rather than nothing. public static func line(_ set: String, _ path: String, _ fills: [String: String] = [:]) -> String { shipped(set, path, fills) ?? "\(set).\(path)" } /// The line at `path` of `set`, or nil when neither the core nor the /// bundled kit has it: for a path chosen at run time (a refusal's code), /// whose reader says its general line instead of a dotted path. public static func shipped(_ set: String, _ path: String, _ fills: [String: String] = [:]) -> String? { let fromCore = received.set(set) guard let template = fromCore?[path] ?? bundled[set]?[path] else { return nil } // Said in this app's name: a business flavour never says Cotton. let filled = fills.reduce(template) { $0.replacingOccurrences(of: "{\($1.key)}", with: $1.value) } return said(filled, as: speaker) } /// The immediate line keys beneath a path, in their data order by key. public static func keys(_ set: String, _ path: String) -> [String] { let prefix = path + "." let lines = received.set(set) ?? bundled[set] ?? [:] return lines.keys.compactMap { key in guard key.hasPrefix(prefix) else { return nil } let child = String(key.dropFirst(prefix.count)) return child.contains(".") ? nil : child }.sorted() } /// The sets the core resolved (`shell.lines`'s `sets`), each kept in /// place of the one before it. public static func receive(_ sets: [String: Any]) { for (set, lines) in sets { guard let lines = lines as? [String: Any] else { continue } received.keep(set, flat(lines)) } } /// A sentence as an app named `name` says it: the word Cotton becomes the /// app's own name, so a business's customer never reads Cotton on a gate /// screen, in a refusal or in an error (lane phonewalk, 19 Oct 2026); /// Cotton's own build, or no name, says Cotton. public static func said(_ text: String, as name: String?) -> String { guard let name, name != "Cotton", text.contains("Cotton") else { return text } // An app whose own name starts with Cotton (Cotton Project) leaves // its name as it is, rather than saying "Cotton Project Project". let rest = name.hasPrefix("Cotton") ? String(name.dropFirst("Cotton".count)) : "" // "Cotton" then a capitalised word names a Cotton app (Cotton Voice, // Cotton Meetings): that one is never renamed. let pattern = #"\bCotton(DB for Mac)?\b(?! \p{Lu})"# + (rest.isEmpty ? "" : "(?!\(NSRegularExpression.escapedPattern(for: rest)))") return text.replacingOccurrences(of: pattern, with: name, options: .regularExpression) } /// The app's name as `bundle`'s manifest (`flavour.json`) says it, or nil /// when the bundle carries none (a test runner, Loom). static func bundledName(of bundle: Bundle) -> String? { guard let url = bundle.url(forResource: "flavour", withExtension: "json"), let data = try? BundledJSON.read(url), let manifest = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any] else { return nil } return manifest["name"] as? String } /// A line file's strings by dotted path (`banner.body.phone`). private static func flat(_ node: [String: Any], _ prefix: String = "") -> Lines { var lines: Lines = [:] for (key, value) in node { let path = prefix.isEmpty ? key : "\(prefix).\(key)" if let words = value as? String { lines[path] = words } else if let inner = value as? [String: Any] { lines.merge(flat(inner, path)) { $1 } } } return lines } /// The sets the core sent, behind a lock: read from any thread. private final class Received: @unchecked Sendable { private let lock = NSLock() private var sets: [String: Lines] = [:] func set(_ id: String) -> Lines? { lock.withLock { sets[id] } } func keep(_ id: String, _ lines: Lines) { lock.withLock { sets[id] = lines } } } } /// A file the app bundles, read with a bound: a packaging error is refused /// before a large file is buffered. CottonUI's `readBundledJSON` is this. public enum BundledJSON { public static func read(_ url: URL, limit: Int = 1_048_576) throws -> Data { let file = try FileHandle(forReadingFrom: url) defer { try? file.close() } let data = try file.read(upToCount: limit + 1) ?? Data() guard data.count <= limit else { throw CocoaError(.fileReadTooLarge) } return data } } /// CottonValues' resource bundle: `Bundle.module` on the phone; on the Mac it /// is looked for by path, as `CottonResources` finds the look's — a packaged /// app's `Contents/Resources/` first, then beside the executable and up to /// three directories above it (`swift build`, `swift test`). enum CottonValuesResources { static let bundleName = "Loom_CottonValues.bundle" static let bundle: Bundle? = { #if canImport(UIKit) return Bundle.module #else return locate(from: Bundle.main) ?? locate(from: Bundle(for: Marker.self)) #endif }() private final class Marker {} static func locate(from main: Bundle) -> Bundle? { var candidates: [URL] = [] if let resources = main.resourceURL { candidates.append(resources.appendingPathComponent(bundleName)) } var directory = main.bundleURL for _ in 0..<4 { candidates.append(directory.appendingPathComponent(bundleName)) directory.deleteLastPathComponent() } for candidate in candidates where FileManager.default.fileExists(atPath: candidate.path) { if let found = Bundle(url: candidate) { return found } } return nil } }