Implement the presentation layer for terminal output:
- cli/format.go: Human-readable formatting functions — FormatTokens
(K/M/B suffixes), FormatCost (adaptive precision: $X.XX under $10,
$X.X under $100, $X over $100, comma-separated over $1000),
FormatDuration (Xh Ym / Xm / Xs), FormatNumber (comma-separated
integers), FormatPercent (0-1 -> "XX.X%"), FormatDelta (signed
cost delta with +/- prefix), FormatDayOfWeek (weekday number to
3-letter abbreviation).
- cli/render.go: lipgloss-styled output components using the Flexoki
Dark color palette:
* RenderTitle: centered title in a rounded border box.
* RenderTable: full-featured bordered table with auto-calculated
column widths, right-aligned numeric columns (all except first),
separator rows (triggered by "---" sentinel), and proper Unicode
box-drawing characters. Supports optional title header and
explicit column widths.
* RenderProgressBar: bracketed fill bar with current/total counts.
* RenderSparkline: Unicode block sparkline (8-level: ▁ through █)
from arbitrary float64 series, auto-scaled to max.
* RenderHorizontalBar: simple horizontal bar chart entry for
inline comparisons.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
111 lines
2.4 KiB
Go
111 lines
2.4 KiB
Go
package cli
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"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 fmt.Sprintf("%d", n)
|
|
}
|
|
}
|
|
|
|
// FormatCost formats a USD cost value.
|
|
func FormatCost(cost float64) string {
|
|
if cost >= 1000 {
|
|
return fmt.Sprintf("$%s", 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 := fmt.Sprintf("%d", n)
|
|
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 fmt.Sprintf("+%s", FormatCost(delta))
|
|
}
|
|
return fmt.Sprintf("-%s", 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 "???"
|
|
}
|