Codebase list golang-github-vbauerster-mpb / 54ea773
refactoring suppressBar Vladimir Bauer 3 years ago
1 changed file(s) with 27 addition(s) and 20 deletion(s). Raw diff Collapse all Expand all
1919 msgCh := make(chan string)
2020 resumeCh := make(chan struct{})
2121 nextCh := make(chan struct{}, 1)
22 ew := &errorWrapper{}
23 timer := time.AfterFunc(2*time.Second, func() {
24 ew.reset(errors.New("timeout"))
25 })
26 defer timer.Stop()
2227 bar := p.AddBar(int64(total),
2328 mpb.BarFillerMiddleware(func(base mpb.BarFiller) mpb.BarFiller {
2429 var msg *string
25 return mpb.BarFillerFunc(func(w io.Writer, st decor.Statistics) {
26 select {
27 case m := <-msgCh:
30 var times int
31 return mpb.BarFillerFunc(func(w io.Writer, st decor.Statistics) error {
32 if msg == nil {
33 select {
34 case m := <-msgCh:
35 msg = &m
36 times = 10
37 nextCh <- struct{}{}
38 default:
39 }
40 return base.Fill(w, st)
41 }
42 switch {
43 case times == 0, st.Completed, st.Aborted:
2844 defer func() {
29 msg = &m
45 msg = nil
3046 }()
31 nextCh <- struct{}{}
32 case <-resumeCh:
33 msg = nil
47 resumeCh <- struct{}{}
3448 default:
49 times--
3550 }
36 if msg != nil {
37 io.WriteString(w, runewidth.Truncate(*msg, st.AvailableWidth, "…"))
38 nextCh <- struct{}{}
39 } else {
40 base.Fill(w, st)
41 }
51 _, err := io.WriteString(w, runewidth.Truncate(*msg, st.AvailableWidth, "…"))
52 nextCh <- struct{}{}
53 return err
4254 })
4355 }),
4456 mpb.PrependDecorators(decor.Name("my bar:")),
4557 mpb.AppendDecorators(newCustomPercentage(nextCh)),
4658 )
47 ew := &errorWrapper{}
48 time.AfterFunc(2*time.Second, func() {
49 ew.reset(errors.New("timeout"))
50 })
5159 // simulating some work
5260 go func() {
5361 rng := rand.New(rand.NewSource(time.Now().UnixNano()))
5664 time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10)
5765 if ew.isErr() {
5866 msgCh <- fmt.Sprintf("%s at %d, retrying...", ew.Error(), i)
59 go ew.reset(nil)
6067 i--
6168 bar.SetRefill(int64(i))
62 time.Sleep(3 * time.Second)
63 resumeCh <- struct{}{}
69 ew.reset(nil)
70 <-resumeCh
6471 continue
6572 }
6673 bar.Increment()