Codebase list golang-github-vbauerster-mpb / 204c673
spinner bar example: style with Meta Vladimir Bauer 2 years ago
1 changed file(s) with 12 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
1919 total, numBars := 101, 3
2020 wg.Add(numBars)
2121
22 condFn := func(cond bool) mpb.BarFiller {
23 if cond {
24 s := mpb.SpinnerStyle("∙∙∙", "●∙∙", "∙●∙", "∙∙●", "∙∙∙")
25 return s.Meta(func(s string) string {
26 return fmt.Sprint("\033[31m", s, "\033[0m") // red
27 }).Build()
28 }
29 return mpb.BarStyle().Lbound("╢").Filler("▌").Tip("▌").Padding("░").Rbound("╟").Build()
30 }
31
2232 for i := 0; i < numBars; i++ {
2333 name := fmt.Sprintf("Bar#%d:", i)
24 bar := p.New(int64(total), condBuilder(i != 0),
34 bar := p.MustAdd(int64(total),
35 condFn(i != 0),
2536 mpb.PrependDecorators(
2637 // simple name decorator
2738 decor.Name(name),
5263 // wait for passed wg and for all bars to complete and flush
5364 p.Wait()
5465 }
55
56 func condBuilder(cond bool) mpb.BarFillerBuilder {
57 return mpb.BarFillerBuilderFunc(func() mpb.BarFiller {
58 if cond {
59 // spinner Bar on cond
60 frames := []string{"∙∙∙", "●∙∙", "∙●∙", "∙∙●", "∙∙∙"}
61 return mpb.SpinnerStyle(frames...).Build()
62 }
63 return mpb.BarStyle().Lbound("╢").Filler("▌").Tip("▌").Padding("░").Rbound("╟").Build()
64 })
65 }