spinner bar example: style with Meta
Vladimir Bauer
2 years ago
| 19 | 19 | total, numBars := 101, 3 |
| 20 | 20 | wg.Add(numBars) |
| 21 | 21 | |
| 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 | ||
| 22 | 32 | for i := 0; i < numBars; i++ { |
| 23 | 33 | 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), | |
| 25 | 36 | mpb.PrependDecorators( |
| 26 | 37 | // simple name decorator |
| 27 | 38 | decor.Name(name), |
| 52 | 63 | // wait for passed wg and for all bars to complete and flush |
| 53 | 64 | p.Wait() |
| 54 | 65 | } |
| 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 | } | |