Label chart axes in binary sizes and small percents, and windows in words

A bytes axis read "20.0BB" - compact's billion with a B after it - and
a quiet node's CPU axis read 0% all the way up. Sizes are now binary like
every other on these pages, a percent under ten keeps a decimal, and a
window of an hour is "the last hour" rather than "1h0m0s".

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5.5 committed 2026-09-28 23:30:01 +02:00
1 parent fe0875ee05
commit 9ae526302b
6 files changed
+57 -16

No files matched your search

+29 -12
View File
@@ -101,18 +101,7 @@ var funcs = template.FuncMap{
// shows. Binary units because that is what `ls -lh` and every disk-full
// message on the host will say, and a page that disagrees with the shell
// about the size of the same file starts an argument about neither.
"bytes": func(n int64) string {
const unit = 1024
if n < unit {
return fmt.Sprintf("%d B", n)
}
div, exp := int64(unit), 0
for v := n / unit; v >= unit; v /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %ciB", float64(n)/float64(div), "KMGTPE"[exp])
},
"bytes": humanBytes,
// dict lets one chart template be called with named arguments. Go
// templates pass a single value, and four charts differing in three
// strings each is four copies of the same markup otherwise.
@@ -256,3 +245,31 @@ func ago(t time.Time) string {
return unit(int(d.Hours()/24/30+0.5), "month")
}
}
// humanBytes writes a size in binary units, the way the shell does.
func humanBytes(n int64) string {
const unit = 1024
if n < unit {
return fmt.Sprintf("%d B", n)
}
div, exp := int64(unit), 0
for v := n / unit; v >= unit; v /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %ciB", float64(n)/float64(div), "KMGTPE"[exp])
}
// windowWord names a chart window the way a sentence would: "hour", "6
// hours", "30 minutes", rather than Go's 1h0m0s.
func windowWord(d time.Duration) string {
switch {
case d == time.Hour:
return "hour"
case d%time.Hour == 0:
return fmt.Sprintf("%d hours", int(d.Hours()))
case d%time.Minute == 0:
return fmt.Sprintf("%d minutes", int(d.Minutes()))
}
return d.String()
}