fix(render): SegmentMeter track uses hollow glyph so bargauge fill is legible without color

Filled and track cells both used the solid ■ glyph, encoding the fill
fraction in color alone. With ANSI stripped (--once piped to a non-TTY,
NO_COLOR, frames pasted into an issue) every bar collapsed to a uniform
full-width bar. Render the track with □ instead, matching the function's
documented output. Also fixes the gauge widget.

Fixes #1
This commit is contained in:
Aleks Petrov committed 2026-07-17 00:25:09 +02:00
1 parent 9285ec529b
commit 6fd5cf15be
2 files changed
+30 -2

No files matched your search

+7 -2
View File
@@ -86,9 +86,14 @@ func GradientMeter(frac float64, width int, stops []string, empty lipgloss.Style
// SegmentMeter renders a btop-style segmented meter: discrete square cells,
// filled portion swept through a color gradient, remainder in the track
// style (dark squares):
// style:
//
// ■■■■■■■■■■■■□□□□□
//
// The filled cells use a solid glyph (■) and the track uses a hollow one (□)
// so the fill fraction stays legible even when color is unavailable (piped
// output, NO_COLOR, --once frames pasted into an issue). Encoding the fill in
// color alone made every bar look full-width in monochrome.
func SegmentMeter(frac float64, width int, stops []string, track lipgloss.Style) string {
if width <= 0 {
return ""
@@ -110,7 +115,7 @@ func SegmentMeter(frac float64, width int, stops []string, track lipgloss.Style)
b.WriteString(styles[i].Render("■"))
}
if width-filled > 0 {
b.WriteString(track.Render(strings.Repeat("■", width-filled)))
b.WriteString(track.Render(strings.Repeat("□", width-filled)))
}
return b.String()
}
+23
View File
@@ -149,6 +149,29 @@ func TestMeterWidth(t *testing.T) {
}
}
// TestSegmentMeterLegibleWithoutColor guards the bargauge bug where the filled
// and track cells used the same glyph, so the fill fraction was carried by
// color alone and every bar looked full-width in monochrome output.
func TestSegmentMeterLegibleWithoutColor(t *testing.T) {
plain := lipgloss.NewStyle() // no color, as when ANSI is stripped
const width = 20
for _, tc := range []struct {
frac float64
wantFilled int
}{
{0, 0}, {0.02, 0}, {0.5, 10}, {0.875, 18}, {1, 20},
} {
out := SegmentMeter(tc.frac, width, []string{"#0f0", "#0f0"}, plain)
if got := lipgloss.Width(out); got != width {
t.Errorf("frac %v: width %d, want %d", tc.frac, got, width)
}
filled := strings.Count(out, "■")
if filled != tc.wantFilled {
t.Errorf("frac %v: filled ■ = %d, want %d (bar carries no width info)", tc.frac, filled, tc.wantFilled)
}
}
}
func TestFormatCompact(t *testing.T) {
tests := []struct {
in float64