diff --git a/example/cancel/main.go b/example/cancel/main.go index fdecde9..fb8da71 100644 --- a/example/cancel/main.go +++ b/example/cancel/main.go @@ -19,7 +19,7 @@ var wg sync.WaitGroup ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second) defer cancel() - p := mpb.New(ctx).SetWidth(64) + p := mpb.New().SetWidth(64).WithContext(ctx) name1 := "Bar#1:" bar1 := p.AddBar(50). diff --git a/example/io/multiple/main.go b/example/io/multiple/main.go index 4ca6386..0c1b178 100644 --- a/example/io/multiple/main.go +++ b/example/io/multiple/main.go @@ -19,7 +19,7 @@ url2 := "https://homebrew.bintray.com/bottles/libtiff-4.0.7.sierra.bottle.tar.gz" var wg sync.WaitGroup - p := mpb.New(nil).SetWidth(60) + p := mpb.New().SetWidth(60) for i, url := range [...]string{url1, url2} { wg.Add(1) diff --git a/example/io/single/main.go b/example/io/single/main.go index 617bb9a..b36cd76 100644 --- a/example/io/single/main.go +++ b/example/io/single/main.go @@ -35,7 +35,7 @@ } defer dest.Close() - p := mpb.New(nil).SetWidth(64) + p := mpb.New().SetWidth(64) bar := p.AddBar(size). PrependCounters("%3s / %3s", mpb.UnitBytes, 18, 0). diff --git a/example/prependETA/main.go b/example/prependETA/main.go index 6acc66d..6a615b0 100644 --- a/example/prependETA/main.go +++ b/example/prependETA/main.go @@ -16,8 +16,7 @@ func main() { var wg sync.WaitGroup - p := mpb.New(nil).SetWidth(64) - // p := mpb.New(nil).RefreshRate(80 * time.Millisecond).SetWidth(64) + p := mpb.New().SetWidth(64) name1 := "Bar#1:" bar1 := p.AddBar(50). diff --git a/example/prependElapsed/main.go b/example/prependElapsed/main.go index b5194c7..e39e387 100644 --- a/example/prependElapsed/main.go +++ b/example/prependElapsed/main.go @@ -16,8 +16,7 @@ func main() { var wg sync.WaitGroup - p := mpb.New(nil).SetWidth(64) - // p := mpb.New(nil).RefreshRate(80 * time.Millisecond).SetWidth(64) + p := mpb.New().SetWidth(64) name1 := "Bar#1:" bar1 := p.AddBar(50). diff --git a/example/remove/main.go b/example/remove/main.go index 4e64c7c..9d7ff0f 100644 --- a/example/remove/main.go +++ b/example/remove/main.go @@ -16,8 +16,7 @@ func main() { var wg sync.WaitGroup - p := mpb.New(nil).SetWidth(64) - // p := mpb.New(nil).RefreshRate(100 * time.Millisecond).SetWidth(64) + p := mpb.New().SetWidth(64) name1 := "Bar#1:" bar1 := p.AddBar(50). diff --git a/example/simple/main.go b/example/simple/main.go index 4a8b5ad..4bdfda7 100644 --- a/example/simple/main.go +++ b/example/simple/main.go @@ -11,7 +11,7 @@ func main() { var wg sync.WaitGroup - p := mpb.New(nil) + p := mpb.New() wg.Add(3) // add wg delta for i := 0; i < 3; i++ { name := fmt.Sprintf("Bar#%d:", i) @@ -31,6 +31,4 @@ } wg.Wait() // Wait for goroutines to finish p.Stop() // Stop mpb's rendering goroutine - // p.AddBar(1) // panic: you cannot reuse p, create new one! - fmt.Println("finish") } diff --git a/example/singleBar/main.go b/example/singleBar/main.go index ae77c67..f053cae 100644 --- a/example/singleBar/main.go +++ b/example/singleBar/main.go @@ -9,10 +9,8 @@ func main() { // Star mpb's rendering goroutine. - // If you don't plan to cancel, feed with nil - // otherwise provide context.Context, see cancel example - p := mpb.New(nil) - // Set custom width for every bar, which mpb will contain + p := mpb.New() + // Set custom width for every bar, which mpb will render // The default one in 70 p.SetWidth(80) // Set custom format for every bar, the default one is "[=>-]" diff --git a/example/sort/main.go b/example/sort/main.go index 33e1cee..f3b5386 100644 --- a/example/sort/main.go +++ b/example/sort/main.go @@ -44,7 +44,7 @@ func main() { var wg sync.WaitGroup - p := mpb.New(nil).SetWidth(60).BeforeRenderFunc(sortByProgressFunc()) + p := mpb.New().SetWidth(60).BeforeRenderFunc(sortByProgressFunc()) name1 := "Bar#1:" bar1 := p.AddBar(100). diff --git a/example/stress/main.go b/example/stress/main.go index 259784d..028730d 100644 --- a/example/stress/main.go +++ b/example/stress/main.go @@ -17,7 +17,7 @@ func main() { var wg sync.WaitGroup - p := mpb.New(nil) + p := mpb.New() wg.Add(totalBars) for i := 0; i < totalBars; i++ { diff --git a/example_test.go b/example_test.go index cdedaab..c7ea9ee 100644 --- a/example_test.go +++ b/example_test.go @@ -4,6 +4,7 @@ "fmt" "math/rand" "time" + "unicode/utf8" "github.com/vbauerster/mpb" ) @@ -12,7 +13,7 @@ // Star mpb's rendering goroutine. // If you don't plan to cancel, feed with nil // otherwise provide context.Context, see cancel example - p := mpb.New(nil) + p := mpb.New() // Set custom width for every bar, which mpb will contain // The default one in 70 p.SetWidth(80) @@ -39,7 +40,7 @@ } func ExampleBar_InProgress() { - p := mpb.New(nil) + p := mpb.New() bar := p.AddBar(100).AppendPercentage(5, 0) for bar.InProgress() { @@ -50,12 +51,14 @@ func ExampleBar_PrependFunc() { decor := func(s *mpb.Statistics, myWidth chan<- int, maxWidth <-chan int) string { - str := fmt.Sprintf("%d/%d", s.Current, s.Total) - return fmt.Sprintf("%8s", str) + str := fmt.Sprintf("%3d/%3d", s.Current, s.Total) + myWidth <- utf8.RuneCountInString(str) + max := <-maxWidth + return fmt.Sprintf(fmt.Sprintf("%%%ds", max+1), str) } totalItem := 100 - p := mpb.New(nil) + p := mpb.New() bar := p.AddBar(int64(totalItem)).PrependFunc(decor) for i := 0; i < totalItem; i++ { @@ -66,12 +69,14 @@ func ExampleBar_AppendFunc() { decor := func(s *mpb.Statistics, myWidth chan<- int, maxWidth <-chan int) string { - str := fmt.Sprintf("%d/%d", s.Current, s.Total) - return fmt.Sprintf("%8s", str) + str := fmt.Sprintf("%3d/%3d", s.Current, s.Total) + myWidth <- utf8.RuneCountInString(str) + max := <-maxWidth + return fmt.Sprintf(fmt.Sprintf("%%%ds", max+1), str) } totalItem := 100 - p := mpb.New(nil) + p := mpb.New() bar := p.AddBar(int64(totalItem)).AppendFunc(decor) for i := 0; i < totalItem; i++ { diff --git a/progress_test.go b/progress_test.go index 773068e..8ce36fe 100644 --- a/progress_test.go +++ b/progress_test.go @@ -7,7 +7,7 @@ func TestAddBar(t *testing.T) { var buf bytes.Buffer - p := New(nil).SetWidth(60).SetOut(&buf) + p := New().SetWidth(60).SetOut(&buf) count := p.BarCount() if count != 0 { t.Errorf("Count want: %q, got: %q\n", 0, count) @@ -24,7 +24,7 @@ } func TestRemoveBar(t *testing.T) { - p := New(nil) + p := New() b := p.AddBar(10) if !p.RemoveBar(b) {