Skip to content

Commit c9b51c5

Browse files
committed
fix: resolve TUI alert duplication and usability issues
- eliminate duplicate system.Status() call via CheckWithStatus() - unify sparkline/progress bar color thresholds (70/90%) - preserve docker cache on timeout instead of discarding - fix truncate to use rune count for unicode support
1 parent ea9b528 commit c9b51c5

5 files changed

Lines changed: 20 additions & 18 deletions

File tree

internal/alerts/alerts.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ func Check(cfg *config.AlertConfig) (*AlertResult, error) {
2929
if err != nil {
3030
return nil, err
3131
}
32+
return CheckWithStatus(cfg, info), nil
33+
}
3234

35+
func CheckWithStatus(cfg *config.AlertConfig, info *system.StatusInfo) *AlertResult {
3336
result := &AlertResult{
3437
CPU: AlertItem{
3538
Status: statusFor(info.CPU.UsagePercent, cfg.CPU),
@@ -52,7 +55,7 @@ func Check(cfg *config.AlertConfig) (*AlertResult, error) {
5255
})
5356
}
5457

55-
return result, nil
58+
return result
5659
}
5760

5861
func statusFor(current, threshold float64) string {

internal/tui/fetch.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ func fetchLocal(alertCfg *config.AlertConfig) ServerData {
6969
data.Status = status
7070
data.Name = status.Hostname
7171

72-
alertResult, _ := alerts.Check(alertCfg)
73-
data.Alerts = alertResult
72+
data.Alerts = alerts.CheckWithStatus(alertCfg, status)
7473

7574
procs, _ := system.TopProcesses(5)
7675
data.Processes = procs
@@ -132,10 +131,12 @@ func fetchDocker() ([]docker.Container, string) {
132131
dockerCacheMu.Unlock()
133132
return containers, status
134133
case <-time.After(2 * time.Second):
135-
// Goroutine will finish eventually and update cache + release flag
136134
dockerCacheMu.Lock()
137-
dockerCacheCtr, dockerCacheSt = nil, "unavailable"
135+
c, s := dockerCacheCtr, dockerCacheSt
138136
dockerCacheMu.Unlock()
137+
if s != "" {
138+
return c, s
139+
}
139140
return nil, "unavailable"
140141
}
141142
}

internal/tui/sparkline.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,15 @@ func sparkline(data []float64, width int) string {
4242
return b.String()
4343
}
4444

45-
// sparklineColor returns the appropriate style for a sparkline
46-
// based on the last value: green <50%, yellow 50-80%, red >80%.
4745
func sparklineColor(data []float64) lipgloss.Style {
4846
if len(data) == 0 {
4947
return greenStyle
5048
}
5149
last := data[len(data)-1]
5250
switch {
53-
case last > 80:
51+
case last >= 90:
5452
return redStyle
55-
case last >= 50:
53+
case last >= 70:
5654
return yellowStyle
5755
default:
5856
return greenStyle

internal/tui/sparkline_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,11 @@ func TestSparklineColor(t *testing.T) {
128128
}{
129129
{"empty", nil},
130130
{"green low", []float64{30}},
131-
{"yellow mid", []float64{60}},
132-
{"red high", []float64{90}},
133-
{"boundary 50", []float64{50}},
134-
{"boundary 80", []float64{80}},
135-
{"boundary 81", []float64{81}},
131+
{"green mid", []float64{60}},
132+
{"yellow 70", []float64{70}},
133+
{"yellow 89", []float64{89}},
134+
{"red 90", []float64{90}},
135+
{"red high", []float64{95}},
136136
}
137137
for _, tt := range tests {
138138
t.Run(tt.name, func(t *testing.T) {

internal/tui/styles.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@ func alertStyle(status string) lipgloss.Style {
8787
}
8888
}
8989

90-
// truncate shortens a string to max length with ellipsis.
9190
func truncate(s string, max int) string {
92-
if len(s) <= max {
91+
r := []rune(s)
92+
if len(r) <= max {
9393
return s
9494
}
9595
if max <= 1 {
96-
return s[:max]
96+
return string(r[:max])
9797
}
98-
return s[:max-1] + "~"
98+
return string(r[:max-1]) + "~"
9999
}

0 commit comments

Comments
 (0)