summaryrefslogtreecommitdiff
path: root/internal/tui/dashboard/sparkline.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-02-25 09:36:57 +0200
committerPaul Buetow <paul@buetow.org>2026-02-25 09:36:57 +0200
commit72ff234e97b16485553a79a876690a359058b110 (patch)
tree7b5133489ff48b0dee6857f4df2e82a704b8768c /internal/tui/dashboard/sparkline.go
parent1279ffb8f2efba54ff005cce91ba65c149cb1ee6 (diff)
Fix initial TUI sizing and align two-row sparklines
Diffstat (limited to 'internal/tui/dashboard/sparkline.go')
-rw-r--r--internal/tui/dashboard/sparkline.go53
1 files changed, 39 insertions, 14 deletions
diff --git a/internal/tui/dashboard/sparkline.go b/internal/tui/dashboard/sparkline.go
index 9c1f2c4..b94d84f 100644
--- a/internal/tui/dashboard/sparkline.go
+++ b/internal/tui/dashboard/sparkline.go
@@ -1,8 +1,9 @@
package dashboard
import "math"
+import "strings"
-var sparkChars = []rune("▁▂▃▄▅▆▇█")
+var sparkRowChars = []rune(" ▁▂▃▄▅▆▇█")
func renderSparkline(data []float64, width int) string {
if len(data) == 0 || width <= 0 {
@@ -11,27 +12,51 @@ func renderSparkline(data []float64, width int) string {
samples := sampleForWidth(data, width)
min, max := minMax(samples)
- line := ""
if min == max {
- line = repeatRune('▄', len(samples))
- return line + "\n" + line
+ top := repeatRune(' ', len(samples))
+ bottom := repeatRune('█', len(samples))
+ return top + "\n" + bottom
}
- out := make([]rune, len(samples))
- scale := float64(len(sparkChars) - 1)
+ top := make([]rune, len(samples))
+ bottom := make([]rune, len(samples))
+ scale := 16.0
denom := max - min
for i, value := range samples {
- idx := int(math.Round((value - min) / denom * scale))
- if idx < 0 {
- idx = 0
+ level := int(math.Round((value - min) / denom * scale))
+ if level < 0 {
+ level = 0
}
- if idx >= len(sparkChars) {
- idx = len(sparkChars) - 1
+ if level > 16 {
+ level = 16
}
- out[i] = sparkChars[idx]
+
+ topLevel := level - 8
+ if topLevel < 0 {
+ topLevel = 0
+ }
+ bottomLevel := level
+ if bottomLevel > 8 {
+ bottomLevel = 8
+ }
+
+ top[i] = sparkRowChars[topLevel]
+ bottom[i] = sparkRowChars[bottomLevel]
+ }
+ return string(top) + "\n" + string(bottom)
+}
+
+func renderLabeledSparkline(label string, data []float64, width int) string {
+ spark := renderSparkline(data, width)
+ if spark == "" {
+ return label
+ }
+ lines := strings.Split(spark, "\n")
+ if len(lines) == 1 {
+ return label + " " + lines[0]
}
- line = string(out)
- return line + "\n" + line
+ pad := repeatRune(' ', len([]rune(label))+1)
+ return label + " " + lines[0] + "\n" + pad + lines[1]
}
func sampleForWidth(data []float64, width int) []float64 {