diff --git a/.travis.yml b/.travis.yml index 0eb0f2f..9a203a6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,7 @@ language: go +arch: + - amd64 + - ppc64le go: - 1.14.x diff --git a/README.md b/README.md index bfb0c4d..a87786d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Multi Progress Bar -[![GoDoc](https://godoc.org/github.com/vbauerster/mpb?status.svg)](https://godoc.org/github.com/vbauerster/mpb) +[![GoDoc](https://pkg.go.dev/badge/github.com/vbauerster/mpb)](https://pkg.go.dev/github.com/vbauerster/mpb/v6) [![Build Status](https://travis-ci.org/vbauerster/mpb.svg?branch=master)](https://travis-ci.org/vbauerster/mpb) [![Go Report Card](https://goreportcard.com/badge/github.com/vbauerster/mpb)](https://goreportcard.com/report/github.com/vbauerster/mpb) @@ -8,16 +8,17 @@ ## Features -* __Multiple Bars__: Multiple progress bars are supported -* __Dynamic Total__: Set total while bar is running -* __Dynamic Add/Remove__: Dynamically add or remove bars -* __Cancellation__: Cancel whole rendering process -* __Predefined Decorators__: Elapsed time, [ewma](https://github.com/VividCortex/ewma) based ETA, Percentage, Bytes counter -* __Decorator's width sync__: Synchronized decorator's width among multiple bars +- **Multiple Bars**: Multiple progress bars are supported +- **Dynamic Total**: Set total while bar is running +- **Dynamic Add/Remove**: Dynamically add or remove bars +- **Cancellation**: Cancel whole rendering process +- **Predefined Decorators**: Elapsed time, [ewma](https://github.com/VividCortex/ewma) based ETA, Percentage, Bytes counter +- **Decorator's width sync**: Synchronized decorator's width among multiple bars ## Usage #### [Rendering single bar](_examples/singleBar/main.go) + ```go package main @@ -25,8 +26,8 @@ "math/rand" "time" - "github.com/vbauerster/mpb/v5" - "github.com/vbauerster/mpb/v5/decor" + "github.com/vbauerster/mpb/v6" + "github.com/vbauerster/mpb/v6/decor" ) func main() { @@ -36,9 +37,9 @@ total := 100 name := "Single Bar:" // adding a single bar, which will inherit container's width - bar := p.AddBar(int64(total), - // override DefaultBarStyle, which is "[=>-]<+" - mpb.BarStyle("╢▌▌░╟"), + bar := p.Add(int64(total), + // progress bar filler with customized style + mpb.NewBarFiller("╢▌▌░╟"), mpb.PrependDecorators( // display our name with one space on the right decor.Name(name, decor.WC{W: len(name) + 1, C: decor.DidentRight}), @@ -61,6 +62,7 @@ ``` #### [Rendering multiple bars](_examples/multiBars/main.go) + ```go var wg sync.WaitGroup // pass &wg (optional), so p will wait for it eventually diff --git a/_examples/barExtender/go.mod b/_examples/barExtender/go.mod index cb71f2a..be164a9 100644 --- a/_examples/barExtender/go.mod +++ b/_examples/barExtender/go.mod @@ -2,4 +2,4 @@ go 1.14 -require github.com/vbauerster/mpb/v5 v5.0.3 +require github.com/vbauerster/mpb/v6 v6.0.3 diff --git a/_examples/barExtender/main.go b/_examples/barExtender/main.go index 0d5d2d5..34e760f 100644 --- a/_examples/barExtender/main.go +++ b/_examples/barExtender/main.go @@ -7,8 +7,8 @@ "sync" "time" - "github.com/vbauerster/mpb/v5" - "github.com/vbauerster/mpb/v5/decor" + "github.com/vbauerster/mpb/v6" + "github.com/vbauerster/mpb/v6/decor" ) func main() { @@ -19,7 +19,7 @@ for i := 0; i < numBars; i++ { name := fmt.Sprintf("Bar#%d:", i) - efn := func(w io.Writer, width int, s *decor.Statistics) { + efn := func(w io.Writer, _ int, s decor.Statistics) { if s.Completed { fmt.Fprintf(w, "Bar id: %d has been completed\n", s.ID) } diff --git a/_examples/cancel/go.mod b/_examples/cancel/go.mod index f1a7f39..e6192d2 100644 --- a/_examples/cancel/go.mod +++ b/_examples/cancel/go.mod @@ -2,4 +2,4 @@ go 1.14 -require github.com/vbauerster/mpb/v5 v5.0.3 +require github.com/vbauerster/mpb/v6 v6.0.3 diff --git a/_examples/cancel/main.go b/_examples/cancel/main.go index b507601..6f4a3de 100644 --- a/_examples/cancel/main.go +++ b/_examples/cancel/main.go @@ -7,8 +7,8 @@ "sync" "time" - "github.com/vbauerster/mpb/v5" - "github.com/vbauerster/mpb/v5/decor" + "github.com/vbauerster/mpb/v6" + "github.com/vbauerster/mpb/v6/decor" ) func main() { diff --git a/_examples/complex/go.mod b/_examples/complex/go.mod index da328e4..b6b796e 100644 --- a/_examples/complex/go.mod +++ b/_examples/complex/go.mod @@ -2,4 +2,4 @@ go 1.14 -require github.com/vbauerster/mpb/v5 v5.0.3 +require github.com/vbauerster/mpb/v6 v6.0.3 diff --git a/_examples/complex/main.go b/_examples/complex/main.go index aa45b74..85aa98f 100644 --- a/_examples/complex/main.go +++ b/_examples/complex/main.go @@ -6,8 +6,8 @@ "sync" "time" - "github.com/vbauerster/mpb/v5" - "github.com/vbauerster/mpb/v5/decor" + "github.com/vbauerster/mpb/v6" + "github.com/vbauerster/mpb/v6/decor" ) func init() { @@ -16,7 +16,7 @@ func main() { doneWg := new(sync.WaitGroup) - p := mpb.New(mpb.WithWidth(64), mpb.WithWaitGroup(doneWg)) + p := mpb.New(mpb.WithWaitGroup(doneWg)) numBars := 4 var bars []*mpb.Bar @@ -44,7 +44,8 @@ i := i go func() { task := fmt.Sprintf("Task#%02d:", i) - job := "\x1b[31;1;4minstalling\x1b[0m" + // ANSI escape sequences are not supported on Windows OS + job := "\x1b[31;1;4mつのだ☆HIRO\x1b[0m" // preparing delayed bars b := p.AddBar(rand.Int63n(101)+100, mpb.BarQueueAfter(bars[i]), diff --git a/_examples/decoratorsOnTop/go.mod b/_examples/decoratorsOnTop/go.mod new file mode 100644 index 0000000..299a87f --- /dev/null +++ b/_examples/decoratorsOnTop/go.mod @@ -0,0 +1,5 @@ +module github.com/vbauerster/mpb/_examples/decoratorsOnTop + +go 1.14 + +require github.com/vbauerster/mpb/v6 v6.0.3 diff --git a/_examples/decoratorsOnTop/main.go b/_examples/decoratorsOnTop/main.go new file mode 100644 index 0000000..bdd0bd5 --- /dev/null +++ b/_examples/decoratorsOnTop/main.go @@ -0,0 +1,44 @@ +package main + +import ( + "io" + "math/rand" + "time" + + "github.com/vbauerster/mpb/v6" + "github.com/vbauerster/mpb/v6/decor" +) + +func main() { + p := mpb.New() + + total := 100 + bar := p.Add(int64(total), nil, + mpb.PrependDecorators( + decor.Name("Percentage: "), + decor.NewPercentage("%d"), + ), + mpb.AppendDecorators( + decor.Name("ETA: "), + decor.OnComplete( + decor.AverageETA(decor.ET_STYLE_GO), "done", + ), + ), + mpb.BarExtender(nlBarFiller(mpb.NewBarFiller("╢▌▌░╟"))), + ) + // simulating some work + max := 100 * time.Millisecond + for i := 0; i < total; i++ { + time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10) + bar.Increment() + } + // wait for our bar to complete and flush + p.Wait() +} + +func nlBarFiller(filler mpb.BarFiller) mpb.BarFiller { + return mpb.BarFillerFunc(func(w io.Writer, reqWidth int, st decor.Statistics) { + filler.Fill(w, reqWidth, st) + w.Write([]byte("\n")) + }) +} diff --git a/_examples/differentWidth/go.mod b/_examples/differentWidth/go.mod index eb3ed39..57700e2 100644 --- a/_examples/differentWidth/go.mod +++ b/_examples/differentWidth/go.mod @@ -2,4 +2,4 @@ go 1.14 -require github.com/vbauerster/mpb/v5 v5.0.3 +require github.com/vbauerster/mpb/v6 v6.0.3 diff --git a/_examples/differentWidth/main.go b/_examples/differentWidth/main.go index 90b2c30..7ed5b42 100644 --- a/_examples/differentWidth/main.go +++ b/_examples/differentWidth/main.go @@ -6,8 +6,8 @@ "sync" "time" - "github.com/vbauerster/mpb/v5" - "github.com/vbauerster/mpb/v5/decor" + "github.com/vbauerster/mpb/v6" + "github.com/vbauerster/mpb/v6/decor" ) func main() { @@ -24,7 +24,7 @@ name := fmt.Sprintf("Bar#%d:", i) bar := p.AddBar(int64(total), // set BarWidth 40 for bar 1 and 2 - mpb.BarOptOn(mpb.BarWidth(40), func() bool { return i > 0 }), + mpb.BarOptional(mpb.BarWidth(40), i > 0), mpb.PrependDecorators( // simple name decorator decor.Name(name), diff --git a/_examples/dynTotal/go.mod b/_examples/dynTotal/go.mod index 76f18f3..2ae2bad 100644 --- a/_examples/dynTotal/go.mod +++ b/_examples/dynTotal/go.mod @@ -2,4 +2,4 @@ go 1.14 -require github.com/vbauerster/mpb/v5 v5.0.3 +require github.com/vbauerster/mpb/v6 v6.0.3 diff --git a/_examples/dynTotal/main.go b/_examples/dynTotal/main.go index 55e8c0e..99342ba 100644 --- a/_examples/dynTotal/main.go +++ b/_examples/dynTotal/main.go @@ -5,8 +5,8 @@ "math/rand" "time" - "github.com/vbauerster/mpb/v5" - "github.com/vbauerster/mpb/v5/decor" + "github.com/vbauerster/mpb/v6" + "github.com/vbauerster/mpb/v6/decor" ) func init() { @@ -17,6 +17,7 @@ p := mpb.New(mpb.WithWidth(64)) var total int64 + // new bar with 'trigger complete event' disabled, because total is zero bar := p.AddBar(total, mpb.PrependDecorators(decor.Counters(decor.UnitKiB, "% .1f / % .1f")), mpb.AppendDecorators(decor.Percentage()), diff --git a/_examples/io/go.mod b/_examples/io/go.mod index 5789e76..2eba870 100644 --- a/_examples/io/go.mod +++ b/_examples/io/go.mod @@ -2,4 +2,4 @@ go 1.14 -require github.com/vbauerster/mpb/v5 v5.0.3 +require github.com/vbauerster/mpb/v6 v6.0.3 diff --git a/_examples/io/main.go b/_examples/io/main.go index 00a6dcd..bd07df9 100644 --- a/_examples/io/main.go +++ b/_examples/io/main.go @@ -6,8 +6,8 @@ "io/ioutil" "time" - "github.com/vbauerster/mpb/v5" - "github.com/vbauerster/mpb/v5/decor" + "github.com/vbauerster/mpb/v6" + "github.com/vbauerster/mpb/v6/decor" ) func main() { @@ -19,7 +19,8 @@ mpb.WithRefreshRate(180*time.Millisecond), ) - bar := p.AddBar(total, mpb.BarStyle("[=>-|"), + bar := p.Add(total, + mpb.NewBarFiller("[=>-|"), mpb.PrependDecorators( decor.CountersKibiByte("% .2f / % .2f"), ), diff --git a/_examples/merge/go.mod b/_examples/merge/go.mod index 50502ea..748b441 100644 --- a/_examples/merge/go.mod +++ b/_examples/merge/go.mod @@ -2,4 +2,4 @@ go 1.14 -require github.com/vbauerster/mpb/v5 v5.0.3 +require github.com/vbauerster/mpb/v6 v6.0.3 diff --git a/_examples/merge/main.go b/_examples/merge/main.go index 03a5817..5b54e14 100644 --- a/_examples/merge/main.go +++ b/_examples/merge/main.go @@ -6,8 +6,8 @@ "sync" "time" - "github.com/vbauerster/mpb/v5" - "github.com/vbauerster/mpb/v5/decor" + "github.com/vbauerster/mpb/v6" + "github.com/vbauerster/mpb/v6/decor" ) func main() { @@ -27,11 +27,13 @@ "done", ), decor.WCSyncSpace, // Placeholder + decor.WCSyncSpace, // Placeholder ), ) } else { pdecorators = mpb.PrependDecorators( decor.CountersNoUnit("% .1d / % .1d", decor.WCSyncSpace), + decor.OnComplete(decor.Spinner(nil, decor.WCSyncSpace), "done"), decor.OnComplete(decor.Spinner(nil, decor.WCSyncSpace), "done"), ) } @@ -63,8 +65,8 @@ func newVariadicSpinner(wc decor.WC) decor.Decorator { spinner := decor.Spinner(nil) - f := func(s *decor.Statistics) string { + fn := func(s decor.Statistics) string { return strings.Repeat(spinner.Decor(s), int(s.Current/3)) } - return decor.Any(f, wc) + return decor.Any(fn, wc) } diff --git a/_examples/multiBars/go.mod b/_examples/multiBars/go.mod index bb356b6..2a1ac25 100644 --- a/_examples/multiBars/go.mod +++ b/_examples/multiBars/go.mod @@ -2,4 +2,4 @@ go 1.14 -require github.com/vbauerster/mpb/v5 v5.0.3 +require github.com/vbauerster/mpb/v6 v6.0.3 diff --git a/_examples/multiBars/main.go b/_examples/multiBars/main.go index 38dc864..65cfeef 100644 --- a/_examples/multiBars/main.go +++ b/_examples/multiBars/main.go @@ -6,8 +6,8 @@ "sync" "time" - "github.com/vbauerster/mpb/v5" - "github.com/vbauerster/mpb/v5/decor" + "github.com/vbauerster/mpb/v6" + "github.com/vbauerster/mpb/v6/decor" ) func main() { diff --git a/_examples/panic/go.mod b/_examples/panic/go.mod index 3846072..15a905b 100644 --- a/_examples/panic/go.mod +++ b/_examples/panic/go.mod @@ -2,4 +2,4 @@ go 1.14 -require github.com/vbauerster/mpb/v5 v5.0.3 +require github.com/vbauerster/mpb/v6 v6.0.3 diff --git a/_examples/panic/main.go b/_examples/panic/main.go index 1a7cfc1..a569c49 100644 --- a/_examples/panic/main.go +++ b/_examples/panic/main.go @@ -3,18 +3,19 @@ import ( "fmt" "os" + "strings" "sync" "time" - "github.com/vbauerster/mpb/v5" - "github.com/vbauerster/mpb/v5/decor" + "github.com/vbauerster/mpb/v6" + "github.com/vbauerster/mpb/v6/decor" ) func main() { var wg sync.WaitGroup p := mpb.New(mpb.WithWaitGroup(&wg), mpb.WithDebugOutput(os.Stderr)) - wantPanic := "Some really long panic panic panic panic panic panic panic, really it is very long" + wantPanic := strings.Repeat("Panic ", 64) numBars := 3 wg.Add(numBars) @@ -35,8 +36,8 @@ } func panicDecorator(name, panicMsg string) decor.Decorator { - return decor.Any(func(s *decor.Statistics) string { - if s.ID == 1 && s.Current >= 42 { + return decor.Any(func(st decor.Statistics) string { + if st.ID == 1 && st.Current >= 42 { panic(panicMsg) } return name diff --git a/_examples/poplog/go.mod b/_examples/poplog/go.mod index a6ab5a9..d1de4d5 100644 --- a/_examples/poplog/go.mod +++ b/_examples/poplog/go.mod @@ -2,4 +2,4 @@ go 1.14 -require github.com/vbauerster/mpb/v5 v5.0.3 +require github.com/vbauerster/mpb/v6 v6.0.3 diff --git a/_examples/poplog/main.go b/_examples/poplog/main.go index 3ff054a..d787444 100644 --- a/_examples/poplog/main.go +++ b/_examples/poplog/main.go @@ -2,69 +2,44 @@ import ( "fmt" - "io" "math/rand" - "sync" "time" - "github.com/vbauerster/mpb/v5" - "github.com/vbauerster/mpb/v5/decor" + "github.com/vbauerster/mpb/v6" + "github.com/vbauerster/mpb/v6/decor" ) func main() { p := mpb.New(mpb.PopCompletedMode()) - total, numBars := 100, 2 + total, numBars := 100, 4 for i := 0; i < numBars; i++ { name := fmt.Sprintf("Bar#%d:", i) bar := p.AddBar(int64(total), - mpb.BarNoPop(), + mpb.BarFillerOnComplete(fmt.Sprintf("%s has been completed", name)), + mpb.BarFillerTrim(), mpb.PrependDecorators( - decor.Name(name), - decor.Percentage(decor.WCSyncSpace), + decor.OnComplete(decor.Name(name), ""), + decor.OnComplete(decor.NewPercentage(" % d "), ""), ), mpb.AppendDecorators( - decor.OnComplete( - decor.EwmaETA(decor.ET_STYLE_GO, 60), "done!", - ), + decor.OnComplete(decor.Name(" "), ""), + decor.OnComplete(decor.EwmaETA(decor.ET_STYLE_GO, 60), ""), ), ) // simulating some work - go func() { - rng := rand.New(rand.NewSource(time.Now().UnixNano())) - max := 100 * time.Millisecond - for i := 0; i < total; i++ { - // start variable is solely for EWMA calculation - // EWMA's unit of measure is an iteration's duration - start := time.Now() - time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10) - bar.Increment() - // we need to call DecoratorEwmaUpdate to fulfill ewma decorator's contract - bar.DecoratorEwmaUpdate(time.Since(start)) - } - }() + rng := rand.New(rand.NewSource(time.Now().UnixNano())) + max := 100 * time.Millisecond + for i := 0; i < total; i++ { + // start variable is solely for EWMA calculation + // EWMA's unit of measure is an iteration's duration + start := time.Now() + time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10) + bar.Increment() + // we need to call DecoratorEwmaUpdate to fulfill ewma decorator's contract + bar.DecoratorEwmaUpdate(time.Since(start)) + } } - var wg sync.WaitGroup - wg.Add(1) - go func() { - defer wg.Done() - rng := rand.New(rand.NewSource(time.Now().UnixNano())) - max := 3000 * time.Millisecond - for i := 0; i < 10; i++ { - filler := makeLogBar(fmt.Sprintf("some log: %d", i)) - p.Add(0, filler).SetTotal(0, true) - time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10) - } - }() - - wg.Wait() p.Wait() } - -func makeLogBar(msg string) mpb.BarFiller { - limit := "%%.%ds" - return mpb.BarFillerFunc(func(w io.Writer, width int, st *decor.Statistics) { - fmt.Fprintf(w, fmt.Sprintf(limit, width), msg) - }) -} diff --git a/_examples/quietMode/go.mod b/_examples/quietMode/go.mod index c856e05..03d30bb 100644 --- a/_examples/quietMode/go.mod +++ b/_examples/quietMode/go.mod @@ -2,4 +2,4 @@ go 1.14 -require github.com/vbauerster/mpb/v5 v5.0.3 +require github.com/vbauerster/mpb/v6 v6.0.3 diff --git a/_examples/quietMode/main.go b/_examples/quietMode/main.go index a918175..3d1b3cb 100644 --- a/_examples/quietMode/main.go +++ b/_examples/quietMode/main.go @@ -7,8 +7,8 @@ "sync" "time" - "github.com/vbauerster/mpb/v5" - "github.com/vbauerster/mpb/v5/decor" + "github.com/vbauerster/mpb/v6" + "github.com/vbauerster/mpb/v6/decor" ) var quietMode bool @@ -23,13 +23,13 @@ // pass &wg (optional), so p will wait for it eventually p := mpb.New( mpb.WithWaitGroup(&wg), - mpb.ContainerOptOn( + mpb.ContainerOptional( // setting to nil will: - // set output to ioutil.Discard and disable internal refresh rate - // cycling, in order to not consume much CPU, hovewer a single refresh - // still will be triggered on bar complete event, per each bar. + // set output to ioutil.Discard and disable refresh rate cycle, in + // order not to consume much CPU. Hovewer a single refresh still will + // be triggered on bar complete event, per each bar. mpb.WithOutput(nil), - func() bool { return quietMode }, + quietMode, ), ) total, numBars := 100, 3 diff --git a/_examples/remove/go.mod b/_examples/remove/go.mod index 2f0ce70..48bee75 100644 --- a/_examples/remove/go.mod +++ b/_examples/remove/go.mod @@ -2,4 +2,4 @@ go 1.14 -require github.com/vbauerster/mpb/v5 v5.0.3 +require github.com/vbauerster/mpb/v6 v6.0.3 diff --git a/_examples/remove/main.go b/_examples/remove/main.go index 1836ee8..ab8d90d 100644 --- a/_examples/remove/main.go +++ b/_examples/remove/main.go @@ -6,8 +6,8 @@ "sync" "time" - "github.com/vbauerster/mpb/v5" - "github.com/vbauerster/mpb/v5/decor" + "github.com/vbauerster/mpb/v6" + "github.com/vbauerster/mpb/v6/decor" ) func main() { @@ -21,7 +21,7 @@ name := fmt.Sprintf("Bar#%d:", i) bar := p.AddBar(int64(total), mpb.BarID(i), - mpb.BarOptOn(mpb.BarRemoveOnComplete(), func() bool { return i == 0 }), + mpb.BarOptional(mpb.BarRemoveOnComplete(), i == 0), mpb.PrependDecorators( decor.Name(name), decor.EwmaETA(decor.ET_STYLE_GO, 60, decor.WCSyncSpace), diff --git a/_examples/reverseBar/go.mod b/_examples/reverseBar/go.mod index b279926..8e07e8c 100644 --- a/_examples/reverseBar/go.mod +++ b/_examples/reverseBar/go.mod @@ -2,4 +2,4 @@ go 1.14 -require github.com/vbauerster/mpb/v5 v5.0.3 +require github.com/vbauerster/mpb/v6 v6.0.3 diff --git a/_examples/reverseBar/main.go b/_examples/reverseBar/main.go index 2993f50..23450cc 100644 --- a/_examples/reverseBar/main.go +++ b/_examples/reverseBar/main.go @@ -6,8 +6,8 @@ "sync" "time" - "github.com/vbauerster/mpb/v5" - "github.com/vbauerster/mpb/v5/decor" + "github.com/vbauerster/mpb/v6" + "github.com/vbauerster/mpb/v6/decor" ) func main() { @@ -19,9 +19,9 @@ for i := 0; i < numBars; i++ { name := fmt.Sprintf("Bar#%d:", i) - bar := p.AddBar(int64(total), + bar := p.Add(int64(total), // reverse Bar#1 - mpb.BarOptOn(mpb.BarReverse(), func() bool { return i == 1 }), + mpb.NewBarFillerPick("", i == 1), mpb.PrependDecorators( // simple name decorator decor.Name(name), diff --git a/_examples/singleBar/go.mod b/_examples/singleBar/go.mod index c2a7d8c..6cd0933 100644 --- a/_examples/singleBar/go.mod +++ b/_examples/singleBar/go.mod @@ -2,4 +2,4 @@ go 1.14 -require github.com/vbauerster/mpb/v5 v5.0.3 +require github.com/vbauerster/mpb/v6 v6.0.3 diff --git a/_examples/singleBar/main.go b/_examples/singleBar/main.go index 5c6029f..f0ba8ea 100644 --- a/_examples/singleBar/main.go +++ b/_examples/singleBar/main.go @@ -4,8 +4,8 @@ "math/rand" "time" - "github.com/vbauerster/mpb/v5" - "github.com/vbauerster/mpb/v5/decor" + "github.com/vbauerster/mpb/v6" + "github.com/vbauerster/mpb/v6/decor" ) func main() { @@ -15,9 +15,9 @@ total := 100 name := "Single Bar:" // adding a single bar, which will inherit container's width - bar := p.AddBar(int64(total), - // override DefaultBarStyle, which is "[=>-]<+" - mpb.BarStyle("╢▌▌░╟"), + bar := p.Add(int64(total), + // progress bar filler with customized style + mpb.NewBarFiller("╢▌▌░╟"), mpb.PrependDecorators( // display our name with one space on the right decor.Name(name, decor.WC{W: len(name) + 1, C: decor.DidentRight}), diff --git a/_examples/spinnerBar/go.mod b/_examples/spinnerBar/go.mod index 05b8407..4f28efb 100644 --- a/_examples/spinnerBar/go.mod +++ b/_examples/spinnerBar/go.mod @@ -2,4 +2,4 @@ go 1.14 -require github.com/vbauerster/mpb/v5 v5.0.3 +require github.com/vbauerster/mpb/v6 v6.0.3 diff --git a/_examples/spinnerBar/main.go b/_examples/spinnerBar/main.go index fb4d910..7867037 100644 --- a/_examples/spinnerBar/main.go +++ b/_examples/spinnerBar/main.go @@ -6,26 +6,27 @@ "sync" "time" - "github.com/vbauerster/mpb/v5" - "github.com/vbauerster/mpb/v5/decor" + "github.com/vbauerster/mpb/v6" + "github.com/vbauerster/mpb/v6/decor" ) func main() { var wg sync.WaitGroup p := mpb.New( mpb.WithWaitGroup(&wg), - mpb.WithWidth(13), + mpb.WithWidth(14), ) total, numBars := 101, 3 wg.Add(numBars) + + spinnerStyle := []string{"∙∙∙", "●∙∙", "∙●∙", "∙∙●", "∙∙∙"} for i := 0; i < numBars; i++ { name := fmt.Sprintf("Bar#%d:", i) var bar *mpb.Bar if i == 0 { - bar = p.AddBar(int64(total), - // override mpb.DefaultBarStyle, which is "[=>-]<+" - mpb.BarStyle("╢▌▌░╟"), + bar = p.Add(int64(total), + mpb.NewBarFiller("╢▌▌░╟"), mpb.PrependDecorators( // simple name decorator decor.Name(name), @@ -39,9 +40,8 @@ ), ) } else { - bar = p.AddSpinner(int64(total), mpb.SpinnerOnMiddle, - // override mpb.DefaultSpinnerStyle - mpb.SpinnerStyle([]string{"∙∙∙", "●∙∙", "∙●∙", "∙∙●", "∙∙∙"}), + bar = p.Add(int64(total), + mpb.NewSpinnerFiller(spinnerStyle, mpb.SpinnerOnMiddle), mpb.PrependDecorators( // simple name decorator decor.Name(name), diff --git a/_examples/spinnerDecorator/go.mod b/_examples/spinnerDecorator/go.mod index 5fe9df5..4bb0416 100644 --- a/_examples/spinnerDecorator/go.mod +++ b/_examples/spinnerDecorator/go.mod @@ -2,4 +2,4 @@ go 1.14 -require github.com/vbauerster/mpb/v5 v5.0.3 +require github.com/vbauerster/mpb/v6 v6.0.3 diff --git a/_examples/spinnerDecorator/main.go b/_examples/spinnerDecorator/main.go index d0499e0..03fcd72 100644 --- a/_examples/spinnerDecorator/main.go +++ b/_examples/spinnerDecorator/main.go @@ -6,8 +6,8 @@ "sync" "time" - "github.com/vbauerster/mpb/v5" - "github.com/vbauerster/mpb/v5/decor" + "github.com/vbauerster/mpb/v6" + "github.com/vbauerster/mpb/v6/decor" ) func main() { diff --git a/_examples/stress/go.mod b/_examples/stress/go.mod index c391186..a7f5409 100644 --- a/_examples/stress/go.mod +++ b/_examples/stress/go.mod @@ -2,4 +2,4 @@ go 1.14 -require github.com/vbauerster/mpb/v5 v5.0.3 +require github.com/vbauerster/mpb/v6 v6.0.3 diff --git a/_examples/stress/main.go b/_examples/stress/main.go index ec46fd3..1dd199f 100644 --- a/_examples/stress/main.go +++ b/_examples/stress/main.go @@ -6,8 +6,8 @@ "sync" "time" - "github.com/vbauerster/mpb/v5" - "github.com/vbauerster/mpb/v5/decor" + "github.com/vbauerster/mpb/v6" + "github.com/vbauerster/mpb/v6/decor" ) const ( diff --git a/_examples/suppressBar/go.mod b/_examples/suppressBar/go.mod index 93845c4..0d10445 100644 --- a/_examples/suppressBar/go.mod +++ b/_examples/suppressBar/go.mod @@ -2,4 +2,7 @@ go 1.14 -require github.com/vbauerster/mpb/v5 v5.0.3 +require ( + github.com/mattn/go-runewidth v0.0.10 + github.com/vbauerster/mpb/v6 v6.0.3 +) diff --git a/_examples/suppressBar/main.go b/_examples/suppressBar/main.go index 906f2f9..f3dfc56 100644 --- a/_examples/suppressBar/main.go +++ b/_examples/suppressBar/main.go @@ -8,8 +8,9 @@ "sync" "time" - "github.com/vbauerster/mpb/v5" - "github.com/vbauerster/mpb/v5/decor" + "github.com/mattn/go-runewidth" + "github.com/vbauerster/mpb/v6" + "github.com/vbauerster/mpb/v6/decor" ) func main() { @@ -18,14 +19,31 @@ total := 100 msgCh := make(chan string) resumeCh := make(chan struct{}) - filler, nextCh := newCustomFiller(msgCh, resumeCh) - bar := p.Add(int64(total), filler, - mpb.PrependDecorators( - decor.Name("my bar:"), - ), - mpb.AppendDecorators( - newCustomPercentage(nextCh), - ), + nextCh := make(chan struct{}, 1) + bar := p.AddBar(int64(total), + mpb.BarFillerMiddleware(func(base mpb.BarFiller) mpb.BarFiller { + var msg *string + return mpb.BarFillerFunc(func(w io.Writer, reqWidth int, st decor.Statistics) { + select { + case m := <-msgCh: + defer func() { + msg = &m + }() + nextCh <- struct{}{} + case <-resumeCh: + msg = nil + default: + } + if msg != nil { + io.WriteString(w, runewidth.Truncate(*msg, st.AvailableWidth, "…")) + nextCh <- struct{}{} + } else { + base.Fill(w, reqWidth, st) + } + }) + }), + mpb.PrependDecorators(decor.Name("my bar:")), + mpb.AppendDecorators(newCustomPercentage(nextCh)), ) ew := &errorWrapper{} time.AfterFunc(2*time.Second, func() { @@ -76,54 +94,15 @@ ew.Unlock() } -type myBarFiller struct { - mpb.BarFiller - base mpb.BarFiller -} - -func (cf *myBarFiller) Base() mpb.BarFiller { - return cf.base -} - -func newCustomFiller(ch <-chan string, resume <-chan struct{}) (mpb.BarFiller, <-chan struct{}) { - base := mpb.NewBarFiller(mpb.DefaultBarStyle, false) - nextCh := make(chan struct{}, 1) - var msg *string - filler := mpb.BarFillerFunc(func(w io.Writer, width int, st *decor.Statistics) { +func newCustomPercentage(nextCh <-chan struct{}) decor.Decorator { + base := decor.Percentage() + fn := func(s decor.Statistics) string { select { - case m := <-ch: - defer func() { - msg = &m - }() - nextCh <- struct{}{} - case <-resume: - msg = nil - default: - } - if msg != nil { - limitFmt := fmt.Sprintf("%%.%ds", width) - fmt.Fprintf(w, limitFmt, *msg) - nextCh <- struct{}{} - } else { - base.Fill(w, width, st) - } - }) - cf := &myBarFiller{ - BarFiller: filler, - base: base, - } - return cf, nextCh -} - -func newCustomPercentage(ch <-chan struct{}) decor.Decorator { - base := decor.Percentage() - f := func(s *decor.Statistics) string { - select { - case <-ch: + case <-nextCh: return "" default: return base.Decor(s) } } - return decor.Any(f) + return decor.Any(fn) } diff --git a/bar.go b/bar.go index 1a4c66f..f18ef96 100644 --- a/bar.go +++ b/bar.go @@ -6,29 +6,16 @@ "fmt" "io" "log" + "runtime/debug" "strings" "time" - "unicode/utf8" - - "github.com/vbauerster/mpb/v5/decor" + + "github.com/acarl005/stripansi" + "github.com/mattn/go-runewidth" + "github.com/vbauerster/mpb/v6/decor" ) -// BarFiller interface. -// Bar renders itself by calling BarFiller's Fill method. You can -// literally have any bar kind, by implementing this interface and -// passing it to the *Progress.Add(...) *Bar method. -type BarFiller interface { - Fill(w io.Writer, width int, stat *decor.Statistics) -} - -// BarFillerFunc is function type adapter to convert function into Filler. -type BarFillerFunc func(w io.Writer, width int, stat *decor.Statistics) - -func (f BarFillerFunc) Fill(w io.Writer, width int, stat *decor.Statistics) { - f(w, width, stat) -} - -// Bar represents a progress Bar. +// Bar represents a progress bar. type Bar struct { priority int // used by heap index int // used by heap @@ -55,20 +42,24 @@ recoveredPanic interface{} } -type extFunc func(in io.Reader, tw int, st *decor.Statistics) (out io.Reader, lines int) - +type extenderFunc func(in io.Reader, reqWidth int, st decor.Statistics) (out io.Reader, lines int) + +// bState is actual bar state. It gets passed to *Bar.serve(...) monitor +// goroutine. type bState struct { - baseF BarFiller - filler BarFiller id int - width int + priority int + reqWidth int total int64 current int64 + refill int64 lastN int64 iterated bool trimSpace bool - toComplete bool + completed bool completeFlushed bool + triggerComplete bool + dropOnComplete bool noPop bool aDecorators []decor.Decorator pDecorators []decor.Decorator @@ -76,12 +67,10 @@ ewmaDecorators []decor.EwmaDecorator shutdownListeners []decor.ShutdownListener bufP, bufB, bufA *bytes.Buffer - extender extFunc - - // priority overrides *Bar's priority, if set - priority int - // dropOnComplete propagates to *Bar - dropOnComplete bool + filler BarFiller + middleware func(BarFiller) BarFiller + extender extenderFunc + // runningBar is a key for *pState.parkedBars runningBar *Bar @@ -99,7 +88,7 @@ noPop: bs.noPop, operateState: make(chan func(*bState)), frameCh: make(chan io.Reader, 1), - syncTableCh: make(chan [][]chan int), + syncTableCh: make(chan [][]chan int, 1), completed: make(chan bool, 1), done: make(chan struct{}), cancel: cancel, @@ -141,23 +130,23 @@ } } -// SetRefill fills bar with refill rune up to amount argument. -// Given default bar style is "[=>-]<+", refill rune is '+'. -// To set bar style use mpb.BarStyle(string) BarOption. +// SetRefill sets refill flag with specified amount. +// The underlying BarFiller will change its visual representation, to +// indicate refill event. Refill event may be referred to some retry +// operation for example. func (b *Bar) SetRefill(amount int64) { - type refiller interface { - SetRefill(int64) - } - b.operateState <- func(s *bState) { - if f, ok := s.baseF.(refiller); ok { - f.SetRefill(amount) - } + select { + case b.operateState <- func(s *bState) { + s.refill = amount + }: + case <-b.done: } } // TraverseDecorators traverses all available decorators and calls cb func on each. func (b *Bar) TraverseDecorators(cb func(decor.Decorator)) { - b.operateState <- func(s *bState) { + select { + case b.operateState <- func(s *bState) { for _, decorators := range [...][]decor.Decorator{ s.pDecorators, s.aDecorators, @@ -166,23 +155,25 @@ cb(extractBaseDecorator(d)) } } + }: + case <-b.done: } } // SetTotal sets total dynamically. -// If total is less or equal to zero it takes progress' current value. -// If complete is true, complete event will be triggered. -func (b *Bar) SetTotal(total int64, complete bool) { - select { - case b.operateState <- func(s *bState) { +// If total is less than or equal to zero it takes progress' current value. +func (b *Bar) SetTotal(total int64, triggerComplete bool) { + select { + case b.operateState <- func(s *bState) { + s.triggerComplete = triggerComplete if total <= 0 { s.total = s.current } else { s.total = total } - if complete && !s.toComplete { + if s.triggerComplete && !s.completed { s.current = s.total - s.toComplete = true + s.completed = true go b.refreshTillShutdown() } }: @@ -191,15 +182,16 @@ } // SetCurrent sets progress' current to an arbitrary value. +// Setting a negative value will cause a panic. func (b *Bar) SetCurrent(current int64) { select { case b.operateState <- func(s *bState) { s.iterated = true s.lastN = current - s.current s.current = current - if s.total > 0 && s.current >= s.total { + if s.triggerComplete && s.current >= s.total { s.current = s.total - s.toComplete = true + s.completed = true go b.refreshTillShutdown() } }: @@ -224,9 +216,9 @@ s.iterated = true s.lastN = n s.current += n - if s.total > 0 && s.current >= s.total { + if s.triggerComplete && s.current >= s.total { s.current = s.total - s.toComplete = true + s.completed = true go b.refreshTillShutdown() } }: @@ -290,7 +282,7 @@ // Completed reports whether the bar is in completed state. func (b *Bar) Completed() bool { select { - case b.operateState <- func(s *bState) { b.completed <- s.toComplete }: + case b.operateState <- func(s *bState) { b.completed <- s.completed }: return <-b.completed case <-b.done: return true @@ -316,42 +308,40 @@ } func (b *Bar) render(tw int) { - if b.recoveredPanic != nil { - b.toShutdown = false - b.frameCh <- b.panicToFrame(tw) - return - } - select { - case b.operateState <- func(s *bState) { + select { + case b.operateState <- func(s *bState) { + stat := newStatistics(tw, s) defer func() { // recovering if user defined decorator panics for example if p := recover(); p != nil { + if b.recoveredPanic == nil { + s.extender = makePanicExtender(p) + b.toShutdown = !b.toShutdown + b.recoveredPanic = p + } + frame, lines := s.extender(nil, s.reqWidth, stat) + b.extendedLines = lines + b.frameCh <- frame b.dlogger.Println(p) - b.recoveredPanic = p - b.toShutdown = !s.completeFlushed - b.frameCh <- b.panicToFrame(tw) } + s.completeFlushed = s.completed }() - - st := newStatistics(s) - frame := s.draw(tw, st) - frame, b.extendedLines = s.extender(frame, tw, st) - - b.toShutdown = s.toComplete && !s.completeFlushed - s.completeFlushed = s.toComplete + frame, lines := s.extender(s.draw(stat), s.reqWidth, stat) + b.extendedLines = lines + b.toShutdown = s.completed && !s.completeFlushed b.frameCh <- frame }: case <-b.done: s := b.cacheState - st := newStatistics(s) - frame := s.draw(tw, st) - frame, b.extendedLines = s.extender(frame, tw, st) + stat := newStatistics(tw, s) + var r io.Reader + if b.recoveredPanic == nil { + r = s.draw(stat) + } + frame, lines := s.extender(r, s.reqWidth, stat) + b.extendedLines = lines b.frameCh <- frame } -} - -func (b *Bar) panicToFrame(termWidth int) io.Reader { - return strings.NewReader(fmt.Sprintf(fmt.Sprintf("%%.%dv\n", termWidth), b.recoveredPanic)) } func (b *Bar) subscribeDecorators() { @@ -369,12 +359,15 @@ shutdownListeners = append(shutdownListeners, d) } }) - b.operateState <- func(s *bState) { + select { + case b.operateState <- func(s *bState) { s.averageDecorators = averageDecorators s.ewmaDecorators = ewmaDecorators s.shutdownListeners = shutdownListeners - } - b.hasEwmaDecorators = len(ewmaDecorators) != 0 + }: + b.hasEwmaDecorators = len(ewmaDecorators) != 0 + case <-b.done: + } } func (b *Bar) refreshTillShutdown() { @@ -396,34 +389,41 @@ } } -func (s *bState) draw(termWidth int, stat *decor.Statistics) io.Reader { +func (s *bState) draw(stat decor.Statistics) io.Reader { + if !s.trimSpace { + stat.AvailableWidth -= 2 + s.bufB.WriteByte(' ') + defer s.bufB.WriteByte(' ') + } + + nlr := strings.NewReader("\n") + tw := stat.AvailableWidth for _, d := range s.pDecorators { - s.bufP.WriteString(d.Decor(stat)) - } - + str := d.Decor(stat) + stat.AvailableWidth -= runewidth.StringWidth(stripansi.Strip(str)) + s.bufP.WriteString(str) + } + if stat.AvailableWidth <= 0 { + trunc := strings.NewReader(runewidth.Truncate(stripansi.Strip(s.bufP.String()), tw, "…")) + s.bufP.Reset() + return io.MultiReader(trunc, s.bufB, nlr) + } + + tw = stat.AvailableWidth for _, d := range s.aDecorators { - s.bufA.WriteString(d.Decor(stat)) - } - - s.bufA.WriteByte('\n') - - prependCount := utf8.RuneCount(s.bufP.Bytes()) - appendCount := utf8.RuneCount(s.bufA.Bytes()) - 1 - - if fitWidth := s.width; termWidth > 1 { - if !s.trimSpace { - // reserve space for edge spaces - termWidth -= 2 - s.bufB.WriteByte(' ') - defer s.bufB.WriteByte(' ') - } - if prependCount+s.width+appendCount > termWidth { - fitWidth = termWidth - prependCount - appendCount - } - s.filler.Fill(s.bufB, fitWidth, stat) - } - - return io.MultiReader(s.bufP, s.bufB, s.bufA) + str := d.Decor(stat) + stat.AvailableWidth -= runewidth.StringWidth(stripansi.Strip(str)) + s.bufA.WriteString(str) + } + if stat.AvailableWidth <= 0 { + trunc := strings.NewReader(runewidth.Truncate(stripansi.Strip(s.bufA.String()), tw, "…")) + s.bufA.Reset() + return io.MultiReader(s.bufP, s.bufB, trunc, nlr) + } + + s.filler.Fill(s.bufB, s.reqWidth, stat) + + return io.MultiReader(s.bufP, s.bufB, s.bufA, nlr) } func (s *bState) wSyncTable() [][]chan int { @@ -448,12 +448,14 @@ return table } -func newStatistics(s *bState) *decor.Statistics { - return &decor.Statistics{ - ID: s.id, - Completed: s.completeFlushed, - Total: s.total, - Current: s.current, +func newStatistics(tw int, s *bState) decor.Statistics { + return decor.Statistics{ + ID: s.id, + AvailableWidth: tw, + Total: s.total, + Current: s.current, + Refill: s.refill, + Completed: s.completeFlushed, } } @@ -474,3 +476,17 @@ d.EwmaUpdate(s.lastN, dur) } } + +func makePanicExtender(p interface{}) extenderFunc { + pstr := fmt.Sprint(p) + stack := debug.Stack() + stackLines := bytes.Count(stack, []byte("\n")) + return func(_ io.Reader, _ int, st decor.Statistics) (io.Reader, int) { + mr := io.MultiReader( + strings.NewReader(runewidth.Truncate(pstr, st.AvailableWidth, "…")), + strings.NewReader(fmt.Sprintf("\n%#v\n", st)), + bytes.NewReader(stack), + ) + return mr, stackLines + 1 + } +} diff --git a/bar_filler.go b/bar_filler.go index 00bf0a4..c8cedaa 100644 --- a/bar_filler.go +++ b/bar_filler.go @@ -2,137 +2,30 @@ import ( "io" - "unicode/utf8" - "github.com/vbauerster/mpb/v5/decor" - "github.com/vbauerster/mpb/v5/internal" + "github.com/vbauerster/mpb/v6/decor" ) -const ( - rLeft = iota - rFill - rTip - rEmpty - rRight - rRevTip - rRefill -) - -// DefaultBarStyle is a string containing 7 runes. -// Each rune is a building block of a progress bar. +// BarFiller interface. +// Bar (without decorators) renders itself by calling BarFiller's Fill method. // -// '1st rune' stands for left boundary rune +// reqWidth is requested width, set by `func WithWidth(int) ContainerOption`. +// If not set, it defaults to terminal width. // -// '2nd rune' stands for fill rune +// Default implementations can be obtained via: // -// '3rd rune' stands for tip rune +// func NewBarFiller(style string) BarFiller +// func NewBarFillerRev(style string) BarFiller +// func NewBarFillerPick(style string, rev bool) BarFiller +// func NewSpinnerFiller(style []string, alignment SpinnerAlignment) BarFiller // -// '4th rune' stands for empty rune -// -// '5th rune' stands for right boundary rune -// -// '6th rune' stands for reverse tip rune -// -// '7th rune' stands for refill rune -// -const DefaultBarStyle string = "[=>-]<+" - -type barFiller struct { - format [][]byte - tip []byte - refill int64 - reverse bool - flush func(w io.Writer, bb [][]byte) +type BarFiller interface { + Fill(w io.Writer, reqWidth int, stat decor.Statistics) } -// NewBarFiller constucts mpb.BarFiller, to be used with *Progress.Add(...) *Bar method. -func NewBarFiller(style string, reverse bool) BarFiller { - if style == "" { - style = DefaultBarStyle - } - bf := &barFiller{ - format: make([][]byte, utf8.RuneCountInString(style)), - reverse: reverse, - } - bf.SetStyle(style) - return bf +// BarFillerFunc is function type adapter to convert function into BarFiller. +type BarFillerFunc func(w io.Writer, reqWidth int, stat decor.Statistics) + +func (f BarFillerFunc) Fill(w io.Writer, reqWidth int, stat decor.Statistics) { + f(w, reqWidth, stat) } - -func (s *barFiller) SetStyle(style string) { - if !utf8.ValidString(style) { - return - } - src := make([][]byte, 0, utf8.RuneCountInString(style)) - for _, r := range style { - src = append(src, []byte(string(r))) - } - copy(s.format, src) - s.SetReverse(s.reverse) -} - -func (s *barFiller) SetReverse(reverse bool) { - if reverse { - s.tip = s.format[rRevTip] - s.flush = reverseFlush - } else { - s.tip = s.format[rTip] - s.flush = normalFlush - } - s.reverse = reverse -} - -func (s *barFiller) SetRefill(amount int64) { - s.refill = amount -} - -func (s *barFiller) Fill(w io.Writer, width int, stat *decor.Statistics) { - // don't count rLeft and rRight as progress - width -= 2 - if width < 2 { - return - } - w.Write(s.format[rLeft]) - defer w.Write(s.format[rRight]) - - bb := make([][]byte, width) - - cwidth := int(internal.PercentageRound(stat.Total, stat.Current, width)) - - for i := 0; i < cwidth; i++ { - bb[i] = s.format[rFill] - } - - if s.refill > 0 { - var rwidth int - if s.refill > stat.Current { - rwidth = cwidth - } else { - rwidth = int(internal.PercentageRound(stat.Total, int64(s.refill), width)) - } - for i := 0; i < rwidth; i++ { - bb[i] = s.format[rRefill] - } - } - - if cwidth > 0 && cwidth < width { - bb[cwidth-1] = s.tip - } - - for i := cwidth; i < width; i++ { - bb[i] = s.format[rEmpty] - } - - s.flush(w, bb) -} - -func normalFlush(w io.Writer, bb [][]byte) { - for i := 0; i < len(bb); i++ { - w.Write(bb[i]) - } -} - -func reverseFlush(w io.Writer, bb [][]byte) { - for i := len(bb) - 1; i >= 0; i-- { - w.Write(bb[i]) - } -} diff --git a/bar_filler_bar.go b/bar_filler_bar.go new file mode 100644 index 0000000..1c339e9 --- /dev/null +++ b/bar_filler_bar.go @@ -0,0 +1,191 @@ +package mpb + +import ( + "bytes" + "io" + "unicode/utf8" + + "github.com/mattn/go-runewidth" + "github.com/rivo/uniseg" + "github.com/vbauerster/mpb/v6/decor" + "github.com/vbauerster/mpb/v6/internal" +) + +const ( + rLeft = iota + rFill + rTip + rSpace + rRight + rRevTip + rRefill +) + +// BarDefaultStyle is a style for rendering a progress bar. +// It consist of 7 ordered runes: +// +// '1st rune' stands for left boundary rune +// +// '2nd rune' stands for fill rune +// +// '3rd rune' stands for tip rune +// +// '4th rune' stands for space rune +// +// '5th rune' stands for right boundary rune +// +// '6th rune' stands for reverse tip rune +// +// '7th rune' stands for refill rune +// +const BarDefaultStyle string = "[=>-]<+" + +type barFiller struct { + format [][]byte + rwidth []int + tip []byte + refill int64 + reverse bool + flush func(io.Writer, *space, [][]byte) +} + +type space struct { + space []byte + rwidth int + count int +} + +// NewBarFiller returns a BarFiller implementation which renders a +// progress bar in regular direction. If style is empty string, +// BarDefaultStyle is applied. To be used with `*Progress.Add(...) +// *Bar` method. +func NewBarFiller(style string) BarFiller { + return newBarFiller(style, false) +} + +// NewBarFillerRev returns a BarFiller implementation which renders a +// progress bar in reverse direction. If style is empty string, +// BarDefaultStyle is applied. To be used with `*Progress.Add(...) +// *Bar` method. +func NewBarFillerRev(style string) BarFiller { + return newBarFiller(style, true) +} + +// NewBarFillerPick pick between regular and reverse BarFiller implementation +// based on rev param. To be used with `*Progress.Add(...) *Bar` method. +func NewBarFillerPick(style string, rev bool) BarFiller { + return newBarFiller(style, rev) +} + +func newBarFiller(style string, rev bool) BarFiller { + bf := &barFiller{ + format: make([][]byte, len(BarDefaultStyle)), + rwidth: make([]int, len(BarDefaultStyle)), + reverse: rev, + } + bf.parse(BarDefaultStyle) + if style != "" && style != BarDefaultStyle { + bf.parse(style) + } + return bf +} + +func (s *barFiller) parse(style string) { + if !utf8.ValidString(style) { + panic("invalid bar style") + } + srcFormat := make([][]byte, len(BarDefaultStyle)) + srcRwidth := make([]int, len(BarDefaultStyle)) + i := 0 + for gr := uniseg.NewGraphemes(style); i < len(BarDefaultStyle) && gr.Next(); i++ { + srcFormat[i] = gr.Bytes() + srcRwidth[i] = runewidth.StringWidth(gr.Str()) + } + copy(s.format, srcFormat[:i]) + copy(s.rwidth, srcRwidth[:i]) + if s.reverse { + s.tip = s.format[rRevTip] + s.flush = reverseFlush + } else { + s.tip = s.format[rTip] + s.flush = regularFlush + } +} + +func (s *barFiller) Fill(w io.Writer, reqWidth int, stat decor.Statistics) { + width := internal.CheckRequestedWidth(reqWidth, stat.AvailableWidth) + brackets := s.rwidth[rLeft] + s.rwidth[rRight] + if width < brackets { + return + } + // don't count brackets as progress + width -= brackets + + w.Write(s.format[rLeft]) + defer w.Write(s.format[rRight]) + + cwidth := int(internal.PercentageRound(stat.Total, stat.Current, width)) + space := &space{ + space: s.format[rSpace], + rwidth: s.rwidth[rSpace], + count: width - cwidth, + } + + index, refill := 0, 0 + bb := make([][]byte, cwidth) + + if cwidth > 0 && cwidth != width { + bb[index] = s.tip + cwidth -= s.rwidth[rTip] + index++ + } + + if stat.Refill > 0 { + refill = int(internal.PercentageRound(stat.Total, int64(stat.Refill), width)) + if refill > cwidth { + refill = cwidth + } + cwidth -= refill + } + + for cwidth > 0 { + bb[index] = s.format[rFill] + cwidth -= s.rwidth[rFill] + index++ + } + + for refill > 0 { + bb[index] = s.format[rRefill] + refill -= s.rwidth[rRefill] + index++ + } + + if cwidth+refill < 0 || space.rwidth > 1 { + buf := new(bytes.Buffer) + s.flush(buf, space, bb[:index]) + io.WriteString(w, runewidth.Truncate(buf.String(), width, "…")) + return + } + + s.flush(w, space, bb) +} + +func regularFlush(w io.Writer, space *space, bb [][]byte) { + for i := len(bb) - 1; i >= 0; i-- { + w.Write(bb[i]) + } + for space.count > 0 { + w.Write(space.space) + space.count -= space.rwidth + } +} + +func reverseFlush(w io.Writer, space *space, bb [][]byte) { + for space.count > 0 { + w.Write(space.space) + space.count -= space.rwidth + } + for i := 0; i < len(bb); i++ { + w.Write(bb[i]) + } +} diff --git a/bar_filler_spinner.go b/bar_filler_spinner.go new file mode 100644 index 0000000..0817b19 --- /dev/null +++ b/bar_filler_spinner.go @@ -0,0 +1,65 @@ +package mpb + +import ( + "io" + "strings" + + "github.com/mattn/go-runewidth" + "github.com/vbauerster/mpb/v6/decor" + "github.com/vbauerster/mpb/v6/internal" +) + +// SpinnerAlignment enum. +type SpinnerAlignment int + +// SpinnerAlignment kinds. +const ( + SpinnerOnLeft SpinnerAlignment = iota + SpinnerOnMiddle + SpinnerOnRight +) + +// SpinnerDefaultStyle is a style for rendering a spinner. +var SpinnerDefaultStyle = []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"} + +type spinnerFiller struct { + frames []string + count uint + alignment SpinnerAlignment +} + +// NewSpinnerFiller returns a BarFiller implementation which renders +// a spinner. If style is nil or zero length, SpinnerDefaultStyle is +// applied. To be used with `*Progress.Add(...) *Bar` method. +func NewSpinnerFiller(style []string, alignment SpinnerAlignment) BarFiller { + if len(style) == 0 { + style = SpinnerDefaultStyle + } + filler := &spinnerFiller{ + frames: style, + alignment: alignment, + } + return filler +} + +func (s *spinnerFiller) Fill(w io.Writer, reqWidth int, stat decor.Statistics) { + width := internal.CheckRequestedWidth(reqWidth, stat.AvailableWidth) + + frame := s.frames[s.count%uint(len(s.frames))] + frameWidth := runewidth.StringWidth(frame) + + if width < frameWidth { + return + } + + switch rest := width - frameWidth; s.alignment { + case SpinnerOnLeft: + io.WriteString(w, frame+strings.Repeat(" ", rest)) + case SpinnerOnMiddle: + str := strings.Repeat(" ", rest/2) + frame + strings.Repeat(" ", rest/2+rest%2) + io.WriteString(w, str) + case SpinnerOnRight: + io.WriteString(w, strings.Repeat(" ", rest)+frame) + } + s.count++ +} diff --git a/bar_option.go b/bar_option.go index 76f2050..e359c11 100644 --- a/bar_option.go +++ b/bar_option.go @@ -4,10 +4,11 @@ "bytes" "io" - "github.com/vbauerster/mpb/v5/decor" + "github.com/vbauerster/mpb/v6/decor" + "github.com/vbauerster/mpb/v6/internal" ) -// BarOption is a function option which changes the default behavior of a bar. +// BarOption is a func option to alter default behavior of a bar. type BarOption func(*bState) func (s *bState) addDecorators(dest *[]decor.Decorator, decorators ...decor.Decorator) { @@ -46,7 +47,7 @@ // BarWidth sets bar width independent of the container. func BarWidth(width int) BarOption { return func(s *bState) { - s.width = width + s.reqWidth = width } } @@ -77,19 +78,22 @@ // BarFillerOnComplete replaces bar's filler with message, on complete event. func BarFillerOnComplete(message string) BarOption { - return func(s *bState) { - s.filler = makeBarFillerOnComplete(s.baseF, message) - } + return BarFillerMiddleware(func(base BarFiller) BarFiller { + return BarFillerFunc(func(w io.Writer, reqWidth int, st decor.Statistics) { + if st.Completed { + io.WriteString(w, message) + } else { + base.Fill(w, reqWidth, st) + } + }) + }) } -func makeBarFillerOnComplete(filler BarFiller, message string) BarFiller { - return BarFillerFunc(func(w io.Writer, width int, st *decor.Statistics) { - if st.Completed { - io.WriteString(w, message) - } else { - filler.Fill(w, width, st) - } - }) +// BarFillerMiddleware provides a way to augment the underlying BarFiller. +func BarFillerMiddleware(middle func(BarFiller) BarFiller) BarOption { + return func(s *bState) { + s.middleware = middle + } } // BarPriority sets bar's priority. Zero is highest priority, i.e. bar @@ -101,47 +105,28 @@ } } -// BarExtender is an option to extend bar to the next new line, with -// arbitrary output. -func BarExtender(extender BarFiller) BarOption { - if extender == nil { +// BarExtender provides a way to extend bar to the next new line. +func BarExtender(filler BarFiller) BarOption { + if filler == nil { return nil } return func(s *bState) { - s.extender = makeExtFunc(extender) + s.extender = makeExtenderFunc(filler) } } -func makeExtFunc(extender BarFiller) extFunc { +func makeExtenderFunc(filler BarFiller) extenderFunc { buf := new(bytes.Buffer) - nl := []byte("\n") - return func(r io.Reader, tw int, st *decor.Statistics) (io.Reader, int) { - extender.Fill(buf, tw, st) - return io.MultiReader(r, buf), bytes.Count(buf.Bytes(), nl) + return func(r io.Reader, reqWidth int, st decor.Statistics) (io.Reader, int) { + filler.Fill(buf, reqWidth, st) + return io.MultiReader(r, buf), bytes.Count(buf.Bytes(), []byte("\n")) } } -// TrimSpace trims bar's edge spaces. -func TrimSpace() BarOption { +// BarFillerTrim removes leading and trailing space around the underlying BarFiller. +func BarFillerTrim() BarOption { return func(s *bState) { s.trimSpace = true - } -} - -// BarStyle overrides mpb.DefaultBarStyle which is "[=>-]<+". -// It's ok to pass string containing just 5 runes, for example "╢▌▌░╟", -// if you don't need to override '<' (reverse tip) and '+' (refill rune). -func BarStyle(style string) BarOption { - if style == "" { - return nil - } - type styleSetter interface { - SetStyle(string) - } - return func(s *bState) { - if t, ok := s.baseF.(styleSetter); ok { - t.SetStyle(style) - } } } @@ -153,51 +138,15 @@ } } -// BarReverse reverse mode, bar will progress from right to left. -func BarReverse() BarOption { - type revSetter interface { - SetReverse(bool) - } - return func(s *bState) { - if t, ok := s.baseF.(revSetter); ok { - t.SetReverse(true) - } - } +// BarOptional will invoke provided option only when pick is true. +func BarOptional(option BarOption, pick bool) BarOption { + return BarOptOn(option, internal.Predicate(pick)) } -// SpinnerStyle sets custom spinner style. -// Effective when Filler type is spinner. -func SpinnerStyle(frames []string) BarOption { - if len(frames) == 0 { - return nil - } - chk := func(filler BarFiller) (interface{}, bool) { - t, ok := filler.(*spinnerFiller) - return t, ok - } - cb := func(t interface{}) { - t.(*spinnerFiller).frames = frames - } - return MakeFillerTypeSpecificBarOption(chk, cb) -} - -// MakeFillerTypeSpecificBarOption makes BarOption specific to Filler's -// actual type. If you implement your own Filler, so most probably -// you'll need this. See BarStyle or SpinnerStyle for example. -func MakeFillerTypeSpecificBarOption( - typeChecker func(BarFiller) (interface{}, bool), - cb func(interface{}), -) BarOption { - return func(s *bState) { - if t, ok := typeChecker(s.baseF); ok { - cb(t) - } - } -} - -// BarOptOn returns option when condition evaluates to true. -func BarOptOn(option BarOption, condition func() bool) BarOption { - if condition() { +// BarOptOn will invoke provided option only when higher order predicate +// evaluates to true. +func BarOptOn(option BarOption, predicate func() bool) BarOption { + if predicate() { return option } return nil diff --git a/bar_test.go b/bar_test.go index b442195..1f0067c 100644 --- a/bar_test.go +++ b/bar_test.go @@ -10,12 +10,12 @@ "time" "unicode/utf8" - . "github.com/vbauerster/mpb/v5" - "github.com/vbauerster/mpb/v5/decor" + "github.com/vbauerster/mpb/v6" + "github.com/vbauerster/mpb/v6/decor" ) func TestBarCompleted(t *testing.T) { - p := New(WithOutput(ioutil.Discard)) + p := mpb.New(mpb.WithWidth(80), mpb.WithOutput(ioutil.Discard)) total := 80 bar := p.AddBar(int64(total)) @@ -33,17 +33,17 @@ } func TestBarID(t *testing.T) { - p := New(WithOutput(ioutil.Discard)) + p := mpb.New(mpb.WithWidth(80), mpb.WithOutput(ioutil.Discard)) total := 100 wantID := 11 - bar := p.AddBar(int64(total), BarID(wantID)) - - go func(total int) { + bar := p.AddBar(int64(total), mpb.BarID(wantID)) + + go func() { for i := 0; i < total; i++ { time.Sleep(50 * time.Millisecond) bar.Increment() } - }(total) + }() gotID := bar.ID() if gotID != wantID { @@ -57,14 +57,13 @@ func TestBarSetRefill(t *testing.T) { var buf bytes.Buffer - width := 100 - p := New(WithOutput(&buf), WithWidth(width)) + p := mpb.New(mpb.WithOutput(&buf), mpb.WithWidth(100)) total := 100 till := 30 - refillRune, _ := utf8.DecodeLastRuneInString(DefaultBarStyle) - - bar := p.AddBar(int64(total), TrimSpace()) + refillRune, _ := utf8.DecodeLastRuneInString(mpb.BarDefaultStyle) + + bar := p.AddBar(int64(total), mpb.BarFillerTrim()) bar.SetRefill(int64(till)) bar.IncrBy(till) @@ -91,12 +90,12 @@ func TestBarHas100PercentWithOnCompleteDecorator(t *testing.T) { var buf bytes.Buffer - p := New(WithOutput(&buf)) + p := mpb.New(mpb.WithWidth(80), mpb.WithOutput(&buf)) total := 50 bar := p.AddBar(int64(total), - AppendDecorators( + mpb.AppendDecorators( decor.OnComplete( decor.Percentage(), "done", ), @@ -119,13 +118,13 @@ func TestBarHas100PercentWithBarRemoveOnComplete(t *testing.T) { var buf bytes.Buffer - p := New(WithOutput(&buf)) + p := mpb.New(mpb.WithWidth(80), mpb.WithOutput(&buf)) total := 50 bar := p.AddBar(int64(total), - BarRemoveOnComplete(), - AppendDecorators(decor.Percentage()), + mpb.BarRemoveOnComplete(), + mpb.AppendDecorators(decor.Percentage()), ) for i := 0; i < total; i++ { @@ -144,9 +143,9 @@ func TestBarStyle(t *testing.T) { var buf bytes.Buffer customFormat := "╢▌▌░╟" - p := New(WithOutput(&buf)) total := 80 - bar := p.AddBar(int64(total), BarStyle(customFormat), TrimSpace()) + p := mpb.New(mpb.WithWidth(total), mpb.WithOutput(&buf)) + bar := p.Add(int64(total), mpb.NewBarFiller(customFormat), mpb.BarFillerTrim()) for i := 0; i < total; i++ { bar.Increment() @@ -170,14 +169,18 @@ func TestBarPanicBeforeComplete(t *testing.T) { var buf bytes.Buffer - p := New(WithDebugOutput(&buf), WithOutput(ioutil.Discard)) + p := mpb.New( + mpb.WithWidth(80), + mpb.WithDebugOutput(&buf), + mpb.WithOutput(ioutil.Discard), + ) total := 100 panicMsg := "Upps!!!" var pCount uint32 bar := p.AddBar(int64(total), - PrependDecorators(panicDecorator(panicMsg, - func(st *decor.Statistics) bool { + mpb.PrependDecorators(panicDecorator(panicMsg, + func(st decor.Statistics) bool { if st.Current >= 42 { atomic.AddUint32(&pCount, 1) return true @@ -206,14 +209,18 @@ func TestBarPanicAfterComplete(t *testing.T) { var buf bytes.Buffer - p := New(WithDebugOutput(&buf), WithOutput(ioutil.Discard)) + p := mpb.New( + mpb.WithWidth(80), + mpb.WithDebugOutput(&buf), + mpb.WithOutput(ioutil.Discard), + ) total := 100 panicMsg := "Upps!!!" var pCount uint32 bar := p.AddBar(int64(total), - PrependDecorators(panicDecorator(panicMsg, - func(st *decor.Statistics) bool { + mpb.PrependDecorators(panicDecorator(panicMsg, + func(st decor.Statistics) bool { if st.Completed { atomic.AddUint32(&pCount, 1) return true @@ -230,8 +237,8 @@ p.Wait() - if pCount != 1 { - t.Errorf("Decor called after panic %d times\n", pCount-1) + if pCount > 2 { + t.Error("Decor called after panic more than 2 times\n") } barStr := buf.String() @@ -240,24 +247,11 @@ } } -func panicDecorator(panicMsg string, cond func(*decor.Statistics) bool) decor.Decorator { - d := &decorator{ - panicMsg: panicMsg, - cond: cond, - } - d.Init() - return d -} - -type decorator struct { - decor.WC - panicMsg string - cond func(*decor.Statistics) bool -} - -func (d *decorator) Decor(st *decor.Statistics) string { - if d.cond(st) { - panic(d.panicMsg) - } - return d.FormatMsg("") -} +func panicDecorator(panicMsg string, cond func(decor.Statistics) bool) decor.Decorator { + return decor.Any(func(st decor.Statistics) string { + if cond(st) { + panic(panicMsg) + } + return "" + }) +} diff --git a/barbench_test.go b/barbench_test.go index 53a8141..76beece 100644 --- a/barbench_test.go +++ b/barbench_test.go @@ -4,11 +4,11 @@ "io/ioutil" "testing" - "github.com/vbauerster/mpb/v5/decor" + "github.com/vbauerster/mpb/v6/decor" ) func BenchmarkIncrSingleBar(b *testing.B) { - p := New(WithOutput(ioutil.Discard)) + p := New(WithOutput(ioutil.Discard), WithWidth(80)) bar := p.AddBar(int64(b.N)) for i := 0; i < b.N; i++ { bar.Increment() @@ -16,7 +16,7 @@ } func BenchmarkIncrSingleBarWhileIsNotCompleted(b *testing.B) { - p := New(WithOutput(ioutil.Discard)) + p := New(WithOutput(ioutil.Discard), WithWidth(80)) bar := p.AddBar(int64(b.N)) for !bar.Completed() { bar.Increment() @@ -24,7 +24,7 @@ } func BenchmarkIncrSingleBarWithNameDecorator(b *testing.B) { - p := New(WithOutput(ioutil.Discard)) + p := New(WithOutput(ioutil.Discard), WithWidth(80)) bar := p.AddBar(int64(b.N), PrependDecorators(decor.Name("test"))) for i := 0; i < b.N; i++ { bar.Increment() @@ -32,7 +32,7 @@ } func BenchmarkIncrSingleBarWithNameAndEwmaETADecorator(b *testing.B) { - p := New(WithOutput(ioutil.Discard)) + p := New(WithOutput(ioutil.Discard), WithWidth(80)) bar := p.AddBar(int64(b.N), PrependDecorators(decor.Name("test")), AppendDecorators(decor.EwmaETA(decor.ET_STYLE_GO, 60)), diff --git a/container_option.go b/container_option.go new file mode 100644 index 0000000..b92c757 --- /dev/null +++ b/container_option.go @@ -0,0 +1,112 @@ +package mpb + +import ( + "io" + "io/ioutil" + "sync" + "time" + + "github.com/vbauerster/mpb/v6/internal" +) + +// ContainerOption is a func option to alter default behavior of a bar +// container. Container term refers to a Progress struct which can +// hold one or more Bars. +type ContainerOption func(*pState) + +// WithWaitGroup provides means to have a single joint point. If +// *sync.WaitGroup is provided, you can safely call just p.Wait() +// without calling Wait() on provided *sync.WaitGroup. Makes sense +// when there are more than one bar to render. +func WithWaitGroup(wg *sync.WaitGroup) ContainerOption { + return func(s *pState) { + s.uwg = wg + } +} + +// WithWidth sets container width. If not set it defaults to terminal +// width. A bar added to the container will inherit its width, unless +// overridden by `func BarWidth(int) BarOption`. +func WithWidth(width int) ContainerOption { + return func(s *pState) { + s.reqWidth = width + } +} + +// WithRefreshRate overrides default 120ms refresh rate. +func WithRefreshRate(d time.Duration) ContainerOption { + return func(s *pState) { + s.rr = d + } +} + +// WithManualRefresh disables internal auto refresh time.Ticker. +// Refresh will occur upon receive value from provided ch. +func WithManualRefresh(ch <-chan interface{}) ContainerOption { + return func(s *pState) { + s.externalRefresh = ch + } +} + +// WithRenderDelay delays rendering. By default rendering starts as +// soon as bar is added, with this option it's possible to delay +// rendering process by keeping provided chan unclosed. In other words +// rendering will start as soon as provided chan is closed. +func WithRenderDelay(ch <-chan struct{}) ContainerOption { + return func(s *pState) { + s.renderDelay = ch + } +} + +// WithShutdownNotifier provided chanel will be closed, after all bars +// have been rendered. +func WithShutdownNotifier(ch chan struct{}) ContainerOption { + return func(s *pState) { + s.shutdownNotifier = ch + } +} + +// WithOutput overrides default os.Stdout output. Setting it to nil +// will effectively disable auto refresh rate and discard any output, +// useful if you want to disable progress bars with little overhead. +func WithOutput(w io.Writer) ContainerOption { + return func(s *pState) { + if w == nil { + s.output = ioutil.Discard + s.outputDiscarded = true + return + } + s.output = w + } +} + +// WithDebugOutput sets debug output. +func WithDebugOutput(w io.Writer) ContainerOption { + if w == nil { + return nil + } + return func(s *pState) { + s.debugOut = w + } +} + +// PopCompletedMode will pop and stop rendering completed bars. +func PopCompletedMode() ContainerOption { + return func(s *pState) { + s.popCompleted = true + } +} + +// ContainerOptional will invoke provided option only when pick is true. +func ContainerOptional(option ContainerOption, pick bool) ContainerOption { + return ContainerOptOn(option, internal.Predicate(pick)) +} + +// ContainerOptOn will invoke provided option only when higher order +// predicate evaluates to true. +func ContainerOptOn(option ContainerOption, predicate func() bool) ContainerOption { + if predicate() { + return option + } + return nil +} diff --git a/cwriter/cuuAndEd_construction_bench_test.go b/cwriter/cuuAndEd_construction_bench_test.go new file mode 100644 index 0000000..af80be9 --- /dev/null +++ b/cwriter/cuuAndEd_construction_bench_test.go @@ -0,0 +1,39 @@ +package cwriter + +import ( + "bytes" + "fmt" + "io/ioutil" + "strconv" + "testing" +) + +func BenchmarkWithFprintf(b *testing.B) { + cuuAndEd := "\x1b[%dA\x1b[J" + for i := 0; i < b.N; i++ { + fmt.Fprintf(ioutil.Discard, cuuAndEd, 4) + } +} + +func BenchmarkWithJoin(b *testing.B) { + bCuuAndEd := [][]byte{[]byte("\x1b["), []byte("A\x1b[J")} + for i := 0; i < b.N; i++ { + ioutil.Discard.Write(bytes.Join(bCuuAndEd, []byte(strconv.Itoa(4)))) + } +} + +func BenchmarkWithAppend(b *testing.B) { + escOpen := []byte("\x1b[") + cuuAndEd := []byte("A\x1b[J") + for i := 0; i < b.N; i++ { + ioutil.Discard.Write(append(strconv.AppendInt(escOpen, 4, 10), cuuAndEd...)) + } +} + +func BenchmarkWithCopy(b *testing.B) { + w := New(ioutil.Discard) + w.lineCount = 4 + for i := 0; i < b.N; i++ { + w.ansiCuuAndEd() + } +} diff --git a/cwriter/doc.go b/cwriter/doc.go new file mode 100644 index 0000000..93c8f82 --- /dev/null +++ b/cwriter/doc.go @@ -0,0 +1,2 @@ +// Package cwriter is a console writer abstraction for the underlying OS. +package cwriter diff --git a/cwriter/util_bsd.go b/cwriter/util_bsd.go new file mode 100644 index 0000000..4e3564e --- /dev/null +++ b/cwriter/util_bsd.go @@ -0,0 +1,7 @@ +// +build darwin dragonfly freebsd netbsd openbsd + +package cwriter + +import "golang.org/x/sys/unix" + +const ioctlReadTermios = unix.TIOCGETA diff --git a/cwriter/util_linux.go b/cwriter/util_linux.go new file mode 100644 index 0000000..253f12d --- /dev/null +++ b/cwriter/util_linux.go @@ -0,0 +1,7 @@ +// +build aix linux + +package cwriter + +import "golang.org/x/sys/unix" + +const ioctlReadTermios = unix.TCGETS diff --git a/cwriter/util_solaris.go b/cwriter/util_solaris.go new file mode 100644 index 0000000..4b29ff5 --- /dev/null +++ b/cwriter/util_solaris.go @@ -0,0 +1,7 @@ +// +build solaris + +package cwriter + +import "golang.org/x/sys/unix" + +const ioctlReadTermios = unix.TCGETA diff --git a/cwriter/writer.go b/cwriter/writer.go index 9ec1ec6..1ade547 100644 --- a/cwriter/writer.go +++ b/cwriter/writer.go @@ -3,17 +3,19 @@ import ( "bytes" "errors" - "fmt" "io" "os" - - "golang.org/x/crypto/ssh/terminal" + "strconv" ) -// NotATTY not a TeleTYpewriter error. -var NotATTY = errors.New("not a terminal") +// ErrNotTTY not a TeleTYpewriter error. +var ErrNotTTY = errors.New("not a terminal") -var cuuAndEd = fmt.Sprintf("%c[%%dA%[1]c[J", 27) +// http://ascii-table.com/ansi-escape-sequences.php +const ( + escOpen = "\x1b[" + cuuAndEd = "A\x1b[J" +) // Writer is a buffered the writer that updates the terminal. The // contents of writer will be flushed when Flush is called. @@ -21,7 +23,7 @@ out io.Writer buf bytes.Buffer lineCount int - fd uintptr + fd int isTerminal bool } @@ -29,16 +31,20 @@ func New(out io.Writer) *Writer { w := &Writer{out: out} if f, ok := out.(*os.File); ok { - w.fd = f.Fd() - w.isTerminal = terminal.IsTerminal(int(w.fd)) + w.fd = int(f.Fd()) + w.isTerminal = IsTerminal(w.fd) } return w } // Flush flushes the underlying buffer. func (w *Writer) Flush(lineCount int) (err error) { + // some terminals interpret 'cursor up 0' as 'cursor up 1' if w.lineCount > 0 { - w.clearLines() + err = w.clearLines() + if err != nil { + return + } } w.lineCount = lineCount _, err = w.buf.WriteTo(w.out) @@ -63,9 +69,16 @@ // GetWidth returns width of underlying terminal. func (w *Writer) GetWidth() (int, error) { - if w.isTerminal { - tw, _, err := terminal.GetSize(int(w.fd)) - return tw, err + if !w.isTerminal { + return -1, ErrNotTTY } - return -1, NotATTY + tw, _, err := GetSize(w.fd) + return tw, err } + +func (w *Writer) ansiCuuAndEd() (err error) { + buf := make([]byte, 8) + buf = strconv.AppendInt(buf[:copy(buf, escOpen)], int64(w.lineCount), 10) + _, err = w.out.Write(append(buf, cuuAndEd...)) + return +} diff --git a/cwriter/writer_posix.go b/cwriter/writer_posix.go index 3fb8b7d..f54a5d0 100644 --- a/cwriter/writer_posix.go +++ b/cwriter/writer_posix.go @@ -2,8 +2,25 @@ package cwriter -import "fmt" +import ( + "golang.org/x/sys/unix" +) -func (w *Writer) clearLines() { - fmt.Fprintf(w.out, cuuAndEd, w.lineCount) +func (w *Writer) clearLines() error { + return w.ansiCuuAndEd() } + +// GetSize returns the dimensions of the given terminal. +func GetSize(fd int) (width, height int, err error) { + ws, err := unix.IoctlGetWinsize(fd, unix.TIOCGWINSZ) + if err != nil { + return -1, -1, err + } + return int(ws.Col), int(ws.Row), nil +} + +// IsTerminal returns whether the given file descriptor is a terminal. +func IsTerminal(fd int) bool { + _, err := unix.IoctlGetTermios(fd, ioctlReadTermios) + return err == nil +} diff --git a/cwriter/writer_windows.go b/cwriter/writer_windows.go index 7125289..1a69c81 100644 --- a/cwriter/writer_windows.go +++ b/cwriter/writer_windows.go @@ -3,58 +3,71 @@ package cwriter import ( - "fmt" - "syscall" "unsafe" + + "golang.org/x/sys/windows" ) -var kernel32 = syscall.NewLazyDLL("kernel32.dll") +var kernel32 = windows.NewLazySystemDLL("kernel32.dll") var ( - procGetConsoleScreenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo") procSetConsoleCursorPosition = kernel32.NewProc("SetConsoleCursorPosition") procFillConsoleOutputCharacter = kernel32.NewProc("FillConsoleOutputCharacterW") - procFillConsoleOutputAttribute = kernel32.NewProc("FillConsoleOutputAttribute") ) -type coord struct { - x int16 - y int16 +func (w *Writer) clearLines() error { + if !w.isTerminal { + // hope it's cygwin or similar + return w.ansiCuuAndEd() + } + + var info windows.ConsoleScreenBufferInfo + if err := windows.GetConsoleScreenBufferInfo(windows.Handle(w.fd), &info); err != nil { + return err + } + + info.CursorPosition.Y -= int16(w.lineCount) + if info.CursorPosition.Y < 0 { + info.CursorPosition.Y = 0 + } + _, _, _ = procSetConsoleCursorPosition.Call( + uintptr(w.fd), + uintptr(uint32(uint16(info.CursorPosition.Y))<<16|uint32(uint16(info.CursorPosition.X))), + ) + + // clear the lines + cursor := &windows.Coord{ + X: info.Window.Left, + Y: info.CursorPosition.Y, + } + count := uint32(info.Size.X) * uint32(w.lineCount) + _, _, _ = procFillConsoleOutputCharacter.Call( + uintptr(w.fd), + uintptr(' '), + uintptr(count), + *(*uintptr)(unsafe.Pointer(cursor)), + uintptr(unsafe.Pointer(new(uint32))), + ) + return nil } -type smallRect struct { - left int16 - top int16 - right int16 - bottom int16 +// GetSize returns the visible dimensions of the given terminal. +// +// These dimensions don't include any scrollback buffer height. +func GetSize(fd int) (width, height int, err error) { + var info windows.ConsoleScreenBufferInfo + if err := windows.GetConsoleScreenBufferInfo(windows.Handle(fd), &info); err != nil { + return 0, 0, err + } + // terminal.GetSize from crypto/ssh adds "+ 1" to both width and height: + // https://go.googlesource.com/crypto/+/refs/heads/release-branch.go1.14/ssh/terminal/util_windows.go#75 + // but looks like this is a root cause of issue #66, so removing both "+ 1" have fixed it. + return int(info.Window.Right - info.Window.Left), int(info.Window.Bottom - info.Window.Top), nil } -type consoleScreenBufferInfo struct { - size coord - cursorPosition coord - attributes uint16 - window smallRect - maximumWindowSize coord +// IsTerminal returns whether the given file descriptor is a terminal. +func IsTerminal(fd int) bool { + var st uint32 + err := windows.GetConsoleMode(windows.Handle(fd), &st) + return err == nil } - -func (w *Writer) clearLines() { - if !w.isTerminal { - fmt.Fprintf(w.out, cuuAndEd, w.lineCount) - } - var info consoleScreenBufferInfo - procGetConsoleScreenBufferInfo.Call(w.fd, uintptr(unsafe.Pointer(&info))) - - info.cursorPosition.y -= int16(w.lineCount) - if info.cursorPosition.y < 0 { - info.cursorPosition.y = 0 - } - procSetConsoleCursorPosition.Call(w.fd, uintptr(uint32(uint16(info.cursorPosition.y))<<16|uint32(uint16(info.cursorPosition.x)))) - - // clear the lines - cursor := coord{ - x: info.window.left, - y: info.cursorPosition.y, - } - count := uint32(info.size.x) * uint32(w.lineCount) - procFillConsoleOutputCharacter.Call(w.fd, uintptr(' '), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(new(uint32)))) -} diff --git a/decor/any.go b/decor/any.go index bf9cf51..39518f5 100644 --- a/decor/any.go +++ b/decor/any.go @@ -1,21 +1,21 @@ package decor // Any decorator displays text, that can be changed during decorator's -// lifetime via provided func call back. +// lifetime via provided DecorFunc. // -// `f` call back which provides string to display +// `fn` DecorFunc callback // // `wcc` optional WC config // -func Any(f func(*Statistics) string, wcc ...WC) Decorator { - return &any{initWC(wcc...), f} +func Any(fn DecorFunc, wcc ...WC) Decorator { + return &any{initWC(wcc...), fn} } type any struct { WC - f func(*Statistics) string + fn DecorFunc } -func (d *any) Decor(s *Statistics) string { - return d.FormatMsg(d.f(s)) +func (d *any) Decor(s Statistics) string { + return d.FormatMsg(d.fn(s)) } diff --git a/decor/counters.go b/decor/counters.go index 297bf93..4a5343d 100644 --- a/decor/counters.go +++ b/decor/counters.go @@ -2,6 +2,7 @@ import ( "fmt" + "strings" ) const ( @@ -31,7 +32,7 @@ // // `unit` one of [0|UnitKiB|UnitKB] zero for no unit // -// `pairFmt` printf compatible verbs for current and total, like "%f" or "%d" +// `pairFmt` printf compatible verbs for current and total pair // // `wcc` optional WC config // @@ -43,25 +44,200 @@ // pairFmt="% d / % d" output: "1 MB / 12 MB" // func Counters(unit int, pairFmt string, wcc ...WC) Decorator { - return Any(chooseSizeProducer(unit, pairFmt), wcc...) -} - -func chooseSizeProducer(unit int, format string) func(*Statistics) string { - if format == "" { - format = "%d / %d" - } - switch unit { - case UnitKiB: - return func(s *Statistics) string { - return fmt.Sprintf(format, SizeB1024(s.Current), SizeB1024(s.Total)) - } - case UnitKB: - return func(s *Statistics) string { - return fmt.Sprintf(format, SizeB1000(s.Current), SizeB1000(s.Total)) - } - default: - return func(s *Statistics) string { - return fmt.Sprintf(format, s.Current, s.Total) - } - } -} + producer := func(unit int, pairFmt string) DecorFunc { + if pairFmt == "" { + pairFmt = "%d / %d" + } else if strings.Count(pairFmt, "%") != 2 { + panic("expected pairFmt with exactly 2 verbs") + } + switch unit { + case UnitKiB: + return func(s Statistics) string { + return fmt.Sprintf(pairFmt, SizeB1024(s.Current), SizeB1024(s.Total)) + } + case UnitKB: + return func(s Statistics) string { + return fmt.Sprintf(pairFmt, SizeB1000(s.Current), SizeB1000(s.Total)) + } + default: + return func(s Statistics) string { + return fmt.Sprintf(pairFmt, s.Current, s.Total) + } + } + } + return Any(producer(unit, pairFmt), wcc...) +} + +// TotalNoUnit is a wrapper around Total with no unit param. +func TotalNoUnit(format string, wcc ...WC) Decorator { + return Total(0, format, wcc...) +} + +// TotalKibiByte is a wrapper around Total with predefined unit +// UnitKiB (bytes/1024). +func TotalKibiByte(format string, wcc ...WC) Decorator { + return Total(UnitKiB, format, wcc...) +} + +// TotalKiloByte is a wrapper around Total with predefined unit +// UnitKB (bytes/1000). +func TotalKiloByte(format string, wcc ...WC) Decorator { + return Total(UnitKB, format, wcc...) +} + +// Total decorator with dynamic unit measure adjustment. +// +// `unit` one of [0|UnitKiB|UnitKB] zero for no unit +// +// `format` printf compatible verb for Total +// +// `wcc` optional WC config +// +// format example if unit=UnitKiB: +// +// format="%.1f" output: "12.0MiB" +// format="% .1f" output: "12.0 MiB" +// format="%d" output: "12MiB" +// format="% d" output: "12 MiB" +// +func Total(unit int, format string, wcc ...WC) Decorator { + producer := func(unit int, format string) DecorFunc { + if format == "" { + format = "%d" + } else if strings.Count(format, "%") != 1 { + panic("expected format with exactly 1 verb") + } + + switch unit { + case UnitKiB: + return func(s Statistics) string { + return fmt.Sprintf(format, SizeB1024(s.Total)) + } + case UnitKB: + return func(s Statistics) string { + return fmt.Sprintf(format, SizeB1000(s.Total)) + } + default: + return func(s Statistics) string { + return fmt.Sprintf(format, s.Total) + } + } + } + return Any(producer(unit, format), wcc...) +} + +// CurrentNoUnit is a wrapper around Current with no unit param. +func CurrentNoUnit(format string, wcc ...WC) Decorator { + return Current(0, format, wcc...) +} + +// CurrentKibiByte is a wrapper around Current with predefined unit +// UnitKiB (bytes/1024). +func CurrentKibiByte(format string, wcc ...WC) Decorator { + return Current(UnitKiB, format, wcc...) +} + +// CurrentKiloByte is a wrapper around Current with predefined unit +// UnitKB (bytes/1000). +func CurrentKiloByte(format string, wcc ...WC) Decorator { + return Current(UnitKB, format, wcc...) +} + +// Current decorator with dynamic unit measure adjustment. +// +// `unit` one of [0|UnitKiB|UnitKB] zero for no unit +// +// `format` printf compatible verb for Current +// +// `wcc` optional WC config +// +// format example if unit=UnitKiB: +// +// format="%.1f" output: "12.0MiB" +// format="% .1f" output: "12.0 MiB" +// format="%d" output: "12MiB" +// format="% d" output: "12 MiB" +// +func Current(unit int, format string, wcc ...WC) Decorator { + producer := func(unit int, format string) DecorFunc { + if format == "" { + format = "%d" + } else if strings.Count(format, "%") != 1 { + panic("expected format with exactly 1 verb") + } + + switch unit { + case UnitKiB: + return func(s Statistics) string { + return fmt.Sprintf(format, SizeB1024(s.Current)) + } + case UnitKB: + return func(s Statistics) string { + return fmt.Sprintf(format, SizeB1000(s.Current)) + } + default: + return func(s Statistics) string { + return fmt.Sprintf(format, s.Current) + } + } + } + return Any(producer(unit, format), wcc...) +} + +// InvertedCurrentNoUnit is a wrapper around InvertedCurrent with no unit param. +func InvertedCurrentNoUnit(format string, wcc ...WC) Decorator { + return InvertedCurrent(0, format, wcc...) +} + +// InvertedCurrentKibiByte is a wrapper around InvertedCurrent with predefined unit +// UnitKiB (bytes/1024). +func InvertedCurrentKibiByte(format string, wcc ...WC) Decorator { + return InvertedCurrent(UnitKiB, format, wcc...) +} + +// InvertedCurrentKiloByte is a wrapper around InvertedCurrent with predefined unit +// UnitKB (bytes/1000). +func InvertedCurrentKiloByte(format string, wcc ...WC) Decorator { + return InvertedCurrent(UnitKB, format, wcc...) +} + +// InvertedCurrent decorator with dynamic unit measure adjustment. +// +// `unit` one of [0|UnitKiB|UnitKB] zero for no unit +// +// `format` printf compatible verb for InvertedCurrent +// +// `wcc` optional WC config +// +// format example if unit=UnitKiB: +// +// format="%.1f" output: "12.0MiB" +// format="% .1f" output: "12.0 MiB" +// format="%d" output: "12MiB" +// format="% d" output: "12 MiB" +// +func InvertedCurrent(unit int, format string, wcc ...WC) Decorator { + producer := func(unit int, format string) DecorFunc { + if format == "" { + format = "%d" + } else if strings.Count(format, "%") != 1 { + panic("expected format with exactly 1 verb") + } + + switch unit { + case UnitKiB: + return func(s Statistics) string { + return fmt.Sprintf(format, SizeB1024(s.Total-s.Current)) + } + case UnitKB: + return func(s Statistics) string { + return fmt.Sprintf(format, SizeB1000(s.Total-s.Current)) + } + default: + return func(s Statistics) string { + return fmt.Sprintf(format, s.Total-s.Current) + } + } + } + return Any(producer(unit, format), wcc...) +} diff --git a/decor/decorator.go b/decor/decorator.go index 5bca63d..e81fae3 100644 --- a/decor/decorator.go +++ b/decor/decorator.go @@ -3,9 +3,9 @@ import ( "fmt" "time" - "unicode/utf8" "github.com/acarl005/stripansi" + "github.com/mattn/go-runewidth" ) const ( @@ -47,21 +47,31 @@ // Statistics consists of progress related statistics, that Decorator // may need. type Statistics struct { - ID int - Completed bool - Total int64 - Current int64 + ID int + AvailableWidth int + Total int64 + Current int64 + Refill int64 + Completed bool } // Decorator interface. -// Implementors should embed WC type, that way only single method -// Decor(*Statistics) needs to be implemented, the rest will be handled -// by WC type. +// Most of the time there is no need to implement this interface +// manually, as decor package already provides a wide range of decorators +// which implement this interface. If however built-in decorators don't +// meet your needs, you're free to implement your own one by implementing +// this particular interface. The easy way to go is to convert a +// `DecorFunc` into a `Decorator` interface by using provided +// `func Any(DecorFunc, ...WC) Decorator`. type Decorator interface { Configurator Synchronizer - Decor(*Statistics) string + Decor(Statistics) string } + +// DecorFunc func type. +// To be used with `func Any`(DecorFunc, ...WC) Decorator`. +type DecorFunc func(Statistics) string // Synchronizer interface. // All decorators implement this interface implicitly. Its Sync @@ -117,38 +127,35 @@ // W represents width and C represents bit set of width related config. // A decorator should embed WC, to enable width synchronization. type WC struct { - W int - C int - dynFormat string - wsync chan int + W int + C int + fill func(s string, w int) string + wsync chan int } // FormatMsg formats final message according to WC.W and WC.C. // Should be called by any Decorator implementation. func (wc *WC) FormatMsg(msg string) string { - var format string - runeCount := utf8.RuneCountInString(stripansi.Strip(msg)) - ansiCount := utf8.RuneCountInString(msg) - runeCount + pureWidth := runewidth.StringWidth(msg) + stripWidth := runewidth.StringWidth(stripansi.Strip(msg)) + maxCell := wc.W if (wc.C & DSyncWidth) != 0 { + cellCount := stripWidth if (wc.C & DextraSpace) != 0 { - runeCount++ + cellCount++ } - wc.wsync <- runeCount - max := <-wc.wsync - format = fmt.Sprintf(wc.dynFormat, ansiCount+max) - } else { - format = fmt.Sprintf(wc.dynFormat, ansiCount+wc.W) + wc.wsync <- cellCount + maxCell = <-wc.wsync } - return fmt.Sprintf(format, msg) + return wc.fill(msg, maxCell+(pureWidth-stripWidth)) } // Init initializes width related config. func (wc *WC) Init() WC { - wc.dynFormat = "%%" + wc.fill = runewidth.FillLeft if (wc.C & DidentRight) != 0 { - wc.dynFormat += "-" + wc.fill = runewidth.FillRight } - wc.dynFormat += "%ds" if (wc.C & DSyncWidth) != 0 { // it's deliberate choice to override wsync on each Init() call, // this way globals like WCSyncSpace can be reused diff --git a/decor/doc.go b/decor/doc.go index 6d26144..bfbb82e 100644 --- a/decor/doc.go +++ b/decor/doc.go @@ -1,6 +1,5 @@ +// Package decor provides common decorators for "github.com/vbauerster/mpb/v6" module. /* - Package decor provides common decorators for "github.com/vbauerster/mpb/v5" module. - Some decorators returned by this package might have a closure state. It is ok to use decorators concurrently, unless you share the same decorator among multiple *mpb.Bar instances. To avoid data races, create new decorator per *mpb.Bar instance. diff --git a/decor/elapsed.go b/decor/elapsed.go index c9999a3..e389f15 100644 --- a/decor/elapsed.go +++ b/decor/elapsed.go @@ -25,11 +25,11 @@ func NewElapsed(style TimeStyle, startTime time.Time, wcc ...WC) Decorator { var msg string producer := chooseTimeProducer(style) - f := func(s *Statistics) string { + fn := func(s Statistics) string { if !s.Completed { msg = producer(time.Since(startTime)) } return msg } - return Any(f, wcc...) + return Any(fn, wcc...) } diff --git a/decor/eta.go b/decor/eta.go index 6cb27a2..d03caa7 100644 --- a/decor/eta.go +++ b/decor/eta.go @@ -63,7 +63,7 @@ producer func(time.Duration) string } -func (d *movingAverageETA) Decor(s *Statistics) string { +func (d *movingAverageETA) Decor(s Statistics) string { v := math.Round(d.average.Value()) remaining := time.Duration((s.Total - s.Current) * int64(v)) if d.normalizer != nil { @@ -117,7 +117,7 @@ producer func(time.Duration) string } -func (d *averageETA) Decor(s *Statistics) string { +func (d *averageETA) Decor(s Statistics) string { var remaining time.Duration if s.Current != 0 { durPerItem := float64(time.Since(d.startTime)) / float64(s.Current) diff --git a/decor/merge.go b/decor/merge.go index 520f13a..e41406a 100644 --- a/decor/merge.go +++ b/decor/merge.go @@ -1,9 +1,10 @@ package decor import ( - "fmt" "strings" - "unicode/utf8" + + "github.com/acarl005/stripansi" + "github.com/mattn/go-runewidth" ) // Merge wraps its decorator argument with intention to sync width @@ -64,18 +65,18 @@ return d.Decorator } -func (d *mergeDecorator) Decor(s *Statistics) string { +func (d *mergeDecorator) Decor(s Statistics) string { msg := d.Decorator.Decor(s) - msgLen := utf8.RuneCountInString(msg) + pureWidth := runewidth.StringWidth(msg) + stripWidth := runewidth.StringWidth(stripansi.Strip(msg)) + cellCount := stripWidth if (d.wc.C & DextraSpace) != 0 { - msgLen++ + cellCount++ } - var total int - max := utf8.RuneCountInString(d.placeHolders[0].FormatMsg("")) - total += max - pw := (msgLen - max) / len(d.placeHolders) - rem := (msgLen - max) % len(d.placeHolders) + total := runewidth.StringWidth(d.placeHolders[0].FormatMsg("")) + pw := (cellCount - total) / len(d.placeHolders) + rem := (cellCount - total) % len(d.placeHolders) var diff int for i := 1; i < len(d.placeHolders); i++ { @@ -87,20 +88,20 @@ width = 0 } } - max = utf8.RuneCountInString(ph.FormatMsg(strings.Repeat(" ", width))) + max := runewidth.StringWidth(ph.FormatMsg(strings.Repeat(" ", width))) total += max diff = max - pw } d.wc.wsync <- pw + rem - max = <-d.wc.wsync - return fmt.Sprintf(fmt.Sprintf(d.wc.dynFormat, max+total), msg) + max := <-d.wc.wsync + return d.wc.fill(msg, max+total+(pureWidth-stripWidth)) } type placeHolderDecorator struct { WC } -func (d *placeHolderDecorator) Decor(*Statistics) string { +func (d *placeHolderDecorator) Decor(Statistics) string { return "" } diff --git a/decor/name.go b/decor/name.go index a7d477e..3af3112 100644 --- a/decor/name.go +++ b/decor/name.go @@ -8,5 +8,5 @@ // `wcc` optional WC config // func Name(str string, wcc ...WC) Decorator { - return Any(func(*Statistics) string { return str }, wcc...) + return Any(func(Statistics) string { return str }, wcc...) } diff --git a/decor/on_complete.go b/decor/on_complete.go index 0a1526b..f46b19a 100644 --- a/decor/on_complete.go +++ b/decor/on_complete.go @@ -24,7 +24,7 @@ msg string } -func (d *onCompleteWrapper) Decor(s *Statistics) string { +func (d *onCompleteWrapper) Decor(s Statistics) string { if s.Completed { wc := d.GetConf() return wc.FormatMsg(d.msg) diff --git a/decor/percentage.go b/decor/percentage.go index 65ca7d3..f4922bb 100644 --- a/decor/percentage.go +++ b/decor/percentage.go @@ -5,7 +5,7 @@ "io" "strconv" - "github.com/vbauerster/mpb/v5/internal" + "github.com/vbauerster/mpb/v6/internal" ) type percentageType float64 @@ -50,7 +50,7 @@ if format == "" { format = "% d" } - f := func(s *Statistics) string { + f := func(s Statistics) string { p := internal.Percentage(s.Total, s.Current, 100) return fmt.Sprintf(format, percentageType(p)) } diff --git a/decor/speed.go b/decor/speed.go index 8a48e3f..634edab 100644 --- a/decor/speed.go +++ b/decor/speed.go @@ -78,7 +78,7 @@ msg string } -func (d *movingAverageSpeed) Decor(s *Statistics) string { +func (d *movingAverageSpeed) Decor(s Statistics) string { if !s.Completed { var speed float64 if v := d.average.Value(); v > 0 { @@ -140,7 +140,7 @@ msg string } -func (d *averageSpeed) Decor(s *Statistics) string { +func (d *averageSpeed) Decor(s Statistics) string { if !s.Completed { speed := float64(s.Current) / float64(time.Since(d.startTime)) d.msg = d.producer(speed * 1e9) diff --git a/decor/speed_test.go b/decor/speed_test.go index 4f16aea..7f7d09d 100644 --- a/decor/speed_test.go +++ b/decor/speed_test.go @@ -122,7 +122,7 @@ for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { decor := NewAverageSpeed(tc.unit, tc.fmt, time.Now().Add(-tc.elapsed)) - stat := &Statistics{ + stat := Statistics{ Current: tc.current, } res := decor.Decor(stat) @@ -250,7 +250,7 @@ for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { decor := NewAverageSpeed(tc.unit, tc.fmt, time.Now().Add(-tc.elapsed)) - stat := &Statistics{ + stat := Statistics{ Current: tc.current, } res := decor.Decor(stat) diff --git a/decor/spinner.go b/decor/spinner.go index abfb2f7..6871639 100644 --- a/decor/spinner.go +++ b/decor/spinner.go @@ -12,7 +12,7 @@ frames = defaultSpinnerStyle } var count uint - f := func(s *Statistics) string { + f := func(s Statistics) string { frame := frames[count%uint(len(frames))] count++ return frame diff --git a/decorators_test.go b/decorators_test.go index 4bc475a..99c49ce 100644 --- a/decorators_test.go +++ b/decorators_test.go @@ -4,8 +4,8 @@ "sync" "testing" - . "github.com/vbauerster/mpb/v5" - "github.com/vbauerster/mpb/v5/decor" + "github.com/vbauerster/mpb/v6" + "github.com/vbauerster/mpb/v6/decor" ) func TestNameDecorator(t *testing.T) { @@ -32,7 +32,7 @@ } for _, test := range tests { - got := test.decorator.Decor(new(decor.Statistics)) + got := test.decorator.Decor(decor.Statistics{}) if got != test.want { t.Errorf("Want: %q, Got: %q\n", test.want, got) } @@ -40,7 +40,7 @@ } type step struct { - stat *decor.Statistics + stat decor.Statistics decorator decor.Decorator want string } @@ -50,36 +50,36 @@ testCases := [][]step{ { { - &decor.Statistics{Total: 100, Current: 8}, + decor.Statistics{Total: 100, Current: 8}, decor.Percentage(decor.WCSyncWidth), "8 %", }, { - &decor.Statistics{Total: 100, Current: 9}, + decor.Statistics{Total: 100, Current: 9}, decor.Percentage(decor.WCSyncWidth), "9 %", }, }, { { - &decor.Statistics{Total: 100, Current: 9}, + decor.Statistics{Total: 100, Current: 9}, decor.Percentage(decor.WCSyncWidth), " 9 %", }, { - &decor.Statistics{Total: 100, Current: 10}, + decor.Statistics{Total: 100, Current: 10}, decor.Percentage(decor.WCSyncWidth), "10 %", }, }, { { - &decor.Statistics{Total: 100, Current: 9}, + decor.Statistics{Total: 100, Current: 9}, decor.Percentage(decor.WCSyncWidth), " 9 %", }, { - &decor.Statistics{Total: 100, Current: 100}, + decor.Statistics{Total: 100, Current: 100}, decor.Percentage(decor.WCSyncWidth), "100 %", }, @@ -94,36 +94,36 @@ testCases := [][]step{ { { - &decor.Statistics{Total: 100, Current: 8}, + decor.Statistics{Total: 100, Current: 8}, decor.Percentage(decor.WCSyncWidthR), "8 %", }, { - &decor.Statistics{Total: 100, Current: 9}, + decor.Statistics{Total: 100, Current: 9}, decor.Percentage(decor.WCSyncWidthR), "9 %", }, }, { { - &decor.Statistics{Total: 100, Current: 9}, + decor.Statistics{Total: 100, Current: 9}, decor.Percentage(decor.WCSyncWidthR), "9 % ", }, { - &decor.Statistics{Total: 100, Current: 10}, + decor.Statistics{Total: 100, Current: 10}, decor.Percentage(decor.WCSyncWidthR), "10 %", }, }, { { - &decor.Statistics{Total: 100, Current: 9}, + decor.Statistics{Total: 100, Current: 9}, decor.Percentage(decor.WCSyncWidthR), "9 % ", }, { - &decor.Statistics{Total: 100, Current: 100}, + decor.Statistics{Total: 100, Current: 100}, decor.Percentage(decor.WCSyncWidthR), "100 %", }, @@ -138,36 +138,36 @@ testCases := [][]step{ { { - &decor.Statistics{Total: 100, Current: 8}, + decor.Statistics{Total: 100, Current: 8}, decor.Percentage(decor.WCSyncSpace), " 8 %", }, { - &decor.Statistics{Total: 100, Current: 9}, + decor.Statistics{Total: 100, Current: 9}, decor.Percentage(decor.WCSyncSpace), " 9 %", }, }, { { - &decor.Statistics{Total: 100, Current: 9}, + decor.Statistics{Total: 100, Current: 9}, decor.Percentage(decor.WCSyncSpace), " 9 %", }, { - &decor.Statistics{Total: 100, Current: 10}, + decor.Statistics{Total: 100, Current: 10}, decor.Percentage(decor.WCSyncSpace), " 10 %", }, }, { { - &decor.Statistics{Total: 100, Current: 9}, + decor.Statistics{Total: 100, Current: 9}, decor.Percentage(decor.WCSyncSpace), " 9 %", }, { - &decor.Statistics{Total: 100, Current: 100}, + decor.Statistics{Total: 100, Current: 100}, decor.Percentage(decor.WCSyncSpace), " 100 %", }, @@ -182,18 +182,20 @@ t.Fail() } - numBars := len(testCases[0]) - var wg sync.WaitGroup for _, columnCase := range testCases { + mpb.SyncWidth(toSyncMatrix(columnCase)) + numBars := len(columnCase) + gott := make([]chan string, numBars) + wg := new(sync.WaitGroup) wg.Add(numBars) - SyncWidth(toSyncMatrix(columnCase)) - gott := make([]chan string, numBars) - for i := 0; i < numBars; i++ { - gott[i] = make(chan string, 1) - go func(s step, ch chan string) { + for i, step := range columnCase { + step := step + ch := make(chan string, 1) + go func() { defer wg.Done() - ch <- s.decorator.Decor(s.stat) - }(columnCase[i], gott[i]) + ch <- step.decorator.Decor(step.stat) + }() + gott[i] = ch } wg.Wait() diff --git a/draw_test.go b/draw_test.go index cc59513..2553ca6 100644 --- a/draw_test.go +++ b/draw_test.go @@ -9,349 +9,367 @@ func TestDraw(t *testing.T) { // key is termWidth testSuite := map[int][]struct { - name string - total, current int64 - barWidth int - trimSpace bool - reverse bool - rup int64 - want string + name string + style string + total int64 + current int64 + refill int64 + barWidth int + trim bool + reverse bool + want string }{ 0: { { - name: "t,c,bw{60,20,80}", + name: "t,c{60,20}", + total: 60, + current: 20, + want: "… ", + }, + { + name: "t,c{60,20}trim", + total: 60, + current: 20, + trim: true, + want: "", + }, + }, + 1: { + { + name: "t,c{60,20}", + total: 60, + current: 20, + want: "… ", + }, + { + name: "t,c{60,20}trim", + total: 60, + current: 20, + trim: true, + want: "", + }, + }, + 2: { + { + name: "t,c{60,20}", + total: 60, + current: 20, + want: " ", + }, + { + name: "t,c{60,20}trim", + total: 60, + current: 20, + trim: true, + want: "[]", + }, + }, + 3: { + { + name: "t,c{60,20}", + total: 60, + current: 20, + want: " ", + }, + { + name: "t,c{60,20}trim", + total: 60, + current: 20, + trim: true, + want: "[-]", + }, + }, + 4: { + { + name: "t,c{60,20}", + total: 60, + current: 20, + want: " [] ", + }, + { + name: "t,c{60,20}trim", + total: 60, + current: 20, + trim: true, + want: "[>-]", + }, + }, + 5: { + { + name: "t,c{60,20}", + total: 60, + current: 20, + want: " [-] ", + }, + { + name: "t,c{60,20}trim", + total: 60, + current: 20, + trim: true, + want: "[>--]", + }, + }, + 6: { + { + name: "t,c{60,20}", + total: 60, + current: 20, + want: " [>-] ", + }, + { + name: "t,c{60,20}trim", + total: 60, + current: 20, + trim: true, + want: "[>---]", + }, + }, + 7: { + { + name: "t,c{60,20}", + total: 60, + current: 20, + want: " [>--] ", + }, + { + name: "t,c{60,20}trim", + total: 60, + current: 20, + trim: true, + want: "[=>---]", + }, + }, + 8: { + { + name: "t,c{60,20}", + total: 60, + current: 20, + want: " [>---] ", + }, + { + name: "t,c{60,20}trim", + total: 60, + current: 20, + trim: true, + want: "[=>----]", + }, + }, + 80: { + { + name: "t,c{60,20}", + total: 60, + current: 20, + want: " [========================>---------------------------------------------------] ", + }, + { + name: "t,c{60,20}trim", + total: 60, + current: 20, + trim: true, + want: "[=========================>----------------------------------------------------]", + }, + { + name: "t,c,bw{60,20,60}", total: 60, current: 20, - barWidth: 80, - want: "", - }, - { - name: "t,c,bw{60,20,80}", - total: 60, - current: 20, - barWidth: 80, - trimSpace: true, - want: "", - }, - }, - 1: { - { - name: "t,c,bw{60,20,80}", + barWidth: 60, + want: " [==================>---------------------------------------] ", + }, + { + name: "t,c,bw{60,20,60}trim", total: 60, current: 20, - barWidth: 80, - want: "", - }, - { - name: "t,c,bw{60,20,80}", - total: 60, - current: 20, - barWidth: 80, - trimSpace: true, - want: "", - }, - }, - 2: { - { - name: "t,c,bw{60,20,80}", - total: 60, - current: 20, - barWidth: 80, - want: " ", - }, - { - name: "t,c,bw,trim{60,20,80,true}", - total: 60, - current: 20, - barWidth: 80, - trimSpace: true, - want: "", - }, - }, - 3: { - { - name: "t,c,bw{60,20,80}", - total: 60, - current: 20, - barWidth: 80, - want: " ", - }, - { - name: "t,c,bw,trim{60,20,80,true}", - total: 60, - current: 20, - barWidth: 80, - trimSpace: true, - want: "", - }, - }, - 4: { - { - name: "t,c,bw{60,20,80}", - total: 60, - current: 20, - barWidth: 80, - want: " ", - }, - { - name: "t,c,bw,trim{60,20,80,true}", - total: 60, - current: 20, - barWidth: 80, - trimSpace: true, - want: "[>-]", - }, - }, - 5: { - { - name: "t,c,bw{60,20,80}", - total: 60, - current: 20, - barWidth: 80, - want: " ", - }, - { - name: "t,c,bw,trim{60,20,80,true}", - total: 60, - current: 20, - barWidth: 80, - trimSpace: true, - want: "[>--]", - }, - }, - 6: { - { - name: "t,c,bw{60,20,80}", - total: 60, - current: 20, - barWidth: 80, - want: " [>-] ", - }, - { - name: "t,c,bw,trim{60,20,80,true}", - total: 60, - current: 20, - barWidth: 80, - trimSpace: true, - want: "[>---]", - }, - }, - 7: { - { - name: "t,c,bw{60,20,80}", - total: 60, - current: 20, - barWidth: 80, - want: " [>--] ", - }, - { - name: "t,c,bw,trim{60,20,80,true}", - total: 60, - current: 20, - barWidth: 80, - trimSpace: true, - want: "[=>---]", - }, - }, - 8: { - { - name: "t,c,bw{60,20,80}", - total: 60, - current: 20, - barWidth: 80, - want: " [>---] ", - }, - { - name: "t,c,bw,trim{60,20,80,true}", - total: 60, - current: 20, - barWidth: 80, - trimSpace: true, - want: "[=>----]", - }, - }, - 80: { - { - name: "t,c,bw{60,20,80}", - total: 60, - current: 20, - barWidth: 80, - want: " [========================>---------------------------------------------------] ", - }, - { - name: "t,c,bw,trim{60,20,80,true}", - total: 60, - current: 20, - barWidth: 80, - trimSpace: true, - want: "[=========================>----------------------------------------------------]", + barWidth: 60, + trim: true, + want: "[==================>---------------------------------------]", }, }, 100: { { - name: "t,c,bw{100,100,0}", - total: 100, - current: 0, - barWidth: 100, - want: " [------------------------------------------------------------------------------------------------] ", - }, - { - name: "t,c,bw,trim{100,100,0,true}", - total: 100, - current: 0, - barWidth: 100, - trimSpace: true, - want: "[--------------------------------------------------------------------------------------------------]", - }, - { - name: "t,c,bw{100,1,100}", - total: 100, - current: 1, - barWidth: 100, - want: " [>-----------------------------------------------------------------------------------------------] ", - }, - { - name: "t,c,bw,trim{100,1,100,true}", - total: 100, - current: 1, - barWidth: 100, - trimSpace: true, - want: "[>-------------------------------------------------------------------------------------------------]", - }, - { - name: "t,c,bw{100,33,100}", - total: 100, - current: 33, - barWidth: 100, - want: " [===============================>----------------------------------------------------------------] ", - }, - { - name: "t,c,bw,trim{100,33,100,true}", - total: 100, - current: 33, - barWidth: 100, - trimSpace: true, - want: "[===============================>------------------------------------------------------------------]", - }, - { - name: "t,c,bw,trim,rev{100,33,100,true,true}", - total: 100, - current: 33, - barWidth: 100, - trimSpace: true, - reverse: true, - want: "[------------------------------------------------------------------<===============================]", - }, - { - name: "t,c,bw,rup{100,33,100,33}", - total: 100, - current: 33, - barWidth: 100, - rup: 33, - want: " [+++++++++++++++++++++++++++++++>----------------------------------------------------------------] ", - }, - { - name: "t,c,bw,rup,trim{100,33,100,33,true}", - total: 100, - current: 33, - barWidth: 100, - rup: 33, - trimSpace: true, - want: "[+++++++++++++++++++++++++++++++>------------------------------------------------------------------]", - }, - { - name: "t,c,bw,rup,trim,rev{100,33,100,33,true,true}", - total: 100, - current: 33, - barWidth: 100, - rup: 33, - trimSpace: true, - reverse: true, - want: "[------------------------------------------------------------------<+++++++++++++++++++++++++++++++]", - }, - { - name: "t,c,bw,rup{100,40,100,32}", - total: 100, - current: 40, - barWidth: 100, - rup: 33, - want: " [++++++++++++++++++++++++++++++++=====>----------------------------------------------------------] ", - }, - { - name: "t,c,bw,rup,trim{100,40,100,32,true}", - total: 100, - current: 40, - barWidth: 100, - rup: 33, - trimSpace: true, - want: "[++++++++++++++++++++++++++++++++======>-----------------------------------------------------------]", - }, - { - name: "t,c,bw{100,99,100}", - total: 100, - current: 99, - barWidth: 100, - want: " [==============================================================================================>-] ", - }, - { - name: "t,c,bw,trim{100,99,100,true}", - total: 100, - current: 99, - barWidth: 100, - trimSpace: true, - want: "[================================================================================================>-]", - }, - { - name: "t,c,bw{100,100,100}", - total: 100, - current: 100, - barWidth: 100, - want: " [================================================================================================] ", - }, - { - name: "t,c,bw,trim{100,100,100,true}", - total: 100, - current: 100, - barWidth: 100, - trimSpace: true, - want: "[==================================================================================================]", + name: "t,c{100,0}", + total: 100, + current: 0, + want: " [------------------------------------------------------------------------------------------------] ", + }, + { + name: "t,c{100,0}trim", + total: 100, + current: 0, + trim: true, + want: "[--------------------------------------------------------------------------------------------------]", + }, + { + name: "t,c{100,1}", + total: 100, + current: 1, + want: " [>-----------------------------------------------------------------------------------------------] ", + }, + { + name: "t,c{100,1}trim", + total: 100, + current: 1, + trim: true, + want: "[>-------------------------------------------------------------------------------------------------]", + }, + { + name: "t,c{100,99}", + total: 100, + current: 99, + want: " [==============================================================================================>-] ", + }, + { + name: "t,c{100,99}trim", + total: 100, + current: 99, + trim: true, + want: "[================================================================================================>-]", + }, + { + name: "t,c{100,100}", + total: 100, + current: 100, + want: " [================================================================================================] ", + }, + { + name: "t,c{100,100}trim", + total: 100, + current: 100, + trim: true, + want: "[==================================================================================================]", + }, + { + name: "t,c,r{100,100,100}trim", + total: 100, + current: 100, + refill: 100, + trim: true, + want: "[++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++]", + }, + { + name: "t,c{100,33}", + total: 100, + current: 33, + want: " [===============================>----------------------------------------------------------------] ", + }, + { + name: "t,c{100,33}trim", + total: 100, + current: 33, + trim: true, + want: "[===============================>------------------------------------------------------------------]", + }, + { + name: "t,c{100,33}trim,rev", + total: 100, + current: 33, + trim: true, + reverse: true, + want: "[------------------------------------------------------------------<===============================]", + }, + { + name: "t,c,r{100,33,33}", + total: 100, + current: 33, + refill: 33, + want: " [+++++++++++++++++++++++++++++++>----------------------------------------------------------------] ", + }, + { + name: "t,c,r{100,33,33}trim", + total: 100, + current: 33, + refill: 33, + trim: true, + want: "[+++++++++++++++++++++++++++++++>------------------------------------------------------------------]", + }, + { + name: "t,c,r{100,33,33}trim,rev", + total: 100, + current: 33, + refill: 33, + trim: true, + reverse: true, + want: "[------------------------------------------------------------------<+++++++++++++++++++++++++++++++]", + }, + { + name: "t,c,r{100,40,33}", + total: 100, + current: 40, + refill: 33, + want: " [++++++++++++++++++++++++++++++++=====>----------------------------------------------------------] ", + }, + { + name: "t,c,r{100,40,33}trim", + total: 100, + current: 40, + refill: 33, + trim: true, + want: "[++++++++++++++++++++++++++++++++======>-----------------------------------------------------------]", + }, + { + name: "t,c,r{100,40,33},rev", + total: 100, + current: 40, + refill: 33, + reverse: true, + want: " [----------------------------------------------------------<=====++++++++++++++++++++++++++++++++] ", + }, + { + name: "t,c,r{100,40,33}trim,rev", + total: 100, + current: 40, + refill: 33, + trim: true, + reverse: true, + want: "[-----------------------------------------------------------<======++++++++++++++++++++++++++++++++]", + }, + { + name: "[=の-] t,c{100,1}", + style: "[=の-]", + total: 100, + current: 1, + want: " [の---------------------------------------------------------------------------------------------…] ", + }, + }, + 197: { + { + name: "t,c,r{97486999,2805950,2805483}trim", + total: 97486999, + current: 2805950, + refill: 2805483, + barWidth: 60, + trim: true, + want: "[+>--------------------------------------------------------]", }, }, } var tmpBuf bytes.Buffer - for termWidth, cases := range testSuite { + for tw, cases := range testSuite { for _, tc := range cases { - s := newTestState(tc.reverse) - s.width = tc.barWidth + s := newTestState(tc.style, tc.reverse) + s.reqWidth = tc.barWidth s.total = tc.total s.current = tc.current - s.trimSpace = tc.trimSpace - if tc.rup > 0 { - if f, ok := s.filler.(interface{ SetRefill(int64) }); ok { - f.SetRefill(tc.rup) - } + s.trimSpace = tc.trim + s.refill = tc.refill + tmpBuf.Reset() + tmpBuf.ReadFrom(s.draw(newStatistics(tw, s))) + by := tmpBuf.Bytes() + + got := string(by[:len(by)-1]) + if !utf8.ValidString(got) { + t.Fail() } - tmpBuf.Reset() - tmpBuf.ReadFrom(s.draw(termWidth, newStatistics(s))) - by := tmpBuf.Bytes() - by = by[:len(by)-1] - - if utf8.RuneCount(by) > termWidth { - t.Errorf("termWidth:%d %q barWidth:%d overflow termWidth\n", termWidth, tc.name, utf8.RuneCount(by)) - } - - got := string(by) if got != tc.want { - t.Errorf("termWidth:%d %q want: %q %d, got: %q %d\n", termWidth, tc.name, tc.want, len(tc.want), got, len(got)) + t.Errorf("termWidth:%d %q want: %q %d, got: %q %d\n", tw, tc.name, tc.want, utf8.RuneCountInString(tc.want), got, utf8.RuneCountInString(got)) } } } } -func newTestState(reverse bool) *bState { +func newTestState(style string, rev bool) *bState { s := &bState{ - filler: NewBarFiller(DefaultBarStyle, reverse), + filler: NewBarFillerPick(style, rev), bufP: new(bytes.Buffer), bufB: new(bytes.Buffer), bufA: new(bytes.Buffer), diff --git a/example_test.go b/example_test.go index b25928d..6f25c1b 100644 --- a/example_test.go +++ b/example_test.go @@ -7,8 +7,8 @@ "math/rand" "time" - "github.com/vbauerster/mpb/v5" - "github.com/vbauerster/mpb/v5/decor" + "github.com/vbauerster/mpb/v6" + "github.com/vbauerster/mpb/v6/decor" ) func Example() { @@ -18,9 +18,9 @@ total := 100 name := "Single Bar:" // adding a single bar, which will inherit container's width - bar := p.AddBar(int64(total), - // override DefaultBarStyle, which is "[=>-]<+" - mpb.BarStyle("╢▌▌░╟"), + bar := p.Add(int64(total), + // progress bar filler with customized style + mpb.NewBarFiller("╢▌▌░╟"), mpb.PrependDecorators( // display our name with one space on the right decor.Name(name, decor.WC{W: len(name) + 1, C: decor.DidentRight}), diff --git a/export_test.go b/export_test.go index 7f5cb84..fba0eaf 100644 --- a/export_test.go +++ b/export_test.go @@ -2,3 +2,4 @@ // make syncWidth func public in test var SyncWidth = syncWidth +var MaxWidthDistributor = &maxWidthDistributor diff --git a/go.mod b/go.mod index 672191f..55d523e 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,11 @@ -module github.com/vbauerster/mpb/v5 +module github.com/vbauerster/mpb/v6 require ( github.com/VividCortex/ewma v1.1.1 github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d - golang.org/x/crypto v0.0.0-20200311171314-f7b00557c8c4 - golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 // indirect + github.com/mattn/go-runewidth v0.0.10 + github.com/rivo/uniseg v0.2.0 + golang.org/x/sys v0.0.0-20210324051608-47abb6519492 ) go 1.14 diff --git a/go.sum b/go.sum index 9a41197..4809b4a 100644 --- a/go.sum +++ b/go.sum @@ -2,12 +2,10 @@ github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA= github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8= github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20200311171314-f7b00557c8c4 h1:QmwruyY+bKbDDL0BaglrbZABEali68eoMFhTZpCjYVA= -golang.org/x/crypto v0.0.0-20200311171314-f7b00557c8c4/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 h1:uYVVQ9WP/Ds2ROhcaGPeIdVq0RIXVLwsHlnvJ+cT1So= -golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +github.com/mattn/go-runewidth v0.0.10 h1:CoZ3S2P7pvtP45xOtBw+/mDL2z0RKI576gSkzRRpdGg= +github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= +github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +golang.org/x/sys v0.0.0-20210324051608-47abb6519492 h1:Paq34FxTluEPvVyayQqMPgHm+vTOrIifmcYxFBx9TLg= +golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/internal/percentage.go b/internal/percentage.go index 7e261cb..a8ef8be 100644 --- a/internal/percentage.go +++ b/internal/percentage.go @@ -7,9 +7,13 @@ if total <= 0 { return 0 } + if current >= total { + return float64(width) + } return float64(int64(width)*current) / float64(total) } +// PercentageRound same as Percentage but with math.Round. func PercentageRound(total, current int64, width int) float64 { return math.Round(Percentage(total, current, width)) } diff --git a/internal/percentage_test.go b/internal/percentage_test.go index a2fa3f7..6d5410d 100644 --- a/internal/percentage_test.go +++ b/internal/percentage_test.go @@ -21,8 +21,7 @@ {"t,c,e{100,50,50}", 100, 50, 50}, {"t,c,e{100,99,99}", 100, 99, 99}, {"t,c,e{100,100,100}", 100, 100, 100}, - {"t,c,e{100,101,101}", 100, 101, 101}, - {"t,c,e{100,102,101}", 100, 102, 102}, + {"t,c,e{100,101,101}", 100, 101, 100}, {"t,c,e{120,0,0}", 120, 0, 0}, {"t,c,e{120,10,8}", 120, 10, 8}, {"t,c,e{120,15,13}", 120, 15, 13}, @@ -33,8 +32,7 @@ {"t,c,e{120,118,98}", 120, 118, 98}, {"t,c,e{120,119,99}", 120, 119, 99}, {"t,c,e{120,120,100}", 120, 120, 100}, - {"t,c,e{120,121,101}", 120, 121, 101}, - {"t,c,e{120,122,101}", 120, 122, 102}, + {"t,c,e{120,121,101}", 120, 121, 100}, }, 80: { {"t,c,e{-1,-1,0}", -1, -1, 0}, @@ -47,8 +45,7 @@ {"t,c,e{100,50,40}", 100, 50, 40}, {"t,c,e{100,99,79}", 100, 99, 79}, {"t,c,e{100,100,80}", 100, 100, 80}, - {"t,c,e{100,101,81}", 100, 101, 81}, - {"t,c,e{100,102,82}", 100, 102, 82}, + {"t,c,e{100,101,81}", 100, 101, 80}, {"t,c,e{120,0,0}", 120, 0, 0}, {"t,c,e{120,10,7}", 120, 10, 7}, {"t,c,e{120,15,10}", 120, 15, 10}, @@ -59,8 +56,7 @@ {"t,c,e{120,118,79}", 120, 118, 79}, {"t,c,e{120,119,79}", 120, 119, 79}, {"t,c,e{120,120,80}", 120, 120, 80}, - {"t,c,e{120,121,81}", 120, 121, 81}, - {"t,c,e{120,122,81}", 120, 122, 81}, + {"t,c,e{120,121,81}", 120, 121, 80}, }, } diff --git a/internal/predicate.go b/internal/predicate.go new file mode 100644 index 0000000..1e4dd24 --- /dev/null +++ b/internal/predicate.go @@ -0,0 +1,6 @@ +package internal + +// Predicate helper for internal use. +func Predicate(pick bool) func() bool { + return func() bool { return pick } +} diff --git a/internal/width.go b/internal/width.go new file mode 100644 index 0000000..216320f --- /dev/null +++ b/internal/width.go @@ -0,0 +1,10 @@ +package internal + +// CheckRequestedWidth checks that requested width doesn't overflow +// available width +func CheckRequestedWidth(requested, available int) int { + if requested <= 0 || requested >= available { + return available + } + return requested +} diff --git a/options.go b/options.go deleted file mode 100644 index 0488702..0000000 --- a/options.go +++ /dev/null @@ -1,105 +0,0 @@ -package mpb - -import ( - "io" - "io/ioutil" - "sync" - "time" -) - -// ContainerOption is a function option which changes the default -// behavior of progress container, if passed to mpb.New(...ContainerOption). -type ContainerOption func(*pState) - -// WithWaitGroup provides means to have a single joint point. If -// *sync.WaitGroup is provided, you can safely call just p.Wait() -// without calling Wait() on provided *sync.WaitGroup. Makes sense -// when there are more than one bar to render. -func WithWaitGroup(wg *sync.WaitGroup) ContainerOption { - return func(s *pState) { - s.uwg = wg - } -} - -// WithWidth sets container width. Default is 80. Bars inherit this -// width, as long as no BarWidth is applied. -func WithWidth(w int) ContainerOption { - return func(s *pState) { - if w < 0 { - return - } - s.width = w - } -} - -// WithRefreshRate overrides default 120ms refresh rate. -func WithRefreshRate(d time.Duration) ContainerOption { - return func(s *pState) { - s.rr = d - } -} - -// WithManualRefresh disables internal auto refresh time.Ticker. -// Refresh will occur upon receive value from provided ch. -func WithManualRefresh(ch <-chan time.Time) ContainerOption { - return func(s *pState) { - s.refreshSrc = ch - } -} - -// WithRenderDelay delays rendering. By default rendering starts as -// soon as bar is added, with this option it's possible to delay -// rendering process by keeping provided chan unclosed. In other words -// rendering will start as soon as provided chan is closed. -func WithRenderDelay(ch <-chan struct{}) ContainerOption { - return func(s *pState) { - s.renderDelay = ch - } -} - -// WithShutdownNotifier provided chanel will be closed, after all bars -// have been rendered. -func WithShutdownNotifier(ch chan struct{}) ContainerOption { - return func(s *pState) { - s.shutdownNotifier = ch - } -} - -// WithOutput overrides default os.Stdout output. Setting it to nil -// will effectively disable auto refresh rate and discard any output, -// useful if you want to disable progress bars with little overhead. -func WithOutput(w io.Writer) ContainerOption { - return func(s *pState) { - if w == nil { - s.refreshSrc = make(chan time.Time) - s.output = ioutil.Discard - return - } - s.output = w - } -} - -// WithDebugOutput sets debug output. -func WithDebugOutput(w io.Writer) ContainerOption { - if w == nil { - return nil - } - return func(s *pState) { - s.debugOut = w - } -} - -// PopCompletedMode will pop and stop rendering completed bars. -func PopCompletedMode() ContainerOption { - return func(s *pState) { - s.popCompleted = true - } -} - -// ContainerOptOn returns option when condition evaluates to true. -func ContainerOptOn(option ContainerOption, condition func() bool) ContainerOption { - if condition() { - return option - } - return nil -} diff --git a/progress.go b/progress.go index a366b92..5a3f962 100644 --- a/progress.go +++ b/progress.go @@ -8,22 +8,22 @@ "io" "io/ioutil" "log" + "math" "os" "sync" "time" - "github.com/vbauerster/mpb/v5/cwriter" - "github.com/vbauerster/mpb/v5/decor" + "github.com/vbauerster/mpb/v6/cwriter" + "github.com/vbauerster/mpb/v6/decor" ) const ( // default RefreshRate prr = 120 * time.Millisecond - // default width - pwidth = 80 ) -// Progress represents the container that renders Progress bars +// Progress represents a container that renders one or more progress +// bars. type Progress struct { ctx context.Context uwg *sync.WaitGroup @@ -36,21 +36,23 @@ dlogger *log.Logger } +// pState holds bars in its priorityQueue. It gets passed to +// *Progress.serve(...) monitor goroutine. type pState struct { bHeap priorityQueue heapUpdated bool pMatrix map[int][]chan int aMatrix map[int][]chan int barShutdownQueue []*Bar - barPopQueue []*Bar // following are provided/overrided by user idCount int - width int + reqWidth int popCompleted bool + outputDiscarded bool rr time.Duration uwg *sync.WaitGroup - refreshSrc <-chan time.Time + externalRefresh <-chan interface{} renderDelay <-chan struct{} shutdownNotifier chan struct{} parkedBars map[*Bar]*Bar @@ -70,7 +72,6 @@ func NewWithContext(ctx context.Context, options ...ContainerOption) *Progress { s := &pState{ bHeap: priorityQueue{}, - width: pwidth, rr: prr, parkedBars: make(map[*Bar]*Bar), output: os.Stdout, @@ -98,22 +99,25 @@ return p } -// AddBar creates a new progress bar and adds it to the rendering queue. +// AddBar creates a bar with default bar filler. Different filler can +// be choosen and applied via `*Progress.Add(...) *Bar` method. func (p *Progress) AddBar(total int64, options ...BarOption) *Bar { - return p.Add(total, NewBarFiller(DefaultBarStyle, false), options...) -} - -// AddSpinner creates a new spinner bar and adds it to the rendering queue. + return p.Add(total, NewBarFiller(BarDefaultStyle), options...) +} + +// AddSpinner creates a bar with default spinner filler. Different +// filler can be choosen and applied via `*Progress.Add(...) *Bar` +// method. func (p *Progress) AddSpinner(total int64, alignment SpinnerAlignment, options ...BarOption) *Bar { - return p.Add(total, NewSpinnerFiller(DefaultSpinnerStyle, alignment), options...) + return p.Add(total, NewSpinnerFiller(SpinnerDefaultStyle, alignment), options...) } // Add creates a bar which renders itself by provided filler. -// Set total to 0, if you plan to update it later. +// If `total <= 0` trigger complete event is disabled until reset with *bar.SetTotal(int64, bool). // Panics if *Progress instance is done, i.e. called after *Progress.Wait(). func (p *Progress) Add(total int64, filler BarFiller, options ...BarOption) *Bar { if filler == nil { - filler = NewBarFiller(DefaultBarStyle, false) + filler = BarFillerFunc(func(io.Writer, int, decor.Statistics) {}) } p.bwg.Add(1) result := make(chan *Bar) @@ -171,7 +175,7 @@ p.setBarPriority(b, priority) } -// BarCount returns bars count +// BarCount returns bars count. func (p *Progress) BarCount() int { result := make(chan int, 1) select { @@ -182,7 +186,7 @@ } } -// Wait waits far all bars to complete and finally shutdowns container. +// Wait waits for all bars to complete and finally shutdowns container. // After this method has been called, there is no way to reuse *Progress // instance. func (p *Progress) Wait() { @@ -215,12 +219,55 @@ op(s) case <-p.refreshCh: if err := s.render(cw); err != nil { - go p.dlogger.Println(err) + p.dlogger.Println(err) } case <-s.shutdownNotifier: + if s.heapUpdated { + if err := s.render(cw); err != nil { + p.dlogger.Println(err) + } + } return } } +} + +func (s *pState) newTicker(done <-chan struct{}) chan time.Time { + ch := make(chan time.Time) + if s.shutdownNotifier == nil { + s.shutdownNotifier = make(chan struct{}) + } + go func() { + if s.renderDelay != nil { + <-s.renderDelay + } + var internalRefresh <-chan time.Time + if !s.outputDiscarded { + if s.externalRefresh == nil { + ticker := time.NewTicker(s.rr) + defer ticker.Stop() + internalRefresh = ticker.C + } + } else { + s.externalRefresh = nil + } + for { + select { + case t := <-internalRefresh: + ch <- t + case x := <-s.externalRefresh: + if t, ok := x.(time.Time); ok { + ch <- t + } else { + ch <- time.Now() + } + case <-done: + close(s.shutdownNotifier) + return + } + } + }() + return ch } func (s *pState) render(cw *cwriter.Writer) error { @@ -233,7 +280,7 @@ tw, err := cw.GetWidth() if err != nil { - tw = s.width + tw = s.reqWidth } for i := 0; i < s.bHeap.Len(); i++ { bar := s.bHeap[i] @@ -250,11 +297,16 @@ b := heap.Pop(&s.bHeap).(*Bar) cw.ReadFrom(<-b.frameCh) if b.toShutdown { - // shutdown at next flush - // this ensures no bar ends up with less than 100% rendered - defer func() { + if b.recoveredPanic != nil { s.barShutdownQueue = append(s.barShutdownQueue, b) - }() + b.toShutdown = false + } else { + // shutdown at next flush + // this ensures no bar ends up with less than 100% rendered + defer func() { + s.barShutdownQueue = append(s.barShutdownQueue, b) + }() + } } lineCount += b.extendedLines + 1 bm[b] = struct{}{} @@ -267,59 +319,23 @@ delete(s.parkedBars, b) b.toDrop = true } + if s.popCompleted && !b.noPop { + lineCount -= b.extendedLines + 1 + b.toDrop = true + } if b.toDrop { delete(bm, b) s.heapUpdated = true - } else if s.popCompleted { - if b := b; !b.noPop { - defer func() { - s.barPopQueue = append(s.barPopQueue, b) - }() - } } b.cancel() } s.barShutdownQueue = s.barShutdownQueue[0:0] - - for _, b := range s.barPopQueue { - delete(bm, b) - s.heapUpdated = true - lineCount -= b.extendedLines + 1 - } - s.barPopQueue = s.barPopQueue[0:0] for b := range bm { heap.Push(&s.bHeap, b) } return cw.Flush(lineCount) -} - -func (s *pState) newTicker(done <-chan struct{}) chan time.Time { - ch := make(chan time.Time) - if s.shutdownNotifier == nil { - s.shutdownNotifier = make(chan struct{}) - } - go func() { - if s.renderDelay != nil { - <-s.renderDelay - } - if s.refreshSrc == nil { - ticker := time.NewTicker(s.rr) - defer ticker.Stop() - s.refreshSrc = ticker.C - } - for { - select { - case tick := <-s.refreshSrc: - ch <- tick - case <-done: - close(s.shutdownNotifier) - return - } - } - }() - return ch } func (s *pState) updateSyncMatrix() { @@ -342,16 +358,17 @@ func (s *pState) makeBarState(total int64, filler BarFiller, options ...BarOption) *bState { bs := &bState{ + id: s.idCount, + priority: s.idCount, + reqWidth: s.reqWidth, total: total, - baseF: extractBaseFiller(filler), filler: filler, - priority: s.idCount, - id: s.idCount, - width: s.width, + extender: func(r io.Reader, _ int, _ decor.Statistics) (io.Reader, int) { return r, 0 }, debugOut: s.debugOut, - extender: func(r io.Reader, _ int, _ *decor.Statistics) (io.Reader, int) { - return r, 0 - }, + } + + if total > 0 { + bs.triggerComplete = true } for _, opt := range options { @@ -360,40 +377,36 @@ } } + if bs.middleware != nil { + bs.filler = bs.middleware(filler) + bs.middleware = nil + } + if s.popCompleted && !bs.noPop { - bs.priority = -1 - } - - bs.bufP = bytes.NewBuffer(make([]byte, 0, bs.width)) - bs.bufB = bytes.NewBuffer(make([]byte, 0, bs.width)) - bs.bufA = bytes.NewBuffer(make([]byte, 0, bs.width)) + bs.priority = -(math.MaxInt32 - s.idCount) + } + + bs.bufP = bytes.NewBuffer(make([]byte, 0, 128)) + bs.bufB = bytes.NewBuffer(make([]byte, 0, 256)) + bs.bufA = bytes.NewBuffer(make([]byte, 0, 128)) return bs } func syncWidth(matrix map[int][]chan int) { for _, column := range matrix { - column := column - go func() { - var maxWidth int - for _, ch := range column { - if w := <-ch; w > maxWidth { - maxWidth = w - } - } - for _, ch := range column { - ch <- maxWidth - } - }() - } -} - -func extractBaseFiller(f BarFiller) BarFiller { - type wrapper interface { - Base() BarFiller - } - if f, ok := f.(wrapper); ok { - return extractBaseFiller(f.Base()) - } - return f -} + go maxWidthDistributor(column) + } +} + +var maxWidthDistributor = func(column []chan int) { + var maxWidth int + for _, ch := range column { + if w := <-ch; w > maxWidth { + maxWidth = w + } + } + for _, ch := range column { + ch <- maxWidth + } +} diff --git a/progress_test.go b/progress_test.go index 3390d2c..aa7a27d 100644 --- a/progress_test.go +++ b/progress_test.go @@ -9,7 +9,8 @@ "testing" "time" - "github.com/vbauerster/mpb/v5" + "github.com/vbauerster/mpb/v6" + "github.com/vbauerster/mpb/v6/decor" ) func init() { @@ -23,12 +24,13 @@ wg.Add(1) b := p.AddBar(100) go func() { + rng := rand.New(rand.NewSource(time.Now().UnixNano())) for i := 0; i < 100; i++ { if i == 33 { wg.Done() } b.Increment() - time.Sleep(randomDuration(100 * time.Millisecond)) + time.Sleep((time.Duration(rng.Intn(10)+1) * (10 * time.Millisecond)) / 2) } }() @@ -50,7 +52,7 @@ bars := make([]*mpb.Bar, 3) for i := 0; i < 3; i++ { b := p.AddBar(100) - bars[i] = b + rng := rand.New(rand.NewSource(time.Now().UnixNano())) go func(n int) { for i := 0; !b.Completed(); i++ { if n == 0 && i >= 33 { @@ -58,9 +60,10 @@ wg.Done() } b.Increment() - time.Sleep(randomDuration(100 * time.Millisecond)) + time.Sleep((time.Duration(rng.Intn(10)+1) * (10 * time.Millisecond)) / 2) } }(i) + bars[i] = b } wg.Wait() @@ -107,6 +110,71 @@ } } +// MaxWidthDistributor shouldn't stuck in the middle while removing or aborting a bar +func TestMaxWidthDistributor(t *testing.T) { + + makeWrapper := func(f func([]chan int), start, end chan struct{}) func([]chan int) { + return func(column []chan int) { + start <- struct{}{} + f(column) + <-end + } + } + + ready := make(chan struct{}) + start := make(chan struct{}) + end := make(chan struct{}) + *mpb.MaxWidthDistributor = makeWrapper(*mpb.MaxWidthDistributor, start, end) + + total := 80 + numBars := 3 + p := mpb.New(mpb.WithOutput(ioutil.Discard)) + for i := 0; i < numBars; i++ { + bar := p.AddBar(int64(total), + mpb.BarOptional(mpb.BarRemoveOnComplete(), i == 0), + mpb.PrependDecorators( + decor.EwmaETA(decor.ET_STYLE_GO, 60, decor.WCSyncSpace), + ), + ) + go func() { + <-ready + rng := rand.New(rand.NewSource(time.Now().UnixNano())) + for i := 0; i < total; i++ { + start := time.Now() + if bar.ID() >= numBars-1 && i >= 42 { + bar.Abort(true) + } + time.Sleep((time.Duration(rng.Intn(10)+1) * (10 * time.Millisecond)) / 2) + bar.Increment() + bar.DecoratorEwmaUpdate(time.Since(start)) + } + }() + } + + go func() { + <-ready + p.Wait() + close(start) + }() + + res := t.Run("maxWidthDistributor", func(t *testing.T) { + close(ready) + for v := range start { + timer := time.NewTimer(100 * time.Millisecond) + select { + case end <- v: + timer.Stop() + case <-timer.C: + t.FailNow() + } + } + }) + + if !res { + t.Error("maxWidthDistributor stuck in the middle") + } +} + func getLastLine(bb []byte) []byte { split := bytes.Split(bb, []byte("\n")) return split[len(split)-2] diff --git a/proxyreader_test.go b/proxyreader_test.go index 14bbca8..71e036b 100644 --- a/proxyreader_test.go +++ b/proxyreader_test.go @@ -7,7 +7,7 @@ "strings" "testing" - "github.com/vbauerster/mpb/v5" + "github.com/vbauerster/mpb/v6" ) const content = `Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do @@ -33,7 +33,7 @@ tReader := &testReader{strings.NewReader(content), false} - bar := p.AddBar(int64(len(content)), mpb.TrimSpace()) + bar := p.AddBar(int64(len(content)), mpb.BarFillerTrim()) var buf bytes.Buffer _, err := io.Copy(&buf, bar.ProxyReader(tReader)) @@ -70,7 +70,7 @@ wt := reader.(io.WriterTo) tReader := &testWriterTo{reader, wt, false} - bar := p.AddBar(int64(len(content)), mpb.TrimSpace()) + bar := p.AddBar(int64(len(content)), mpb.BarFillerTrim()) var buf bytes.Buffer _, err := io.Copy(&buf, bar.ProxyReader(tReader)) diff --git a/spinner_filler.go b/spinner_filler.go deleted file mode 100644 index 517725f..0000000 --- a/spinner_filler.go +++ /dev/null @@ -1,61 +0,0 @@ -package mpb - -import ( - "io" - "strings" - "unicode/utf8" - - "github.com/vbauerster/mpb/v5/decor" -) - -// SpinnerAlignment enum. -type SpinnerAlignment int - -// SpinnerAlignment kinds. -const ( - SpinnerOnLeft SpinnerAlignment = iota - SpinnerOnMiddle - SpinnerOnRight -) - -// DefaultSpinnerStyle is a slice of strings, which makes a spinner. -var DefaultSpinnerStyle = []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"} - -type spinnerFiller struct { - frames []string - count uint - alignment SpinnerAlignment -} - -// NewSpinnerFiller constucts mpb.BarFiller, to be used with *Progress.Add(...) *Bar method. -func NewSpinnerFiller(style []string, alignment SpinnerAlignment) BarFiller { - if len(style) == 0 { - style = DefaultSpinnerStyle - } - filler := &spinnerFiller{ - frames: style, - alignment: alignment, - } - return filler -} - -func (s *spinnerFiller) Fill(w io.Writer, width int, stat *decor.Statistics) { - - frame := s.frames[s.count%uint(len(s.frames))] - frameWidth := utf8.RuneCountInString(frame) - - if width < frameWidth { - return - } - - switch rest := width - frameWidth; s.alignment { - case SpinnerOnLeft: - io.WriteString(w, frame+strings.Repeat(" ", rest)) - case SpinnerOnMiddle: - str := strings.Repeat(" ", rest/2) + frame + strings.Repeat(" ", rest/2+rest%2) - io.WriteString(w, str) - case SpinnerOnRight: - io.WriteString(w, strings.Repeat(" ", rest)+frame) - } - s.count++ -}