mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-18 18:04:17 +00:00
revert(ui): switch progress bar back to pterm ProgressbarPrinter
The custom thread-safe progress bar (progress.go) had width overflow issues. Revert to pterm's built-in ProgressbarPrinter which handles terminal width calculation internally. Remove StartProgressWithMaxWidth in favor of the simpler StartProgress API.
This commit is contained in:
@@ -395,7 +395,7 @@ func auditInstalled(sourcePath, mode, projectRoot, threshold string, opts auditO
|
||||
// Phase 1: parallel scan with progress bar.
|
||||
var progressBar *ui.ProgressBar
|
||||
if !jsonOutput {
|
||||
progressBar = ui.StartProgressWithMaxWidth("Scanning skills", len(skillPaths), headerMinWidth+4)
|
||||
progressBar = ui.StartProgress("Scanning skills", len(skillPaths))
|
||||
}
|
||||
onDone := func() {
|
||||
if progressBar != nil {
|
||||
@@ -521,7 +521,7 @@ func auditFiltered(sourcePath string, names, groups []string, mode, projectRoot,
|
||||
// Phase 1: parallel scan with progress bar.
|
||||
var progressBar *ui.ProgressBar
|
||||
if !jsonOutput {
|
||||
progressBar = ui.StartProgressWithMaxWidth("Scanning skills", len(matched), headerMinWidth+4)
|
||||
progressBar = ui.StartProgress("Scanning skills", len(matched))
|
||||
}
|
||||
onDone := func() {
|
||||
if progressBar != nil {
|
||||
|
||||
@@ -1,169 +0,0 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"atomicgo.dev/cursor"
|
||||
"github.com/pterm/pterm"
|
||||
)
|
||||
|
||||
// ProgressBar is a thread-safe progress bar with optional MaxWidth.
|
||||
// It replaces pterm's ProgressbarPrinter to avoid race conditions
|
||||
// from concurrent Increment() calls and to support width constraints.
|
||||
type ProgressBar struct {
|
||||
current atomic.Int64
|
||||
total int
|
||||
title string
|
||||
maxWidth int // 0 = terminal width
|
||||
start time.Time
|
||||
isTTY bool
|
||||
|
||||
mu sync.Mutex
|
||||
stopped bool
|
||||
}
|
||||
|
||||
// StartProgress starts a progress bar. maxWidth 0 means full terminal width.
|
||||
func StartProgress(title string, total int) *ProgressBar {
|
||||
return StartProgressWithMaxWidth(title, total, 0)
|
||||
}
|
||||
|
||||
// StartProgressWithMaxWidth starts a progress bar constrained to maxWidth columns.
|
||||
func StartProgressWithMaxWidth(title string, total int, maxWidth int) *ProgressBar {
|
||||
if !IsTTY() {
|
||||
fmt.Printf("%s (0/%d)\n", title, total)
|
||||
return &ProgressBar{total: total, title: title}
|
||||
}
|
||||
|
||||
cursor.Hide()
|
||||
p := &ProgressBar{
|
||||
total: total,
|
||||
title: title,
|
||||
maxWidth: maxWidth,
|
||||
start: time.Now(),
|
||||
isTTY: true,
|
||||
}
|
||||
p.render()
|
||||
return p
|
||||
}
|
||||
|
||||
// Increment increments progress by 1. Safe for concurrent use.
|
||||
func (p *ProgressBar) Increment() {
|
||||
p.current.Add(1)
|
||||
if p.isTTY {
|
||||
p.render()
|
||||
}
|
||||
}
|
||||
|
||||
// Add increments progress by n. Safe for concurrent use.
|
||||
func (p *ProgressBar) Add(n int) {
|
||||
p.current.Add(int64(n))
|
||||
if p.isTTY {
|
||||
p.render()
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateTitle updates the progress bar title.
|
||||
func (p *ProgressBar) UpdateTitle(title string) {
|
||||
const maxTitleWidth = 40
|
||||
display := title
|
||||
if len(display) > maxTitleWidth {
|
||||
display = display[:maxTitleWidth-3] + "..."
|
||||
}
|
||||
|
||||
p.mu.Lock()
|
||||
p.title = display
|
||||
p.mu.Unlock()
|
||||
|
||||
if p.isTTY {
|
||||
p.render()
|
||||
} else {
|
||||
fmt.Printf(" %s\n", strings.TrimRight(display, " "))
|
||||
}
|
||||
}
|
||||
|
||||
// Stop stops the progress bar, rendering the final state with N/N.
|
||||
func (p *ProgressBar) Stop() {
|
||||
p.mu.Lock()
|
||||
if p.stopped {
|
||||
p.mu.Unlock()
|
||||
return
|
||||
}
|
||||
p.stopped = true
|
||||
p.mu.Unlock()
|
||||
|
||||
if !p.isTTY {
|
||||
return
|
||||
}
|
||||
|
||||
// Force to total and render final frame.
|
||||
p.current.Store(int64(p.total))
|
||||
p.render()
|
||||
fmt.Println()
|
||||
cursor.Show()
|
||||
}
|
||||
|
||||
// render draws the progress bar on the current line using \r.
|
||||
func (p *ProgressBar) render() {
|
||||
cur := int(p.current.Load())
|
||||
if cur > p.total {
|
||||
cur = p.total
|
||||
}
|
||||
total := p.total
|
||||
if total == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
p.mu.Lock()
|
||||
title := p.title
|
||||
p.mu.Unlock()
|
||||
|
||||
width := pterm.GetTerminalWidth()
|
||||
if p.maxWidth > 0 && p.maxWidth < width {
|
||||
width = p.maxWidth
|
||||
}
|
||||
|
||||
// Format: title [cur/total] ████░░░░ pct% | elapsed
|
||||
pct := int(math.Round(float64(cur) / float64(total) * 100))
|
||||
elapsed := time.Since(p.start).Round(time.Second)
|
||||
|
||||
padding := 1 + int(math.Log10(float64(total)))
|
||||
counter := fmt.Sprintf("[%0*d/%d]", padding, cur, total)
|
||||
pctStr := fmt.Sprintf("%3d%%", pct)
|
||||
timeStr := fmt.Sprintf("| %s", elapsed)
|
||||
|
||||
// Colorize parts.
|
||||
titleC := Cyan + title + Reset
|
||||
counterC := Gray + "[" + White + fmt.Sprintf("%0*d", padding, cur) + Gray + "/" + White + fmt.Sprintf("%d", total) + Gray + "]" + Reset
|
||||
pctC := pctColor(pct) + pctStr + Reset
|
||||
|
||||
// Calculate bar width: total - title - counter - pct - time - spaces.
|
||||
fixedLen := len(title) + 1 + len(counter) + 1 + 1 + len(pctStr) + 1 + len(timeStr)
|
||||
barWidth := width - fixedLen
|
||||
if barWidth < 5 {
|
||||
barWidth = 5
|
||||
}
|
||||
|
||||
filled := barWidth * cur / total
|
||||
empty := barWidth - filled
|
||||
barC := Cyan + strings.Repeat("█", filled) + Gray + strings.Repeat("░", empty) + Reset
|
||||
|
||||
line := fmt.Sprintf("%s %s %s %s %s", titleC, counterC, barC, pctC, timeStr)
|
||||
|
||||
fmt.Printf("\r%-*s", width, line)
|
||||
}
|
||||
|
||||
// pctColor returns a color code fading from red (0%) to green (100%).
|
||||
func pctColor(pct int) string {
|
||||
if pct >= 100 {
|
||||
return Green
|
||||
}
|
||||
if pct >= 50 {
|
||||
return Yellow
|
||||
}
|
||||
return Red
|
||||
}
|
||||
+62
-1
@@ -342,7 +342,68 @@ func SummaryBox(title string, items map[string]string) {
|
||||
box.Println(content)
|
||||
}
|
||||
|
||||
// ProgressBar and related methods are in progress.go.
|
||||
// ProgressBar wraps pterm progress bar
|
||||
type ProgressBar struct {
|
||||
bar *pterm.ProgressbarPrinter
|
||||
total int
|
||||
}
|
||||
|
||||
// StartProgress starts a progress bar
|
||||
func StartProgress(title string, total int) *ProgressBar {
|
||||
if !IsTTY() {
|
||||
fmt.Printf("%s (0/%d)\n", title, total)
|
||||
return &ProgressBar{total: total}
|
||||
}
|
||||
|
||||
bar, _ := pterm.DefaultProgressbar.
|
||||
WithTotal(total).
|
||||
WithTitle(title).
|
||||
Start()
|
||||
return &ProgressBar{bar: bar, total: total}
|
||||
}
|
||||
|
||||
// Increment increments progress by 1.
|
||||
func (p *ProgressBar) Increment() {
|
||||
if p.bar != nil {
|
||||
p.bar.Increment()
|
||||
}
|
||||
}
|
||||
|
||||
// Add increments progress by n.
|
||||
func (p *ProgressBar) Add(n int) {
|
||||
if p.bar != nil {
|
||||
p.bar.Add(n)
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateTitle updates progress bar title. The title is padded or
|
||||
// truncated to a fixed width so that the bar width stays stable.
|
||||
func (p *ProgressBar) UpdateTitle(title string) {
|
||||
const maxWidth = 40
|
||||
// Truncate long titles with ellipsis
|
||||
display := title
|
||||
if len(display) > maxWidth {
|
||||
display = display[:maxWidth-3] + "..."
|
||||
}
|
||||
// Pad to fixed width to prevent bar width from changing
|
||||
if len(display) < maxWidth {
|
||||
display += strings.Repeat(" ", maxWidth-len(display))
|
||||
}
|
||||
|
||||
if p.bar != nil {
|
||||
p.bar.UpdateTitle(display)
|
||||
} else {
|
||||
fmt.Printf(" %s\n", strings.TrimRight(display, " "))
|
||||
}
|
||||
}
|
||||
|
||||
// Stop stops the progress bar. It's safe to call even if the bar already
|
||||
// stopped itself (pterm auto-stops when Current >= Total).
|
||||
func (p *ProgressBar) Stop() {
|
||||
if p.bar != nil && p.bar.IsActive {
|
||||
p.bar.Stop()
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateNotification prints a colorful update notification
|
||||
func UpdateNotification(currentVersion, latestVersion string) {
|
||||
|
||||
Reference in New Issue
Block a user