fix(menubar): budget the second row by the width it draws

The 24-unit budget was a Character count, so "GitHub Copilot 剩余 12% · 6 小时
2 分" fitted it at 24 Characters while drawing 31 cells: a third wider than
the first row it is not allowed to exceed. abbreviate now measures East Asian
wide and fullwidth scalars as two. All-Latin rows are unchanged.
This commit is contained in:
iamtoruk 2026-09-12 16:12:11 -07:00
parent 82b02faa4f
commit 20f151faa4
2 changed files with 56 additions and 9 deletions

View file

@ -243,11 +243,11 @@ enum MenubarRowFormatter {
figures += " · \(countdown)"
}
guard !quota.label.isEmpty else { return figures }
let label = abbreviate(quota.label, to: secondRowCharacterBudget - figures.count - 1)
let label = abbreviate(quota.label, to: secondRowCharacterBudget - displayCells(figures) - 1)
return label.isEmpty ? figures : "\(label) \(figures)"
}
/// How wide the second row may get, in characters.
/// How wide the second row may get, in display cells (`displayCells(_:)`).
///
/// The status item is variable width, so the widest line wins; an unbounded
/// second row ("GitHub Copilot 12% left · 6d 3h") made the item more than
@ -260,13 +260,42 @@ enum MenubarRowFormatter {
/// past what the first row alone could already occupy.
static let secondRowCharacterBudget = 24
/// Shortens `text` to `limit` characters, marking the cut with an ellipsis.
/// Returns "" when there is no room for even one character plus the mark, so
/// the caller can drop the part entirely rather than render a bare "…".
/// Display cells `text` occupies. A Han, kana or Hangul glyph is about twice
/// as wide as a Latin one, so counting Characters lets a translated row
/// overrun the very width the budget exists to hold: `6 小时 2 分` is 8
/// Characters and 11 cells.
///
/// ponytail: the East Asian Wide/Fullwidth blocks the catalog can actually
/// contain, not the whole of UAX #11. Widen the ranges if a locale outside
/// them ships.
static func displayCells(_ text: String) -> Int {
text.unicodeScalars.reduce(0) { total, scalar in
switch scalar.value {
case 0x1100...0x115F, 0x2E80...0xA4CF, 0xAC00...0xD7A3,
0xF900...0xFAFF, 0xFE30...0xFE6F, 0xFF00...0xFF60,
0xFFE0...0xFFE6, 0x20000...0x3FFFD:
return total + 2
default:
return total + 1
}
}
}
/// Shortens `text` to `limit` display cells, marking the cut with an
/// ellipsis. Returns "" when there is no room for even one character plus
/// the mark, so the caller can drop the part entirely rather than render a
/// bare "…". Identical to a character count for an all-Latin row.
static func abbreviate(_ text: String, to limit: Int) -> String {
guard text.count > limit else { return text }
guard displayCells(text) > limit else { return text }
guard limit >= 2 else { return "" }
var kept = String(text.prefix(limit - 1))
var kept = ""
var used = 0
for character in text {
let width = displayCells(String(character))
if used + width > limit - 1 { break }
kept.append(character)
used += width
}
while kept.last == " " { kept.removeLast() }
return kept.isEmpty ? "" : kept + "…"
}

View file

@ -377,7 +377,7 @@ struct MenubarSecondRowTests {
)
// Both figures survive; the name is what gives way.
#expect(row == "GitHub… 12% left · 6d 3h")
#expect(row?.count == MenubarRowFormatter.secondRowCharacterBudget)
#expect(MenubarRowFormatter.displayCells(row ?? "") == MenubarRowFormatter.secondRowCharacterBudget)
}
@Test("no metric can produce a row past the budget")
@ -397,7 +397,7 @@ struct MenubarSecondRowTests {
for metric in MenubarSecondRowMetric.allCases {
let row = MenubarRowFormatter.secondRow(settings: settings(true, metric), snapshot: wide, now: now)
#expect(row != nil)
#expect((row?.count ?? 0) <= MenubarRowFormatter.secondRowCharacterBudget)
#expect(MenubarRowFormatter.displayCells(row ?? "") <= MenubarRowFormatter.secondRowCharacterBudget)
}
}
@ -415,6 +415,24 @@ struct MenubarSecondRowTests {
#expect(MenubarRowFormatter.clampToRowBudget("12 sess") == "12 sess")
}
@Test("a wide-glyph row is budgeted by the width it draws, not its character count")
func wideGlyphsCountDouble() {
// "6 小时 2 分" is 8 Characters and 11 cells. Counting Characters let a
// zh-Hans row draw about a third wider than the budget allows.
#expect(MenubarRowFormatter.displayCells("6 小时 2 分") == 11)
#expect(MenubarRowFormatter.displayCells("GitHub Copilot") == 14)
// An all-Latin row is unchanged: cells and characters agree.
#expect(MenubarRowFormatter.abbreviate("GitHub Copilot", to: 8) == "GitHub…")
// Two wide glyphs already fill four cells, so a five-cell limit keeps
// one of them plus the mark.
#expect(MenubarRowFormatter.abbreviate("剩余配额跟踪", to: 5) == "剩余…")
#expect(
MenubarRowFormatter.displayCells(
MenubarRowFormatter.clampToRowBudget("GitHub Copilot 剩余 12% · 6 小时 2 分")
) <= MenubarRowFormatter.secondRowCharacterBudget
)
}
@Test("an unlabelled provider keeps the full figures")
func unlabelledQuotaRow() {
#expect(