diff options
| author | Tommaso Negri <me@tommasongr.com> | 2026-08-08 13:07:10 +0200 |
|---|---|---|
| committer | Tommaso Negri <me@tommasongr.com> | 2026-08-08 13:07:10 +0200 |
| commit | 8d1e111f074d961a7ca2d33c7242814058380e3f (patch) | |
| tree | c4a6a9a887dfa46eee267e5eed6762a29f39f5bf /Sources/phbar | |
| parent | 618799ee703cc4addc7e50550ccf87b4b4cc7780 (diff) | |
| download | bar-8d1e111f074d961a7ca2d33c7242814058380e3f.tar.gz bar-8d1e111f074d961a7ca2d33c7242814058380e3f.zip | |
allow multiple highlight levels
Diffstat (limited to 'Sources/phbar')
| -rw-r--r-- | Sources/phbar/Config.swift | 4 | ||||
| -rw-r--r-- | Sources/phbar/Models/Cell.swift | 58 |
2 files changed, 43 insertions, 19 deletions
diff --git a/Sources/phbar/Config.swift b/Sources/phbar/Config.swift index 746aa7e..6244578 100644 --- a/Sources/phbar/Config.swift +++ b/Sources/phbar/Config.swift @@ -23,8 +23,8 @@ enum Config { /// The color used for the bar background. static let background: Color = .hex("#010408").opacity(0.825) - /// The color used to highlight a cell inside a block (see ``Cell``). - static let highlight: Color = .hex("#0f304a") + /// The colors used to highlight a cell at different levels (see ``Cell``). + static let highlights: [Color] = [.hex("#0f304a"), .hex("#1f689d")] /// The blocks that make up the bar, listed in display order. /// diff --git a/Sources/phbar/Models/Cell.swift b/Sources/phbar/Models/Cell.swift index 789f615..0ad8e51 100644 --- a/Sources/phbar/Models/Cell.swift +++ b/Sources/phbar/Models/Cell.swift @@ -1,4 +1,5 @@ import Foundation +import RegexBuilder import SwiftUI /// A thin wrapper around a single text line captured @@ -9,17 +10,19 @@ import SwiftUI /// laid out horizontally respecting the order. /// /// Besides the underlying text value, a cell holds an -/// highlighted status that, if true, applies the special -/// background color defined in `Config.swift`. +/// highlight level (as Int) that, if true, applies the +/// special background color defined in `Config.swift`. /// You can set the highlight at runtime by prefixing the -/// text line with the special notation `!!`. +/// text line with the special notation `&!<level>`, where +/// `<level>` is a positive integer that can be used as an +/// index to pick an highlight (count starts from 1). /// -/// E.g: `echo "!! $(date "+%a %d, %H:%M:%S") "` +/// E.g: `echo "&!1 $(date "+%a %d, %H:%M:%S") "` /// -/// Highlight can have multiple use cases, but it was -/// imagined specifically for distinguishing a single cell -/// within a group. For example when you want to show which -/// workspace is currently active. +/// Highlight colors can have multiple use cases, but they +/// were imagined specifically for distinguishing cells +/// within a group. For example when you want to show +/// window manager workspaces and which one is active. struct Cell: RawRepresentable, Identifiable { /// Stable identity for SwiftUI diffing. let id = UUID() @@ -27,20 +30,41 @@ struct Cell: RawRepresentable, Identifiable { /// The represented text content. let rawValue: String - /// Whether the cell is highlighted or not. - let highlighted: Bool + /// The highlight level of the cell. + let highlight: Int - /// The background color derived from the highlighted state. + /// The background color derived from the highlight level. /// - /// Returns `Config.highlight` when the cell is highlighted, - /// otherwise `.clear` so the underlying bar background - /// shows through. + /// Returns the `Config.highlights` color at the same index + /// if present, otherwise `.clear` so the underlying bar + /// background shows through. var background: Color { - highlighted ? Config.highlight : .clear + guard highlight != 0 && highlight <= Config.highlights.count else { return .clear } + + return Config.highlights[highlight - 1] } init(rawValue: String) { - self.highlighted = rawValue.hasPrefix("!!") - self.rawValue = highlighted ? String(rawValue.dropFirst(2)) : rawValue + let pattern = Regex { + Optionally { + "&!" + Capture { OneOrMore(.digit) } + } + Capture { OneOrMore(.any) } + } + + guard let match = rawValue.firstMatch(of: pattern) else { + self.rawValue = rawValue + self.highlight = 0 + return + } + + if let rawHighlight = match.output.1, let highlight = Int(String(rawHighlight)) { + self.highlight = highlight + } else { + self.highlight = 0 + } + + self.rawValue = String(match.output.2) } } |
