aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Sources/phbar/Config.swift4
-rw-r--r--Sources/phbar/Models/Cell.swift58
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)
}
}