aboutsummaryrefslogtreecommitdiff
path: root/Sources
diff options
context:
space:
mode:
Diffstat (limited to 'Sources')
-rw-r--r--Sources/bar/Models/Cell.swift47
-rw-r--r--Sources/bar/bar.docc/highlighting.md12
2 files changed, 25 insertions, 34 deletions
diff --git a/Sources/bar/Models/Cell.swift b/Sources/bar/Models/Cell.swift
index b7911b5..0ae324b 100644
--- a/Sources/bar/Models/Cell.swift
+++ b/Sources/bar/Models/Cell.swift
@@ -1,6 +1,4 @@
-import RegexBuilder
import SwiftUI
-import Synchronization
/// A thin wrapper around a single text line captured
/// by a block.
@@ -28,15 +26,6 @@ import Synchronization
/// within a group. For example when you want to show
/// window manager workspaces and which one is active.
struct Cell: RawRepresentable, Identifiable {
- /// Build the parser once rather than on every refresh of every cell.
- private static let pattern = Mutex(Regex {
- Optionally {
- "&!"
- Capture { OneOrMore(.digit) }
- }
- Capture { OneOrMore(.any) }
- })
-
/// A content-derived identifier combining text and highlight.
///
/// Identical cells share this value and content changes change it.
@@ -51,26 +40,26 @@ struct Cell: RawRepresentable, Identifiable {
var background: Color
init(rawValue: String) {
- guard let match = Self.pattern.withLock({ rawValue.firstMatch(of: $0) }) else {
- self.rawValue = rawValue
- self.background = .clear
- self.id = "\(rawValue):0"
- return
- }
+ var text = rawValue
+ var level = 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
+ if rawValue.hasPrefix("&!") {
+ let suffix = rawValue.dropFirst(2)
+ // Consume every ASCII digit before interpreting the level.
+ let digits = suffix.prefix { character in
+ character.unicodeScalars.count == 1 && character >= "0" && character <= "9"
+ }
+ let body = suffix.dropFirst(digits.count)
+ if !digits.isEmpty && !body.isEmpty {
+ text = String(body)
+ if let parsed = Int(digits), parsed > 0, parsed <= Config.highlights.count {
+ level = parsed
+ }
+ }
}
- self.rawValue = String(match.output.2)
- self.background = Config.highlights[highlight - 1]
- self.id = "\(self.rawValue):\(highlight)"
+ self.rawValue = text
+ self.background = level > 0 ? Config.highlights[level - 1] : .clear
+ self.id = "\(text):\(level)"
}
}
diff --git a/Sources/bar/bar.docc/highlighting.md b/Sources/bar/bar.docc/highlighting.md
index 8075031..dc318ec 100644
--- a/Sources/bar/bar.docc/highlighting.md
+++ b/Sources/bar/bar.docc/highlighting.md
@@ -34,9 +34,11 @@ echo "&!2 workspace 2" # highlighted with Config.highlights[1]
echo "workspace 3" # no highlight
```
-> Important: A parsed `&!<level>` prefix followed by text is stripped before rendering, so only the text after it is displayed.
-> For example, `&!1 workspace 1` renders as `workspace 1` on a highlighted background.
+> Important: Only a prefix at the start of the line is recognized. Its level uses ASCII digits (`0`–`9`), and the entire digit sequence is consumed. A nonempty body must follow it. Whitespace counts as a body and is preserved exactly: `&!1 workspace 1` renders as ` workspace 1`, including the leading space.
-> Important: The highlight level is a positive integer that indexes into ``Config-swift.enum/highlights`` (1-based).
-> For a parsed prefix followed by text, zero, an out-of-range level, or a number too large to represent as an Int leaves the cell unhighlighted and removes the prefix.
-> Use a level followed by text as shown above: malformed or textless prefixes are not guaranteed to be stripped by the current parser.
+The highlight level is a positive integer that indexes into ``Config-swift.enum/highlights`` (1-based).
+With a nonempty body, zero, an out-of-range level, or an integer overflow removes the prefix but leaves the body unhighlighted.
+Leading zeroes are allowed (`&!01 text` selects level 1).
+
+Empty input, textless prefixes such as `&!1` and `&!12`, and malformed prefixes such as `&!`, `&!-1 text`, or `&!1 text` remain literal and unhighlighted.
+Embedded prefixes also remain literal, as in `text &!1 more`.