Files
wehub-resource-sync a06f331eb8
CI / benchmark (push) Has been skipped
install-script / posix-syntax (push) Successful in 6m1s
CI / build-onnx (push) Failing after 6m43s
init-smoke / dry-run (push) Failing after 15m57s
security / govulncheck (push) Has been cancelled
security / trivy-fs (push) Has been cancelled
CI / test (1.26, ubuntu-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
CI / test (1.26, macos-latest) (push) Has been cancelled
CI / build-windows (push) Has been cancelled
CI / lint (push) Has been cancelled
install-script / powershell-syntax (push) Has been cancelled
install-script / install (macos-14) (push) Has been cancelled
install-script / install (ubuntu-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:33:42 +08:00

71 lines
1.6 KiB
Go

package progress
import (
"fmt"
"strconv"
"time"
)
// fmtCount renders n with thousands separators ("48,210"). Counters in the
// step rows read as magnitudes, not digit strings; the separator makes a
// six-digit symbol count legible at a glance.
func fmtCount(n int64) string {
s := strconv.FormatInt(n, 10)
neg := false
if len(s) > 0 && s[0] == '-' {
neg = true
s = s[1:]
}
if len(s) <= 3 {
if neg {
return "-" + s
}
return s
}
head := len(s) % 3
out := make([]byte, 0, len(s)+len(s)/3+1)
if neg {
out = append(out, '-')
}
if head > 0 {
out = append(out, s[:head]...)
}
for i := head; i < len(s); i += 3 {
if len(out) > 0 && out[len(out)-1] != '-' {
out = append(out, ',')
}
out = append(out, s[i:i+3]...)
}
return string(out)
}
// fmtDurationCompact renders a duration at the precision a progress line
// wants: sub-10s with one decimal ("3.2s"), sub-minute in whole seconds,
// then minute granularity. Sub-100ms rounds to "0.1s" floor-less so a fast
// step still shows a real number.
func fmtDurationCompact(d time.Duration) string {
if d < 0 {
d = 0
}
switch {
case d < time.Second:
ms := d.Milliseconds()
if ms < 1 {
ms = 1
}
return fmt.Sprintf("%dms", ms)
case d < 10*time.Second:
return fmt.Sprintf("%.1fs", d.Seconds())
case d < time.Minute:
return fmt.Sprintf("%ds", int(d.Seconds()+0.5))
case d < time.Hour:
m := int(d / time.Minute)
s := int((d % time.Minute) / time.Second)
return fmt.Sprintf("%dm%02ds", m, s)
default:
h := int(d / time.Hour)
m := int((d % time.Hour) / time.Minute)
return fmt.Sprintf("%dh%02dm", h, m)
}
}