summaryrefslogtreecommitdiff
path: root/internal/tui/dashboard/sparkline.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-02-26 10:11:37 +0200
committerPaul Buetow <paul@buetow.org>2026-02-26 10:11:37 +0200
commit76db79bbd74ebf58ea4403a7e623316c1e4b41de (patch)
tree20fd850131b6958d95a20aea5ae0c7d3ee3bf8de /internal/tui/dashboard/sparkline.go
parent8e7f9c656df5ab155648af316635b90dbd53f8d0 (diff)
tui: stabilize overview sparklines and prevent wrap artifacts
Diffstat (limited to 'internal/tui/dashboard/sparkline.go')
-rw-r--r--internal/tui/dashboard/sparkline.go33
1 files changed, 14 insertions, 19 deletions
diff --git a/internal/tui/dashboard/sparkline.go b/internal/tui/dashboard/sparkline.go
index 93a6789..2ce8c90 100644
--- a/internal/tui/dashboard/sparkline.go
+++ b/internal/tui/dashboard/sparkline.go
@@ -11,15 +11,23 @@ func renderSparkline(data []float64, width int) string {
}
samples := sampleForWidth(data, width)
+ leftPad := 0
+ if len(samples) < width {
+ leftPad = width - len(samples)
+ }
min, max := minMax(samples)
if min == max {
top := repeatRune(' ', width)
- bottom := repeatRune('█', width)
+ bottom := repeatRune(' ', leftPad) + repeatRune('█', len(samples))
return top + "\n" + bottom
}
top := make([]rune, width)
bottom := make([]rune, width)
+ for i := 0; i < leftPad; i++ {
+ top[i] = ' '
+ bottom[i] = ' '
+ }
scale := 16.0
denom := max - min
for i, value := range samples {
@@ -40,7 +48,7 @@ func renderSparkline(data []float64, width int) string {
bottomLevel = 8
}
- col := i
+ col := leftPad + i
top[col] = sparkRowChars[topLevel]
bottom[col] = sparkRowChars[bottomLevel]
}
@@ -61,24 +69,11 @@ func renderLabeledSparkline(label string, data []float64, width int) string {
}
func sampleForWidth(data []float64, width int) []float64 {
- if width == 1 {
- return []float64{data[len(data)-1]}
- }
- if len(data) == 1 {
- out := make([]float64, width)
- for i := range out {
- out[i] = data[0]
- }
- return out
- }
-
- last := len(data) - 1
- samples := make([]float64, width)
- for i := 0; i < width; i++ {
- idx := int(math.Round(float64(i) * float64(last) / float64(width-1)))
- samples[i] = data[idx]
+ if width >= len(data) {
+ return append([]float64(nil), data...)
}
- return samples
+ start := len(data) - width
+ return append([]float64(nil), data[start:]...)
}
func minMax(values []float64) (float64, float64) {