diff options
| author | Tommaso Negri <me@tommasongr.com> | 2026-08-12 14:56:16 +0200 |
|---|---|---|
| committer | Tommaso Negri <me@tommasongr.com> | 2026-08-12 14:56:16 +0200 |
| commit | 8a2301a69a6f7bf46d7af92a3126dd9fab20d88d (patch) | |
| tree | 398d00614c2f9819fe950aa20c7241a6553c833d /Sources | |
| parent | 9fc36c60439cc9d3f0094be1183e2f511a568a87 (diff) | |
| download | bar-8a2301a69a6f7bf46d7af92a3126dd9fab20d88d.tar.gz bar-8a2301a69a6f7bf46d7af92a3126dd9fab20d88d.zip | |
avoid background computed property in cells
Diffstat (limited to 'Sources')
| -rw-r--r-- | Sources/bar/Models/Cell.swift | 33 |
1 files changed, 14 insertions, 19 deletions
diff --git a/Sources/bar/Models/Cell.swift b/Sources/bar/Models/Cell.swift index 48798ba..a09d1f9 100644 --- a/Sources/bar/Models/Cell.swift +++ b/Sources/bar/Models/Cell.swift @@ -38,19 +38,8 @@ struct Cell: RawRepresentable, Identifiable { /// The represented text content. let rawValue: String - /// The highlight level of the cell. - let highlight: Int - - /// The background color derived from the highlight level. - /// - /// Returns the `Config.highlights` color at the same index - /// if present, otherwise `.clear` so the underlying bar - /// background shows through. - var background: Color { - guard highlight != 0 && highlight <= Config.highlights.count else { return .clear } - - return Config.highlights[highlight - 1] - } + /// The background color of the cell derived from the highlight level. + var background: Color init(rawValue: String) { let pattern = Regex { @@ -63,18 +52,24 @@ struct Cell: RawRepresentable, Identifiable { guard let match = rawValue.firstMatch(of: pattern) else { self.rawValue = rawValue - self.highlight = 0 + self.background = .clear self.id = "\(rawValue):0" return } - if let rawHighlight = match.output.1, let highlight = Int(String(rawHighlight)) { - self.highlight = highlight - } else { - self.highlight = 0 + guard let rawHighlight = match.output.1, + let highlight = Int(String(rawHighlight)), + highlight > 0, + highlight <= Config.highlights.count + else { + self.rawValue = String(match.output.2) + self.background = .clear + self.id = "\(self.rawValue):0" + return } self.rawValue = String(match.output.2) - self.id = "\(self.rawValue):\(self.highlight)" + self.background = Config.highlights[highlight - 1] + self.id = "\(self.rawValue):\(highlight)" } } |
