fix(widget): visual-width math and order-independent thresholds

- gauge scale line, bargauge value column, and timeseries y-gutter sized
  labels with len(); FormatValue can emit µs (2 bytes, 1 cell), skewing the
  layout by a cell. Use lipgloss.Width.
- thresholdColor picked the last matching step, assuming ascending order —
  Grafana exports steps sorted, hand-written YAML may not be. Resolve the
  highest matching threshold regardless of slice order.
- drop the dead 'var _ = lipgloss.Width' import keeper.
This commit is contained in:
Aleks Petrov committed 2026-07-07 19:51:12 +02:00
1 parent bd3cab4dd8
commit 78de3b283a
5 files changed
+38 -13

No files matched your search

+2 -2
View File
@@ -77,8 +77,8 @@ func (b *BarGauge) body(iw, ih int, th theme.Theme) string {
}
valueW := 0
for _, vt := range valueTexts {
if len(vt) > valueW {
valueW = len(vt)
if w := lipgloss.Width(vt); w > valueW {
valueW = w
}
}
barW := iw - legendW - valueW - 4 // 2 gaps of 2 spaces
+1 -1
View File
@@ -78,7 +78,7 @@ func (g *Gauge) body(iw, ih int, th theme.Theme) string {
if ih >= 4 {
lo := FormatValue(g.Min, g.Unit, g.Decimals)
hi := FormatValue(g.Max, g.Unit, g.Decimals)
gap := iw - len(lo) - len(hi)
gap := iw - lipgloss.Width(lo) - lipgloss.Width(hi)
if gap > 0 {
lines = append(lines, th.DimStyle().Render(lo+strings.Repeat(" ", gap)+hi))
}
+3 -5
View File
@@ -57,9 +57,9 @@ func (t *TimeSeries) body(iw, ih int, th theme.Theme) string {
hiLabel := FormatValue(maxV, t.Unit, t.Decimals)
loLabel := FormatValue(minV, t.Unit, t.Decimals)
gutter := len(hiLabel)
if len(loLabel) > gutter {
gutter = len(loLabel)
gutter := lipgloss.Width(hiLabel)
if w := lipgloss.Width(loLabel); w > gutter {
gutter = w
}
if gutter > iw/3 {
gutter = iw / 3
@@ -180,5 +180,3 @@ func (t *TimeSeries) legendInfo(th theme.Theme) string {
}
return strings.Join(parts, " ")
}
var _ = lipgloss.Width // keep lipgloss import if unused in future edits
+9 -5
View File
@@ -112,20 +112,24 @@ func vCenter(content string, iw, ih int) string {
}
// thresholdColor resolves the color for value v given thresholds: the color
// of the highest threshold whose Value <= v. Falls back to fallback.
// of the highest threshold whose Value <= v, regardless of slice order (Grafana
// exports steps sorted, hand-written YAML may not be). A nil-Value threshold is
// the base color, used when no numeric threshold matches. Falls back to fallback.
func thresholdColor(v float64, thresholds []Threshold, th theme.Theme, fallback string) string {
color := fallback
base := true
matched := false
bestVal := 0.0
for _, t := range thresholds {
if t.Value == nil {
if base {
if !matched {
color = th.ResolveColor(t.Color)
}
continue
}
if v >= *t.Value {
if v >= *t.Value && (!matched || *t.Value >= bestVal) {
matched = true
bestVal = *t.Value
color = th.ResolveColor(t.Color)
base = false
}
}
return color
+23
View File
@@ -82,6 +82,28 @@ func TestThresholdColor(t *testing.T) {
}
}
// Threshold resolution must not depend on slice order — Grafana exports steps
// sorted ascending, hand-written YAML may not be.
func TestThresholdColorUnsorted(t *testing.T) {
th := theme.Pilot
fv := func(v float64) *float64 { return &v }
// Same steps as TestThresholdColor, deliberately shuffled with base last.
thresholds := []Threshold{
{Value: fv(90), Color: "green"}, {Value: fv(70), Color: "yellow"}, {Color: "red"},
}
tests := []struct {
v float64
want string
}{
{50, th.Error}, {66, th.Error}, {75, th.Warning}, {95, th.Success},
}
for _, tt := range tests {
if got := thresholdColor(tt.v, thresholds, th, th.Label); got != tt.want {
t.Errorf("thresholdColor(%v) = %q, want %q", tt.v, got, tt.want)
}
}
}
func TestFormatValue(t *testing.T) {
tests := []struct {
v float64
@@ -93,6 +115,7 @@ func TestFormatValue(t *testing.T) {
{154.234, "currencyUSD", "$154.23"},
{465.24, "s", "7.8m"},
{0.5, "s", "500ms"},
{0.000002, "s", "2µs"}, // non-ASCII unit: width math must be visual, not len()
{57300, "short", "57.3K"},
{3, "", "3"},
}