A frame has three lines, in the style of github.com/programmersd21/zap: - an operation badge, a braille spinner and the item; - a bar blending Catppuccin blue to mauve over a dark track; - a data / speed / ETA line, or "finishing…" once all input is read. Rendering is a pure function of the frame. It falls back to plain text without color, and truncates so no line wraps on narrow terminals.
194 lines
5.2 KiB
Go
194 lines
5.2 KiB
Go
// Package progress draws a live progress display on a terminal while a
|
|
// backend runs: a spinner line, a gradient bar and a data/speed/ETA line,
|
|
// redrawn in place on stderr. The look follows zap
|
|
// (github.com/programmersd21/zap): Catppuccin Mocha colors, a blue-to-mauve
|
|
// bar and a braille spinner.
|
|
package progress
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"strings"
|
|
"time"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
// Catppuccin Mocha palette.
|
|
type rgb struct{ r, g, b uint8 }
|
|
|
|
var (
|
|
barFrom = rgb{0x89, 0xb4, 0xfa} // blue
|
|
barTo = rgb{0xcb, 0xa6, 0xf7} // mauve
|
|
barTrack = rgb{0x31, 0x32, 0x44} // surface0
|
|
accent = rgb{0xf5, 0xc2, 0xe7} // pink: spinner
|
|
muted = rgb{0x7f, 0x84, 0x9c} // overlay1: labels, path
|
|
dataColor = rgb{0xcb, 0xa6, 0xf7} // mauve
|
|
speedCol = rgb{0xf9, 0xe2, 0xaf} // yellow
|
|
etaColor = rgb{0x89, 0xdc, 0xeb} // sky
|
|
badgeText = rgb{0x1e, 0x1e, 0x2e} // base
|
|
)
|
|
|
|
// badgeColors gives each operation's badge its own color.
|
|
var badgeColors = map[string]rgb{
|
|
"extract": {0x89, 0xb4, 0xfa}, // blue
|
|
"create": {0xa6, 0xe3, 0xa1}, // green
|
|
}
|
|
|
|
var spinner = []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"}
|
|
|
|
const (
|
|
barFull = "█"
|
|
barEmpty = "░"
|
|
indent = " "
|
|
// MinWidth is the narrowest terminal the display is drawn on.
|
|
MinWidth = 40
|
|
)
|
|
|
|
// frame is everything one drawing depends on.
|
|
type frame struct {
|
|
op string // "extract" or "create"
|
|
label string // what is being processed
|
|
tick int // spinner position
|
|
done int64 // bytes processed
|
|
total int64 // bytes expected; <= 0 if unknown
|
|
speed float64
|
|
columns int
|
|
color bool
|
|
}
|
|
|
|
// render returns the display lines. No line is wider than f.columns, so the
|
|
// display never wraps and can be erased line by line.
|
|
func render(f frame) []string {
|
|
p := painter(f.color)
|
|
|
|
label := f.label
|
|
if label == "" {
|
|
label = "scanning…"
|
|
}
|
|
label = truncate(label, f.columns-20)
|
|
head := indent + p.badge(f.op) + " " + p.fg(accent, spinner[f.tick%len(spinner)]) + " " + p.italic(muted, label)
|
|
|
|
width := barWidth(f.columns)
|
|
filled := 0
|
|
if f.total > 0 {
|
|
filled = int(math.Round(math.Min(float64(f.done)/float64(f.total), 1) * float64(width)))
|
|
}
|
|
var bar strings.Builder
|
|
for i := range width {
|
|
if i < filled {
|
|
bar.WriteString(p.fg(blend(barFrom, barTo, float64(i)/float64(max(width-1, 1))), barFull))
|
|
} else {
|
|
bar.WriteString(p.fg(barTrack, barEmpty))
|
|
}
|
|
}
|
|
|
|
parts := []string{p.fg(muted, "data ") + p.fg(dataColor, dataText(f.done, f.total))}
|
|
switch {
|
|
case f.total > 0 && f.done >= f.total:
|
|
// All input has been read; the backend is still writing or, for
|
|
// multithreaded compressors, still compressing buffered input.
|
|
parts = append(parts, p.fg(etaColor, "finishing…"))
|
|
case f.speed > 0:
|
|
parts = append(parts, p.fg(speedCol, FormatBytes(int64(f.speed))+"/s"))
|
|
if eta, ok := eta(f.done, f.total, f.speed); ok {
|
|
parts = append(parts, p.fg(muted, "eta ")+p.fg(etaColor, formatDuration(eta)))
|
|
}
|
|
}
|
|
info := indent + strings.Join(parts, p.fg(muted, " · "))
|
|
|
|
return []string{head, indent + bar.String(), info}
|
|
}
|
|
|
|
// barWidth follows zap: the terminal width minus 36, clamped to 20..60.
|
|
func barWidth(columns int) int { return min(max(columns-36, 20), 60) }
|
|
|
|
func dataText(done, total int64) string {
|
|
if total <= 0 {
|
|
return FormatBytes(done)
|
|
}
|
|
return FormatBytes(min(done, total)) + " / " + FormatBytes(total)
|
|
}
|
|
|
|
func eta(done, total int64, speed float64) (time.Duration, bool) {
|
|
if total <= 0 || speed <= 0 {
|
|
return 0, false
|
|
}
|
|
secs := float64(max(total-done, 0)) / speed
|
|
if secs > 86400 {
|
|
return 0, false
|
|
}
|
|
return time.Duration(secs * float64(time.Second)), true
|
|
}
|
|
|
|
// formatDuration prints "<1s", "42s", "3m 05s" or "2h 07m".
|
|
func formatDuration(d time.Duration) string {
|
|
s := int(d.Seconds())
|
|
switch {
|
|
case s < 1:
|
|
return "<1s"
|
|
case s < 60:
|
|
return fmt.Sprintf("%ds", s)
|
|
case s < 3600:
|
|
return fmt.Sprintf("%dm %02ds", s/60, s%60)
|
|
default:
|
|
return fmt.Sprintf("%dh %02dm", s/3600, s%3600/60)
|
|
}
|
|
}
|
|
|
|
// FormatBytes formats n bytes as "512 B", "1.5 KiB", ... (rounded down).
|
|
func FormatBytes(n int64) string {
|
|
if n < 1024 {
|
|
return fmt.Sprintf("%d B", n)
|
|
}
|
|
units := []string{"KiB", "MiB", "GiB", "TiB", "PiB"}
|
|
v := float64(n) / 1024
|
|
i := 0
|
|
for v >= 1024 && i < len(units)-1 {
|
|
v /= 1024
|
|
i++
|
|
}
|
|
return fmt.Sprintf("%.1f %s", math.Floor(v*10)/10, units[i])
|
|
}
|
|
|
|
func truncate(s string, n int) string {
|
|
if n < 1 {
|
|
n = 1
|
|
}
|
|
if utf8.RuneCountInString(s) <= n {
|
|
return s
|
|
}
|
|
r := []rune(s)
|
|
return string(r[:n-1]) + "…"
|
|
}
|
|
|
|
func blend(a, b rgb, t float64) rgb {
|
|
mix := func(x, y uint8) uint8 { return uint8(math.Round(float64(x) + (float64(y)-float64(x))*t)) }
|
|
return rgb{mix(a.r, b.r), mix(a.g, b.g), mix(a.b, b.b)}
|
|
}
|
|
|
|
// painter applies 24-bit color escapes, or nothing when color is off.
|
|
type painter bool
|
|
|
|
func (p painter) fg(c rgb, s string) string {
|
|
if !p {
|
|
return s
|
|
}
|
|
return fmt.Sprintf("\x1b[38;2;%d;%d;%dm%s\x1b[0m", c.r, c.g, c.b, s)
|
|
}
|
|
|
|
func (p painter) italic(c rgb, s string) string {
|
|
if !p {
|
|
return s
|
|
}
|
|
return "\x1b[3m" + p.fg(c, s)
|
|
}
|
|
|
|
func (p painter) badge(op string) string {
|
|
if !p {
|
|
return "[" + op + "]"
|
|
}
|
|
bg := badgeColors[op]
|
|
return fmt.Sprintf("\x1b[1;38;2;%d;%d;%d;48;2;%d;%d;%dm %s \x1b[0m",
|
|
badgeText.r, badgeText.g, badgeText.b, bg.r, bg.g, bg.b, op)
|
|
}
|