simple condFillerBuilder for reverseBar example
Vladimir Bauer
2 years ago
| 16 | 16 | total, numBars := 100, 3 |
| 17 | 17 | wg.Add(numBars) |
| 18 | 18 | |
| 19 | condFillerBuilder := func(cond bool) mpb.BarFillerBuilder { | |
| 20 | if cond { // reverse Bar on cond | |
| 21 | return mpb.BarStyle().Tip("<").Reverse() | |
| 22 | } | |
| 23 | return mpb.BarStyle() | |
| 24 | } | |
| 25 | ||
| 19 | 26 | for i := 0; i < numBars; i++ { |
| 20 | 27 | name := fmt.Sprintf("Bar#%d:", i) |
| 21 | bar := p.New(int64(total), condBuilder(i == 1), | |
| 28 | bar := p.New(int64(total), | |
| 29 | condFillerBuilder(i == 1), | |
| 22 | 30 | mpb.PrependDecorators( |
| 23 | 31 | // simple name decorator |
| 24 | 32 | decor.Name(name), |
| 51 | 59 | // wait for passed wg and for all bars to complete and flush |
| 52 | 60 | p.Wait() |
| 53 | 61 | } |
| 54 | ||
| 55 | func condBuilder(cond bool) mpb.BarFillerBuilder { | |
| 56 | return mpb.BarFillerBuilderFunc(func() mpb.BarFiller { | |
| 57 | bs := mpb.BarStyle() | |
| 58 | if cond { | |
| 59 | // reverse Bar on cond | |
| 60 | bs = bs.Tip("<").Reverse() | |
| 61 | } | |
| 62 | return bs.Build() | |
| 63 | }) | |
| 64 | } | |