hdmistat/internal/fbdraw/grid_test.go
2025-07-24 14:32:50 +02:00

161 lines
3.7 KiB
Go

package fbdraw
import (
"fmt"
"image/color"
"testing"
"git.eeqj.de/sneak/hdmistat/internal/font"
)
func ExampleGrid() {
// Create a 80x25 character grid (standard terminal size)
grid := NewGrid(80, 25)
// Create a writer for convenience
w := NewGridWriter(grid)
// Clear with dark background
w.SetBackground(Gray10).Clear()
// Draw header
w.MoveTo(0, 0).
SetBackground(Blue).
SetColor(White).
SetWeight(font.WeightBold).
WriteLine(" System Monitor v1.0 ")
// Reset to normal style
w.SetBackground(Black).SetWeight(font.WeightRegular)
// System info section
w.MoveTo(2, 2).
SetColor(Green).SetWeight(font.WeightBold).
Write("SYSTEM INFO").
SetWeight(font.WeightRegular).SetColor(White)
w.MoveTo(2, 4).Write("Hostname: ").SetColor(Cyan).Write("server01.example.com")
w.MoveTo(2, 5).SetColor(White).Write("Uptime: ").SetColor(Yellow).Write("14 days, 3:42:15")
w.MoveTo(2, 6).SetColor(White).Write("Load: ").SetColor(Red).Write("2.45 1.82 1.65")
// CPU meters
w.MoveTo(2, 8).SetColor(Blue).SetWeight(font.WeightBold).Write("CPU USAGE")
w.SetWeight(font.WeightRegular)
cpuValues := []float64{45.2, 78.9, 23.4, 91.5}
for i, cpu := range cpuValues {
w.MoveTo(2, 10+i)
w.SetColor(White).Write("CPU%d [", i)
// Draw meter
meterWidth := 20
filled := int(cpu / 100.0 * float64(meterWidth))
// Choose color based on usage
if cpu > 80 {
w.SetColor(Red)
} else if cpu > 50 {
w.SetColor(Yellow)
} else {
w.SetColor(Green)
}
for j := 0; j < meterWidth; j++ {
if j < filled {
w.Write("█")
} else {
w.Write("░")
}
}
w.SetColor(White).Write("] %5.1f%%", cpu)
}
// Memory section
w.MoveTo(45, 8).SetColor(Purple).SetWeight(font.WeightBold).Write("MEMORY")
w.SetWeight(font.WeightRegular).SetColor(White)
w.MoveTo(45, 10).Write("Used: ").SetColor(Green).Write("4.2 GB / 16.0 GB")
w.MoveTo(45, 11).SetColor(White).Write("Free: ").SetColor(Green).Write("11.8 GB")
w.MoveTo(45, 12).SetColor(White).Write("Cache: ").SetColor(Blue).Write("2.1 GB")
// Process table
w.MoveTo(2, 16).SetColor(Orange).SetWeight(font.WeightBold).Write("TOP PROCESSES")
w.SetWeight(font.WeightRegular)
// Table header
w.MoveTo(2, 18).SetBackground(Gray30).SetColor(White).SetWeight(font.WeightBold)
w.Write(" PID USER PROCESS CPU% MEM% ")
w.SetBackground(Black).SetWeight(font.WeightRegular)
// Table rows
processes := []struct {
pid int
user string
name string
cpu float64
mem float64
}{
{1234, "root", "systemd", 0.2, 0.1},
{5678, "user", "firefox", 15.8, 12.4},
{9012, "user", "vscode", 42.1, 8.7},
}
for i, p := range processes {
y := 19 + i
// Alternate row backgrounds
if i%2 == 0 {
w.SetBackground(Gray10)
} else {
w.SetBackground(Black)
}
// Highlight high CPU
if p.cpu > 40 {
w.SetColor(Red)
} else if p.cpu > 20 {
w.SetColor(Yellow)
} else {
w.SetColor(Gray70)
}
w.MoveTo(0, y)
w.Write(" %5d %-8s %-25s %5.1f %5.1f ",
p.pid, p.user, p.name, p.cpu, p.mem)
}
// Output as text
fmt.Println(grid.ToText())
}
func TestGrid(t *testing.T) {
grid := NewGrid(40, 10)
w := NewGridWriter(grid)
// Test basic writing
w.MoveTo(5, 2).Write("Hello, World!")
// Test colors and styles
w.MoveTo(5, 4).
SetColor(Red).SetWeight(font.WeightBold).
Write("Bold Red Text")
// Test unicode
w.MoveTo(5, 6).
SetColor(Green).
Write("Progress: [████████▒▒▒▒▒▒▒▒] 50%")
// Check text output
text := grid.ToText()
if text == "" {
t.Error("Grid should not be empty")
}
// Check specific cell
cell := grid.Cells[2][5] // Row 2, Column 5 should be 'H'
if cell.Rune != 'H' {
t.Errorf("Expected 'H' at (5,2), got '%c'", cell.Rune)
}
}