Files
cburn/internal/cli/format.go
teernisse 892f578565 fix: linter compliance and code quality improvements across codebase
Address golangci-lint findings and improve error handling throughout:

Package doc comments:
- Add canonical "// Package X ..." comments to source, model, config,
  pipeline, cli, store, and main packages for godoc compliance.

Security & correctness:
- Fix directory permissions 0o755 -> 0o750 in store/cache.go Open()
  (gosec G301: restrict group write on cache directory)
- Fix config.Save() to check encoder error before closing file, preventing
  silent data loss on encode failure
- Add //nolint:gosec annotations with justifications on intentional
  patterns (constructed file paths, manual bounds checking, config fields)
- Add //nolint:nilerr on intentional error-swallowing in scanner WalkDir
- Add //nolint:revive on stuttering type names (ModelStats, ModelUsage)
  that would break too many call sites to rename

Performance (perfsprint):
- Replace fmt.Sprintf("%d", n) with strconv.FormatInt(n, 10) in format.go
  FormatTokens() and FormatNumber() hot paths
- Clean up redundant fmt.Sprintf patterns in FormatCost and FormatDelta

Code cleanup:
- Convert if-else chain to switch in parser.go skipJSONString() for clarity
- Remove unused indexedResult struct from pipeline/loader.go
- Add deferred cache.Close() in pipeline/bench_test.go to prevent leaks
- Add deferred cache.Close() in cmd/root.go data loading path
- Fix doc comment alignment in scanner.go decodeProjectName
- Remove trailing blank line in cmd/costs.go
- Fix duplicate "/day" suffix in cmd/summary.go cost-per-day formatting
- Rename shadowed variable 'max' -> 'maxVal' in cli/render.go Sparkline
2026-02-20 16:07:26 -05:00

113 lines
2.5 KiB
Go

// Package cli provides formatting and rendering utilities for terminal output.
package cli
import (
"fmt"
"math"
"strconv"
"strings"
)
// FormatTokens formats a token count with human-readable suffixes.
// e.g., 1234 -> "1.2K", 1234567 -> "1.2M", 1234567890 -> "1.2B"
func FormatTokens(n int64) string {
abs := n
if abs < 0 {
abs = -abs
}
switch {
case abs >= 1_000_000_000:
return fmt.Sprintf("%.1fB", float64(n)/1_000_000_000)
case abs >= 1_000_000:
return fmt.Sprintf("%.1fM", float64(n)/1_000_000)
case abs >= 1_000:
return fmt.Sprintf("%.1fK", float64(n)/1_000)
default:
return strconv.FormatInt(n, 10)
}
}
// FormatCost formats a USD cost value.
func FormatCost(cost float64) string {
if cost >= 1000 {
return "$" + FormatNumber(int64(math.Round(cost)))
}
if cost >= 100 {
return fmt.Sprintf("$%.0f", cost)
}
if cost >= 10 {
return fmt.Sprintf("$%.1f", cost)
}
return fmt.Sprintf("$%.2f", cost)
}
// FormatDuration formats seconds into a human-readable duration.
// e.g., 3725 -> "1h 2m", 125 -> "2m", 45 -> "45s"
func FormatDuration(secs int64) string {
if secs <= 0 {
return "0s"
}
hours := secs / 3600
mins := (secs % 3600) / 60
if hours > 0 {
return fmt.Sprintf("%dh %dm", hours, mins)
}
if mins > 0 {
return fmt.Sprintf("%dm", mins)
}
return fmt.Sprintf("%ds", secs)
}
// FormatNumber adds comma separators to an integer.
// e.g., 1234567 -> "1,234,567"
func FormatNumber(n int64) string {
if n < 0 {
return "-" + FormatNumber(-n)
}
s := strconv.FormatInt(n, 10)
if len(s) <= 3 {
return s
}
var result strings.Builder
remainder := len(s) % 3
if remainder > 0 {
result.WriteString(s[:remainder])
}
for i := remainder; i < len(s); i += 3 {
if result.Len() > 0 {
result.WriteByte(',')
}
result.WriteString(s[i : i+3])
}
return result.String()
}
// FormatPercent formats a 0-1 float as a percentage string.
func FormatPercent(f float64) string {
return fmt.Sprintf("%.1f%%", f*100)
}
// FormatDelta formats a cost delta with sign and color hint.
// Returns the formatted string and whether it's positive.
func FormatDelta(current, previous float64) string {
delta := current - previous
if delta >= 0 {
return "+" + FormatCost(delta)
}
return "-" + FormatCost(-delta)
}
// FormatDayOfWeek returns a 3-letter day abbreviation from a weekday number.
func FormatDayOfWeek(weekday int) string {
days := []string{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}
if weekday >= 0 && weekday < 7 {
return days[weekday]
}
return "???"
}