Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions blink/internal/reporter/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ type Progress struct {
active bool
total int
cur int
frame int
mu sync.Mutex
}

// iconFrames is a pulsing bloom that grows from a dot to a star and back, advanced one step per render.
var iconFrames = []string{"·", "∘", "○", "◯", "✦", "✧", "✦", "◯", "○", "∘"}

// NewProgress returns a progress bar; it only draws when w is a terminal.
func NewProgress(w *os.File) *Progress {
p := &Progress{w: w, width: 28}
Expand Down Expand Up @@ -64,23 +68,14 @@ func (p *Progress) Finish() {
if p == nil || !p.active {
return
}
_, _ = fmt.Fprintf(p.w, "\r%s\r", strings.Repeat(" ", p.width+20))
_, _ = fmt.Fprintf(p.w, "\r%s\r", strings.Repeat(" ", p.width+24))
}

func (p *Progress) render() {
if p.total <= 0 {
return
}
pct := p.cur * 100 / p.total
filled := p.cur * p.width / p.total
var bar string
switch {
case filled >= p.width:
bar = strings.Repeat("=", p.width)
case filled == 0:
bar = ">" + strings.Repeat(" ", p.width-1)
default:
bar = strings.Repeat("=", filled-1) + ">" + strings.Repeat(" ", p.width-filled)
}
_, _ = fmt.Fprintf(p.w, "\r %d/%d [%s] %3d%%", p.cur, p.total, bar, pct)
icon := iconFrames[p.frame%len(iconFrames)]
p.frame++
_, _ = fmt.Fprintf(p.w, "\r %s", icon)
}
Loading