Files
rtnetmon/internal/monitor/styles.go
T
sneak 18b27dc48c
check / check (push) Failing after 0s
Adopt repo standards: scaffold, policies, lint-clean (closes #1)
Add the standard scaffold and bring the tree to a clean lint under the
vendored `default: all` config: `script/` Scripts-to-Rule-Them-All
entrypoints with the `Makefile` as thin shims; a `Dockerfile` whose
`lint` and `test` phases gate the build; `.gitea/workflows/` CI running
`script/cibuild`; `REPO_POLICIES.md`, `.editorconfig`, `.dockerignore`,
`LICENSE` (WTFPL), `TODO.md`, `.gitignore`. The `.golangci.yml` is
byte-identical to the canonical copy in the `prompts` repo
(`https://git.eeqj.de/sneak/prompts/raw/branch/main/.golangci.yml`).

The 211 lint findings were fixed, not suppressed: package globals became
functions/fields/a command constructor, magic numbers became named
constants, `ctx` threads into the probes, loop functions were split to
cut complexity. Behavior is unchanged; the log file mode stays `0644`.
Four `//nolint:gosec` remain — G204 on the fixed-argv subprocess calls,
G304 on the operator-chosen log file — matching the reference repos.

`make check` is green (lint and tests run in Docker).

Model: opus-4-8
2026-09-21 07:13:37 +00:00

119 lines
2.9 KiB
Go

//go:build linux
package monitor
import tcell "github.com/gdamore/tcell/v2"
// percentFull is the top of the 0..100 percentage scale.
const percentFull = 100.0
// Meter fill thresholds, expressed as percent-good (100 = best, 0 = worst).
const (
meterExcellent = 90
meterGood = 75
meterFair = 60
meterMediocre = 40
meterPoor = 20
)
// TCP latency thresholds, in milliseconds.
const (
latencyGood = 50
latencyModerate = 100
latencyHigh = 200
)
// lossWarnPercent is the packet-loss level above which the display warns.
const lossWarnPercent = 5
func styleBrightGreen() tcell.Style {
return tcell.StyleDefault.Foreground(tcell.ColorGreen).Bold(true)
}
func styleGreen() tcell.Style {
return tcell.StyleDefault.Foreground(tcell.ColorGreen)
}
func styleDimGreen() tcell.Style {
return tcell.StyleDefault.Foreground(tcell.ColorGreen).Dim(true)
}
func styleYellow() tcell.Style {
return tcell.StyleDefault.Foreground(tcell.ColorYellow)
}
func styleOrange() tcell.Style {
return tcell.StyleDefault.Foreground(tcell.ColorOrange)
}
func styleRed() tcell.Style {
return tcell.StyleDefault.Foreground(tcell.ColorRed)
}
func styleBrightRed() tcell.Style {
return tcell.StyleDefault.Foreground(tcell.ColorRed).Bold(true)
}
// rainbow returns the color cycle used for spinners and the clock.
func rainbow() []tcell.Style {
return []tcell.Style{
tcell.StyleDefault.Foreground(tcell.ColorRed),
tcell.StyleDefault.Foreground(tcell.ColorOrange),
tcell.StyleDefault.Foreground(tcell.ColorYellow),
tcell.StyleDefault.Foreground(tcell.ColorGreen),
tcell.StyleDefault.Foreground(tcell.ColorBlue),
tcell.StyleDefault.Foreground(tcell.ColorPurple),
}
}
// MeterColorForValue returns the color style for a meter value. When reverse
// is true, low values are good (green) and high values bad (red); otherwise
// high values are good.
func MeterColorForValue(value, maxValue int, reverse bool) tcell.Style {
percentage := float64(value) / float64(maxValue) * percentFull
if reverse {
percentage = percentFull - percentage
}
switch {
case percentage >= meterExcellent:
return styleBrightGreen()
case percentage >= meterGood:
return styleGreen()
case percentage >= meterFair:
return styleDimGreen()
case percentage >= meterMediocre:
return styleYellow()
case percentage >= meterPoor:
return styleOrange()
default:
return styleRed()
}
}
// StyleLatency returns the style for a TCP latency in milliseconds.
func StyleLatency(ms float64) tcell.Style {
switch {
case ms < latencyGood:
return styleBrightGreen()
case ms < latencyModerate:
return styleGreen()
case ms < latencyHigh:
return styleYellow()
default:
return styleRed()
}
}
// StyleLoss returns the style for a packet-loss percentage.
func StyleLoss(p float64) tcell.Style {
switch {
case p == 0:
return styleBrightGreen()
case p < lossWarnPercent:
return styleYellow()
default:
return styleBrightRed()
}
}