2024-01-15 04:45:34 +03:00
|
|
|
package progress
|
|
|
|
|
|
|
|
import (
|
2024-02-09 19:08:39 +03:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2024-02-28 20:35:22 +03:00
|
|
|
"strings"
|
2024-01-15 04:45:34 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
humanize "github.com/dustin/go-humanize"
|
|
|
|
"github.com/dustin/go-humanize/english"
|
2024-02-28 20:35:22 +03:00
|
|
|
mpb "github.com/vbauerster/mpb/v8"
|
2024-01-15 04:45:34 +03:00
|
|
|
"github.com/vbauerster/mpb/v8/decor"
|
2024-02-28 20:35:22 +03:00
|
|
|
|
|
|
|
"github.com/neilotoole/sq/libsq/core/langz"
|
2024-01-15 04:45:34 +03:00
|
|
|
)
|
|
|
|
|
2024-02-28 20:35:22 +03:00
|
|
|
// BarOpt is a functional option for Bar creation.
|
|
|
|
type BarOpt interface {
|
|
|
|
apply(*Progress, *barConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
// barConfig is passed to Progress.createBar. Note that there are five decorator
|
|
|
|
// fields: these are effectively the "widgets" that are displayed on any given
|
|
|
|
// bar. If a widget is nil, a nopWidget will be set by newVirtualBar. This is
|
|
|
|
// because we need the widgets to exist (even if invisible) for visual
|
|
|
|
// alignment purposes.
|
|
|
|
type barConfig struct {
|
|
|
|
style mpb.BarFillerBuilder
|
|
|
|
counterWidget decor.Decorator
|
|
|
|
percentWidget decor.Decorator
|
|
|
|
timerWidget decor.Decorator
|
|
|
|
memoryWidget decor.Decorator
|
|
|
|
msgWidget decor.Decorator
|
|
|
|
total int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// createBar creates a new bar, and adds it to Progress.allBars.
|
|
|
|
//
|
|
|
|
// The caller must hold Progress.mu.
|
|
|
|
func (p *Progress) createBar(cfg *barConfig, opts []BarOpt) Bar {
|
|
|
|
if p == nil {
|
|
|
|
return nopBar{}
|
|
|
|
}
|
|
|
|
|
|
|
|
vb := newVirtualBar(p, cfg, opts)
|
|
|
|
p.allBars = append(p.allBars, vb)
|
|
|
|
return vb
|
|
|
|
}
|
|
|
|
|
|
|
|
// forgetBar removes bar vb from Progress.allBars. It is the caller's
|
|
|
|
// responsibility to first invoke virtualBar.destroy.
|
|
|
|
func (p *Progress) forgetBar(vb *virtualBar) {
|
|
|
|
if p == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
p.allBars = langz.Remove(p.allBars, vb)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewByteCounter returns a new determinate bar whose label metric is the size
|
|
|
|
// in bytes of the data being processed. The caller is ultimately responsible
|
|
|
|
// for calling Bar.Stop on the returned Bar.
|
|
|
|
func (p *Progress) NewByteCounter(msg string, size int64, opts ...BarOpt) Bar {
|
2024-01-15 04:45:34 +03:00
|
|
|
if p == nil {
|
2024-02-23 15:35:13 +03:00
|
|
|
return nopBar{}
|
2024-01-15 04:45:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
|
2024-02-28 20:35:22 +03:00
|
|
|
cfg := &barConfig{
|
|
|
|
msgWidget: staticMsgWidget(p, msg),
|
|
|
|
total: size,
|
|
|
|
}
|
2024-01-15 04:45:34 +03:00
|
|
|
|
|
|
|
if size < 0 {
|
|
|
|
cfg.style = spinnerStyle(p.colors.Filler)
|
2024-02-28 20:35:22 +03:00
|
|
|
cfg.counterWidget = colorize(
|
|
|
|
decor.Current(decor.SizeB1000(0), "% .1f", p.align.counter),
|
|
|
|
p.colors.Size,
|
|
|
|
)
|
2024-01-15 04:45:34 +03:00
|
|
|
} else {
|
|
|
|
cfg.style = barStyle(p.colors.Filler)
|
2024-02-28 20:35:22 +03:00
|
|
|
cfg.counterWidget = colorize(
|
|
|
|
decor.Counters(decor.SizeB1000(0), "% .1f / % .1f", p.align.counter),
|
|
|
|
p.colors.Size,
|
|
|
|
)
|
|
|
|
cfg.percentWidget = colorize(
|
|
|
|
decor.NewPercentage(" %.1f", p.align.percent),
|
|
|
|
p.colors.Percent,
|
|
|
|
)
|
2024-01-15 04:45:34 +03:00
|
|
|
}
|
|
|
|
|
2024-02-28 20:35:22 +03:00
|
|
|
return p.createBar(cfg, opts)
|
2024-01-15 04:45:34 +03:00
|
|
|
}
|
|
|
|
|
2024-02-09 19:08:39 +03:00
|
|
|
// NewFilesizeCounter returns a new indeterminate bar whose label metric is a
|
|
|
|
// filesize, or "-" if it can't be read. If f is non-nil, its size is used; else
|
|
|
|
// the file at path fp is used. The caller is ultimately responsible for calling
|
|
|
|
// Bar.Stop on the returned Bar.
|
2024-02-28 20:35:22 +03:00
|
|
|
func (p *Progress) NewFilesizeCounter(msg string, f *os.File, fp string, opts ...BarOpt) Bar {
|
2024-02-09 19:08:39 +03:00
|
|
|
if p == nil {
|
2024-02-23 15:35:13 +03:00
|
|
|
return nopBar{}
|
2024-02-09 19:08:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
|
2024-02-28 20:35:22 +03:00
|
|
|
cfg := &barConfig{
|
|
|
|
msgWidget: staticMsgWidget(p, msg),
|
|
|
|
total: -1,
|
|
|
|
style: spinnerStyle(p.colors.Filler),
|
|
|
|
}
|
2024-02-09 19:08:39 +03:00
|
|
|
|
2024-02-28 20:35:22 +03:00
|
|
|
fn := func(statistics decor.Statistics) string {
|
2024-02-09 19:08:39 +03:00
|
|
|
var fi os.FileInfo
|
|
|
|
var err error
|
|
|
|
if f != nil {
|
|
|
|
fi, err = f.Stat()
|
|
|
|
} else {
|
|
|
|
fi, err = os.Stat(fp)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "-"
|
|
|
|
}
|
|
|
|
|
2024-02-28 20:35:22 +03:00
|
|
|
return fmt.Sprintf("% .1f", decor.SizeB1000(fi.Size()))
|
|
|
|
}
|
2024-02-09 19:08:39 +03:00
|
|
|
|
2024-02-28 20:35:22 +03:00
|
|
|
cfg.counterWidget = colorize(decor.Any(fn, p.align.counter), p.colors.Size)
|
|
|
|
|
|
|
|
return p.createBar(cfg, opts)
|
2024-02-09 19:08:39 +03:00
|
|
|
}
|
|
|
|
|
2024-01-15 04:45:34 +03:00
|
|
|
// NewUnitCounter returns a new indeterminate bar whose label
|
|
|
|
// metric is the plural of the provided unit. The caller is ultimately
|
2024-01-25 07:01:24 +03:00
|
|
|
// responsible for calling Bar.Stop on the returned Bar.
|
2024-01-15 04:45:34 +03:00
|
|
|
//
|
|
|
|
// bar := p.NewUnitCounter("Ingest records", "rec")
|
|
|
|
// defer bar.Stop()
|
|
|
|
//
|
|
|
|
// for i := 0; i < 100; i++ {
|
|
|
|
// bar.Incr(1)
|
|
|
|
// time.Sleep(100 * time.Millisecond)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// This produces output similar to:
|
|
|
|
//
|
|
|
|
// Ingesting records ∙∙● 87 recs
|
|
|
|
//
|
|
|
|
// Note that the unit arg is automatically pluralized.
|
2024-02-28 20:35:22 +03:00
|
|
|
func (p *Progress) NewUnitCounter(msg, unit string, opts ...BarOpt) Bar {
|
2024-01-15 04:45:34 +03:00
|
|
|
if p == nil {
|
2024-02-23 15:35:13 +03:00
|
|
|
return nopBar{}
|
2024-01-15 04:45:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
|
|
|
|
cfg := &barConfig{
|
2024-02-28 20:35:22 +03:00
|
|
|
msgWidget: staticMsgWidget(p, msg),
|
|
|
|
total: -1,
|
|
|
|
style: spinnerStyle(p.colors.Filler),
|
2024-01-15 04:45:34 +03:00
|
|
|
}
|
|
|
|
|
2024-02-28 20:35:22 +03:00
|
|
|
fn := func(statistics decor.Statistics) string {
|
2024-01-15 04:45:34 +03:00
|
|
|
s := humanize.Comma(statistics.Current)
|
|
|
|
if unit != "" {
|
|
|
|
s += " " + english.PluralWord(int(statistics.Current), unit, "")
|
|
|
|
}
|
|
|
|
return s
|
2024-02-28 20:35:22 +03:00
|
|
|
}
|
2024-01-15 04:45:34 +03:00
|
|
|
|
2024-02-28 20:35:22 +03:00
|
|
|
cfg.counterWidget = colorize(decor.Any(fn, p.align.counter), p.colors.Size)
|
|
|
|
|
|
|
|
return p.createBar(cfg, opts)
|
2024-01-15 04:45:34 +03:00
|
|
|
}
|
|
|
|
|
2024-02-28 20:35:22 +03:00
|
|
|
// NewWaiter returns a generic indeterminate spinner with a timer. This produces
|
|
|
|
// output similar to:
|
2024-01-15 04:45:34 +03:00
|
|
|
//
|
|
|
|
// @excel/remote: start download ●∙∙ 4s
|
|
|
|
//
|
2024-01-25 07:01:24 +03:00
|
|
|
// The caller is ultimately responsible for calling Bar.Stop on the
|
2024-01-15 04:45:34 +03:00
|
|
|
// returned Bar.
|
2024-02-28 20:35:22 +03:00
|
|
|
func (p *Progress) NewWaiter(msg string, opts ...BarOpt) Bar {
|
2024-01-15 04:45:34 +03:00
|
|
|
if p == nil {
|
2024-02-23 15:35:13 +03:00
|
|
|
return nopBar{}
|
2024-01-15 04:45:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
|
2024-02-28 20:35:22 +03:00
|
|
|
opts = append(opts, OptTimer)
|
|
|
|
|
2024-01-15 04:45:34 +03:00
|
|
|
cfg := &barConfig{
|
2024-02-28 20:35:22 +03:00
|
|
|
msgWidget: staticMsgWidget(p, msg),
|
|
|
|
total: -1,
|
|
|
|
style: spinnerStyle(p.colors.Filler),
|
2024-01-15 04:45:34 +03:00
|
|
|
}
|
|
|
|
|
2024-02-28 20:35:22 +03:00
|
|
|
return p.createBar(cfg, opts)
|
2024-01-15 04:45:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewUnitTotalCounter returns a new determinate bar whose label
|
|
|
|
// metric is the plural of the provided unit. The caller is ultimately
|
2024-01-25 07:01:24 +03:00
|
|
|
// responsible for calling Bar.Stop on the returned Bar.
|
2024-01-15 04:45:34 +03:00
|
|
|
//
|
|
|
|
// This produces output similar to:
|
|
|
|
//
|
|
|
|
// Ingesting sheets ∙∙∙∙∙● 4 / 16 sheets
|
|
|
|
//
|
|
|
|
// Note that the unit arg is automatically pluralized.
|
2024-02-28 20:35:22 +03:00
|
|
|
func (p *Progress) NewUnitTotalCounter(msg, unit string, total int64, opts ...BarOpt) Bar {
|
2024-01-15 04:45:34 +03:00
|
|
|
if p == nil {
|
2024-02-23 15:35:13 +03:00
|
|
|
return nopBar{}
|
2024-01-15 04:45:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if total <= 0 {
|
|
|
|
return p.NewUnitCounter(msg, unit)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
|
|
|
|
cfg := &barConfig{
|
2024-02-28 20:35:22 +03:00
|
|
|
msgWidget: staticMsgWidget(p, msg),
|
|
|
|
total: total,
|
|
|
|
style: barStyle(p.colors.Filler),
|
2024-01-15 04:45:34 +03:00
|
|
|
}
|
|
|
|
|
2024-02-28 20:35:22 +03:00
|
|
|
fn := func(statistics decor.Statistics) string {
|
2024-01-15 04:45:34 +03:00
|
|
|
s := humanize.Comma(statistics.Current) + " / " + humanize.Comma(statistics.Total)
|
|
|
|
if unit != "" {
|
|
|
|
s += " " + english.PluralWord(int(statistics.Current), unit, "")
|
|
|
|
}
|
|
|
|
return s
|
2024-02-28 20:35:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg.counterWidget = colorize(decor.Any(fn, p.align.counter), p.colors.Size)
|
|
|
|
return p.createBar(cfg, opts)
|
2024-01-15 04:45:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTimeoutWaiter returns a new indeterminate bar whose label is the
|
|
|
|
// amount of time remaining until expires. It produces output similar to:
|
|
|
|
//
|
|
|
|
// Locking @sakila ●∙∙ timeout in 7s
|
|
|
|
//
|
2024-01-25 07:01:24 +03:00
|
|
|
// The caller is ultimately responsible for calling Bar.Stop on
|
2024-01-15 04:45:34 +03:00
|
|
|
// the returned bar, although the bar will also be stopped when the
|
|
|
|
// parent Progress stops.
|
2024-02-28 20:35:22 +03:00
|
|
|
func (p *Progress) NewTimeoutWaiter(msg string, expires time.Time, opts ...BarOpt) Bar {
|
2024-01-15 04:45:34 +03:00
|
|
|
if p == nil {
|
2024-02-23 15:35:13 +03:00
|
|
|
return nopBar{}
|
2024-01-15 04:45:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg := &barConfig{
|
2024-02-28 20:35:22 +03:00
|
|
|
msgWidget: staticMsgWidget(p, msg),
|
|
|
|
style: spinnerStyle(p.colors.Waiting),
|
2024-01-15 04:45:34 +03:00
|
|
|
}
|
|
|
|
|
2024-02-28 20:35:22 +03:00
|
|
|
fn := func(statistics decor.Statistics) string {
|
2024-01-15 04:45:34 +03:00
|
|
|
remaining := time.Until(expires)
|
|
|
|
switch {
|
|
|
|
case remaining > 0:
|
2024-02-28 20:35:22 +03:00
|
|
|
return fmt.Sprintf("timeout in %s", remaining.Round(time.Second))
|
2024-01-15 04:45:34 +03:00
|
|
|
case remaining > -time.Second:
|
|
|
|
// We do the extra second to prevent a "flash" of the timeout message,
|
|
|
|
// and it also prevents "timeout in -1s" etc. This situation should be
|
|
|
|
// rare; the caller should have already called Stop() on the Progress
|
|
|
|
// when the timeout happened, but we'll play it safe.
|
2024-02-28 20:35:22 +03:00
|
|
|
return "timeout in 0s"
|
2024-01-15 04:45:34 +03:00
|
|
|
default:
|
2024-02-28 20:35:22 +03:00
|
|
|
return "timed out"
|
2024-01-15 04:45:34 +03:00
|
|
|
}
|
2024-02-28 20:35:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg.counterWidget = decor.Meta(decor.Any(fn, p.align.counter), func(s string) string {
|
|
|
|
if strings.HasPrefix(s, "timeout in") {
|
|
|
|
return p.colors.Size.Sprint(s)
|
|
|
|
}
|
|
|
|
return p.colors.Warning.Sprint(s)
|
2024-01-15 04:45:34 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
cfg.total = int64(time.Until(expires))
|
2024-02-28 20:35:22 +03:00
|
|
|
|
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
|
|
|
|
return p.createBar(cfg, opts)
|
2024-01-15 04:45:34 +03:00
|
|
|
}
|