diff --git a/README.md b/README.md index ee1b212..ad916fd 100644 --- a/README.md +++ b/README.md @@ -36,10 +36,10 @@ total := 100 name := "Single Bar:" - // adding a single bar, which will inherit container's width - bar := p.Add(int64(total), - // progress bar filler with customized style - mpb.NewBarFiller(mpb.BarStyle().Lbound("╢").Filler("▌").Tip("▌").Padding("░").Rbound("╟")), + // create a single bar, which will inherit container's width + bar := p.New(int64(total), + // BarFillerBuilder with custom style + mpb.BarStyle().Lbound("╢").Filler("▌").Tip("▌").Padding("░").Rbound("╟"), 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/decoratorsOnTop/main.go b/_examples/decoratorsOnTop/main.go index 16163b6..a6e0ae6 100644 --- a/_examples/decoratorsOnTop/main.go +++ b/_examples/decoratorsOnTop/main.go @@ -13,7 +13,9 @@ p := mpb.New() total := 100 - bar := p.Add(int64(total), nil, + bar := p.New(int64(total), + mpb.NopStyle(), // make main bar style nop, so there are just decorators + mpb.BarExtender(extended(mpb.BarStyle())), // extend wtih normal bar on the next line mpb.PrependDecorators( decor.Name("Percentage: "), decor.NewPercentage("%d"), @@ -24,7 +26,6 @@ decor.AverageETA(decor.ET_STYLE_GO), "done", ), ), - mpb.BarExtender(nlBarFiller(mpb.NewBarFiller(mpb.BarStyle()))), ) // simulating some work max := 100 * time.Millisecond @@ -36,7 +37,8 @@ p.Wait() } -func nlBarFiller(filler mpb.BarFiller) mpb.BarFiller { +func extended(builder mpb.BarFillerBuilder) mpb.BarFiller { + filler := builder.Build() 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/io/main.go b/_examples/io/main.go index 6f2bf89..339d853 100644 --- a/_examples/io/main.go +++ b/_examples/io/main.go @@ -19,8 +19,8 @@ mpb.WithRefreshRate(180*time.Millisecond), ) - bar := p.Add(total, - mpb.NewBarFiller(mpb.BarStyle().Rbound("|")), + bar := p.New(total, + mpb.BarStyle().Rbound("|"), mpb.PrependDecorators( decor.CountersKibiByte("% .2f / % .2f"), ), diff --git a/_examples/mexicanBar/main.go b/_examples/mexicanBar/main.go index c157ae9..ce2a733 100644 --- a/_examples/mexicanBar/main.go +++ b/_examples/mexicanBar/main.go @@ -20,8 +20,7 @@ bs.Tip("\u001b[0m⛵\u001b[36;1m") bs.Padding("_") bs.Rbound("\u001b[0m]") - bar := p.Add(int64(total), - mpb.NewBarFiller(bs), + bar := p.New(int64(total), bs, mpb.PrependDecorators(decor.Name(name)), mpb.AppendDecorators(decor.Percentage()), ) diff --git a/_examples/reverseBar/main.go b/_examples/reverseBar/main.go index 14a13be..6ac4378 100644 --- a/_examples/reverseBar/main.go +++ b/_examples/reverseBar/main.go @@ -12,20 +12,16 @@ func main() { var wg sync.WaitGroup - // pass &wg (optional), so p will wait for it eventually - p := mpb.New(mpb.WithWaitGroup(&wg)) + p := mpb.New( + // passing &wg will make p.Wait() call wait for it first + mpb.WithWaitGroup(&wg), + ) total, numBars := 100, 3 wg.Add(numBars) for i := 0; i < numBars; i++ { name := fmt.Sprintf("Bar#%d:", i) - bs := mpb.BarStyle() - if i == 1 { - // reverse Bar#1 - bs = bs.Tip("<").Reverse() - } - bar := p.Add(int64(total), - mpb.NewBarFiller(bs), + bar := p.New(int64(total), condBuilder(i == 1), mpb.PrependDecorators( // simple name decorator decor.Name(name), @@ -56,6 +52,17 @@ } }() } - // Waiting for passed &wg and for all bars to complete and flush + // wait for all bars to complete and flush p.Wait() } + +func condBuilder(cond bool) mpb.BarFillerBuilderFunc { + return mpb.BarFillerBuilderFunc(func() mpb.BarFiller { + bs := mpb.BarStyle() + if cond { + // reverse Bar on cond + bs = bs.Tip("<").Reverse() + } + return bs.Build() + }) +} diff --git a/_examples/singleBar/main.go b/_examples/singleBar/main.go index f0b87a3..925a2d5 100644 --- a/_examples/singleBar/main.go +++ b/_examples/singleBar/main.go @@ -14,10 +14,10 @@ total := 100 name := "Single Bar:" - // adding a single bar, which will inherit container's width - bar := p.Add(int64(total), - // progress bar filler with customized style - mpb.NewBarFiller(mpb.BarStyle().Lbound("╢").Filler("▌").Tip("▌").Padding("░").Rbound("╟")), + // create a single bar, which will inherit container's width + bar := p.New(int64(total), + // BarFillerBuilder with custom style + mpb.BarStyle().Lbound("╢").Filler("▌").Tip("▌").Padding("░").Rbound("╟"), 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/spinTipBar/main.go b/_examples/spinTipBar/main.go index 51aeb10..7d3f2ef 100644 --- a/_examples/spinTipBar/main.go +++ b/_examples/spinTipBar/main.go @@ -14,8 +14,8 @@ total := 100 name := "Single Bar:" - bar := p.Add(int64(total), - mpb.NewBarFiller(mpb.BarStyle().Tip(`-`, `\`, `|`, `/`)), + bar := p.New(int64(total), + mpb.BarStyle().Tip(`-`, `\`, `|`, `/`), mpb.PrependDecorators(decor.Name(name)), mpb.AppendDecorators(decor.Percentage()), ) diff --git a/_examples/spinnerBar/main.go b/_examples/spinnerBar/main.go index bf3c8db..368f169 100644 --- a/_examples/spinnerBar/main.go +++ b/_examples/spinnerBar/main.go @@ -13,49 +13,28 @@ func main() { var wg sync.WaitGroup p := mpb.New( + // passing &wg will make p.Wait() call wait for it first mpb.WithWaitGroup(&wg), - mpb.WithWidth(14), + mpb.WithWidth(16), ) 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.Add(int64(total), - mpb.NewBarFiller(mpb.BarStyle().Lbound("╢").Filler("▌").Tip("▌").Padding("░").Rbound("╟")), - mpb.PrependDecorators( - // simple name decorator - decor.Name(name), + bar := p.New(int64(total), condBuilder(i != 0), + mpb.PrependDecorators( + // simple name decorator + decor.Name(name), + ), + mpb.AppendDecorators( + // replace ETA decorator with "done" message, OnComplete event + decor.OnComplete( + // ETA decorator with ewma age of 60 + decor.EwmaETA(decor.ET_STYLE_GO, 60), "done", ), - mpb.AppendDecorators( - // replace ETA decorator with "done" message, OnComplete event - decor.OnComplete( - // ETA decorator with ewma age of 60 - decor.EwmaETA(decor.ET_STYLE_GO, 60), "done", - ), - ), - ) - } else { - bar = p.Add(int64(total), - mpb.NewBarFiller(mpb.SpinnerStyle(spinnerStyle...)), - mpb.PrependDecorators( - // simple name decorator - decor.Name(name), - ), - mpb.AppendDecorators( - // replace ETA decorator with "done" message, OnComplete event - decor.OnComplete( - // ETA decorator with ewma age of 60 - decor.EwmaETA(decor.ET_STYLE_GO, 60), "done", - ), - ), - ) - } - + ), + ) // simulating some work go func() { defer wg.Done() @@ -75,3 +54,14 @@ // wait for all bars to complete and flush p.Wait() } + +func condBuilder(cond bool) mpb.BarFillerBuilderFunc { + return mpb.BarFillerBuilderFunc(func() mpb.BarFiller { + if cond { + // spinner Bar on cond + frames := []string{"∙∙∙", "●∙∙", "∙●∙", "∙∙●", "∙∙∙"} + return mpb.SpinnerStyle(frames...).Build() + } + return mpb.BarStyle().Lbound("╢").Filler("▌").Tip("▌").Padding("░").Rbound("╟").Build() + }) +} diff --git a/_examples/tipOnComplete/main.go b/_examples/tipOnComplete/main.go index b4d5a23..62a26b7 100644 --- a/_examples/tipOnComplete/main.go +++ b/_examples/tipOnComplete/main.go @@ -14,8 +14,8 @@ total := 100 name := "Single Bar:" - bar := p.Add(int64(total), - mpb.NewBarFiller(mpb.BarStyle().TipOnComplete(">")), + bar := p.New(int64(total), + mpb.BarStyle().TipOnComplete(">"), mpb.PrependDecorators(decor.Name(name)), mpb.AppendDecorators(decor.Percentage()), ) diff --git a/example_test.go b/example_test.go index cde49f8..072c8b6 100644 --- a/example_test.go +++ b/example_test.go @@ -17,10 +17,10 @@ total := 100 name := "Single Bar:" - // adding a single bar, which will inherit container's width - bar := p.Add(int64(total), - // progress bar filler with customized style - mpb.NewBarFiller(mpb.BarStyle().Lbound("╢").Filler("▌").Tip("▌").Padding("░").Rbound("╟")), + // create a single bar, which will inherit container's width + bar := p.New(int64(total), + // BarFillerBuilder with custom style + mpb.BarStyle().Lbound("╢").Filler("▌").Tip("▌").Padding("░").Rbound("╟"), mpb.PrependDecorators( // display our name with one space on the right decor.Name(name, decor.WC{W: len(name) + 1, C: decor.DidentRight}),