Codebase list golang-github-vbauerster-mpb / a3bfb16
prefer *Progress.New in examples Vladimir Bauer 4 years ago
10 changed file(s) with 66 addition(s) and 68 deletion(s). Raw diff Collapse all Expand all
3535
3636 total := 100
3737 name := "Single Bar:"
38 // adding a single bar, which will inherit container's width
39 bar := p.Add(int64(total),
40 // progress bar filler with customized style
41 mpb.NewBarFiller(mpb.BarStyle().Lbound("╢").Filler("▌").Tip("▌").Padding("░").Rbound("╟")),
38 // create a single bar, which will inherit container's width
39 bar := p.New(int64(total),
40 // BarFillerBuilder with custom style
41 mpb.BarStyle().Lbound("╢").Filler("▌").Tip("▌").Padding("░").Rbound("╟"),
4242 mpb.PrependDecorators(
4343 // display our name with one space on the right
4444 decor.Name(name, decor.WC{W: len(name) + 1, C: decor.DidentRight}),
1212 p := mpb.New()
1313
1414 total := 100
15 bar := p.Add(int64(total), nil,
15 bar := p.New(int64(total),
16 mpb.NopStyle(), // make main bar style nop, so there are just decorators
17 mpb.BarExtender(extended(mpb.BarStyle())), // extend wtih normal bar on the next line
1618 mpb.PrependDecorators(
1719 decor.Name("Percentage: "),
1820 decor.NewPercentage("%d"),
2325 decor.AverageETA(decor.ET_STYLE_GO), "done",
2426 ),
2527 ),
26 mpb.BarExtender(nlBarFiller(mpb.NewBarFiller(mpb.BarStyle()))),
2728 )
2829 // simulating some work
2930 max := 100 * time.Millisecond
3536 p.Wait()
3637 }
3738
38 func nlBarFiller(filler mpb.BarFiller) mpb.BarFiller {
39 func extended(builder mpb.BarFillerBuilder) mpb.BarFiller {
40 filler := builder.Build()
3941 return mpb.BarFillerFunc(func(w io.Writer, reqWidth int, st decor.Statistics) {
4042 filler.Fill(w, reqWidth, st)
4143 w.Write([]byte("\n"))
1818 mpb.WithRefreshRate(180*time.Millisecond),
1919 )
2020
21 bar := p.Add(total,
22 mpb.NewBarFiller(mpb.BarStyle().Rbound("|")),
21 bar := p.New(total,
22 mpb.BarStyle().Rbound("|"),
2323 mpb.PrependDecorators(
2424 decor.CountersKibiByte("% .2f / % .2f"),
2525 ),
1919 bs.Tip("\u001b[0m⛵\u001b[36;1m")
2020 bs.Padding("_")
2121 bs.Rbound("\u001b[0m]")
22 bar := p.Add(int64(total),
23 mpb.NewBarFiller(bs),
22 bar := p.New(int64(total), bs,
2423 mpb.PrependDecorators(decor.Name(name)),
2524 mpb.AppendDecorators(decor.Percentage()),
2625 )
1111
1212 func main() {
1313 var wg sync.WaitGroup
14 // pass &wg (optional), so p will wait for it eventually
15 p := mpb.New(mpb.WithWaitGroup(&wg))
14 p := mpb.New(
15 // passing &wg will make p.Wait() call wait for it first
16 mpb.WithWaitGroup(&wg),
17 )
1618 total, numBars := 100, 3
1719 wg.Add(numBars)
1820
1921 for i := 0; i < numBars; i++ {
2022 name := fmt.Sprintf("Bar#%d:", i)
21 bs := mpb.BarStyle()
22 if i == 1 {
23 // reverse Bar#1
24 bs = bs.Tip("<").Reverse()
25 }
26 bar := p.Add(int64(total),
27 mpb.NewBarFiller(bs),
23 bar := p.New(int64(total), condBuilder(i == 1),
2824 mpb.PrependDecorators(
2925 // simple name decorator
3026 decor.Name(name),
5551 }
5652 }()
5753 }
58 // Waiting for passed &wg and for all bars to complete and flush
54 // wait for all bars to complete and flush
5955 p.Wait()
6056 }
57
58 func condBuilder(cond bool) mpb.BarFillerBuilderFunc {
59 return mpb.BarFillerBuilderFunc(func() mpb.BarFiller {
60 bs := mpb.BarStyle()
61 if cond {
62 // reverse Bar on cond
63 bs = bs.Tip("<").Reverse()
64 }
65 return bs.Build()
66 })
67 }
1313
1414 total := 100
1515 name := "Single Bar:"
16 // adding a single bar, which will inherit container's width
17 bar := p.Add(int64(total),
18 // progress bar filler with customized style
19 mpb.NewBarFiller(mpb.BarStyle().Lbound("╢").Filler("▌").Tip("▌").Padding("░").Rbound("╟")),
16 // create a single bar, which will inherit container's width
17 bar := p.New(int64(total),
18 // BarFillerBuilder with custom style
19 mpb.BarStyle().Lbound("╢").Filler("▌").Tip("▌").Padding("░").Rbound("╟"),
2020 mpb.PrependDecorators(
2121 // display our name with one space on the right
2222 decor.Name(name, decor.WC{W: len(name) + 1, C: decor.DidentRight}),
1313
1414 total := 100
1515 name := "Single Bar:"
16 bar := p.Add(int64(total),
17 mpb.NewBarFiller(mpb.BarStyle().Tip(`-`, `\`, `|`, `/`)),
16 bar := p.New(int64(total),
17 mpb.BarStyle().Tip(`-`, `\`, `|`, `/`),
1818 mpb.PrependDecorators(decor.Name(name)),
1919 mpb.AppendDecorators(decor.Percentage()),
2020 )
1212 func main() {
1313 var wg sync.WaitGroup
1414 p := mpb.New(
15 // passing &wg will make p.Wait() call wait for it first
1516 mpb.WithWaitGroup(&wg),
16 mpb.WithWidth(14),
17 mpb.WithWidth(16),
1718 )
1819 total, numBars := 101, 3
1920 wg.Add(numBars)
2021
21 spinnerStyle := []string{"∙∙∙", "●∙∙", "∙●∙", "∙∙●", "∙∙∙"}
22
2322 for i := 0; i < numBars; i++ {
2423 name := fmt.Sprintf("Bar#%d:", i)
25 var bar *mpb.Bar
26 if i == 0 {
27 bar = p.Add(int64(total),
28 mpb.NewBarFiller(mpb.BarStyle().Lbound("╢").Filler("▌").Tip("▌").Padding("░").Rbound("╟")),
29 mpb.PrependDecorators(
30 // simple name decorator
31 decor.Name(name),
24 bar := p.New(int64(total), condBuilder(i != 0),
25 mpb.PrependDecorators(
26 // simple name decorator
27 decor.Name(name),
28 ),
29 mpb.AppendDecorators(
30 // replace ETA decorator with "done" message, OnComplete event
31 decor.OnComplete(
32 // ETA decorator with ewma age of 60
33 decor.EwmaETA(decor.ET_STYLE_GO, 60), "done",
3234 ),
33 mpb.AppendDecorators(
34 // replace ETA decorator with "done" message, OnComplete event
35 decor.OnComplete(
36 // ETA decorator with ewma age of 60
37 decor.EwmaETA(decor.ET_STYLE_GO, 60), "done",
38 ),
39 ),
40 )
41 } else {
42 bar = p.Add(int64(total),
43 mpb.NewBarFiller(mpb.SpinnerStyle(spinnerStyle...)),
44 mpb.PrependDecorators(
45 // simple name decorator
46 decor.Name(name),
47 ),
48 mpb.AppendDecorators(
49 // replace ETA decorator with "done" message, OnComplete event
50 decor.OnComplete(
51 // ETA decorator with ewma age of 60
52 decor.EwmaETA(decor.ET_STYLE_GO, 60), "done",
53 ),
54 ),
55 )
56 }
57
35 ),
36 )
5837 // simulating some work
5938 go func() {
6039 defer wg.Done()
7453 // wait for all bars to complete and flush
7554 p.Wait()
7655 }
56
57 func condBuilder(cond bool) mpb.BarFillerBuilderFunc {
58 return mpb.BarFillerBuilderFunc(func() mpb.BarFiller {
59 if cond {
60 // spinner Bar on cond
61 frames := []string{"∙∙∙", "●∙∙", "∙●∙", "∙∙●", "∙∙∙"}
62 return mpb.SpinnerStyle(frames...).Build()
63 }
64 return mpb.BarStyle().Lbound("╢").Filler("▌").Tip("▌").Padding("░").Rbound("╟").Build()
65 })
66 }
1313
1414 total := 100
1515 name := "Single Bar:"
16 bar := p.Add(int64(total),
17 mpb.NewBarFiller(mpb.BarStyle().TipOnComplete(">")),
16 bar := p.New(int64(total),
17 mpb.BarStyle().TipOnComplete(">"),
1818 mpb.PrependDecorators(decor.Name(name)),
1919 mpb.AppendDecorators(decor.Percentage()),
2020 )
1616
1717 total := 100
1818 name := "Single Bar:"
19 // adding a single bar, which will inherit container's width
20 bar := p.Add(int64(total),
21 // progress bar filler with customized style
22 mpb.NewBarFiller(mpb.BarStyle().Lbound("╢").Filler("▌").Tip("▌").Padding("░").Rbound("╟")),
19 // create a single bar, which will inherit container's width
20 bar := p.New(int64(total),
21 // BarFillerBuilder with custom style
22 mpb.BarStyle().Lbound("╢").Filler("▌").Tip("▌").Padding("░").Rbound("╟"),
2323 mpb.PrependDecorators(
2424 // display our name with one space on the right
2525 decor.Name(name, decor.WC{W: len(name) + 1, C: decor.DidentRight}),