diff --git a/_examples/merge/main.go b/_examples/merge/main.go index ca1e49f..017755a 100644 --- a/_examples/merge/main.go +++ b/_examples/merge/main.go @@ -1,8 +1,8 @@ package main import ( - "fmt" "math/rand" + "strings" "sync" "time" @@ -17,34 +17,31 @@ func main() { var wg sync.WaitGroup // pass &wg (optional), so p will wait for it eventually - p := mpb.New(mpb.WithWaitGroup(&wg)) + p := mpb.New(mpb.WithWaitGroup(&wg), mpb.WithWidth(60)) total, numBars := 100, 3 wg.Add(numBars) for i := 0; i < numBars; i++ { - name := fmt.Sprintf("Bar#%d:", i) var pdecorators mpb.BarOption if i == 0 { - pdecorators = mpb.PrependDecorators(decor.Name(name), - // Merge to sync width with CountersNoUnit and Percentage decorators + pdecorators = mpb.PrependDecorators( + // Merge to sync width with decorators on lines 37 and 38 decor.Merge( - decor.OnComplete(variadicName(decor.WCSyncSpace), "done"), + // decor.OnComplete(newVariadicSpinner(decor.WCSyncSpace), "done"), + newVariadicSpinner(decor.WCSyncSpace), decor.WCSyncSpace, // Placeholder ), ) } else { - pdecorators = mpb.PrependDecorators(decor.Name(name), + pdecorators = mpb.PrependDecorators( decor.CountersNoUnit("% .1d / % .1d", decor.WCSyncSpace), - decor.Percentage(decor.WCSyncSpace), + decor.OnComplete(decor.Spinner(nil, decor.WCSyncSpace), "done"), ) } bar := p.AddBar(int64(total), pdecorators, mpb.AppendDecorators( - decor.OnComplete( - // ETA decorator with ewma age of 60 - decor.EwmaETA(decor.ET_STYLE_GO, 60), "done", - ), + decor.OnComplete(decor.EwmaETA(decor.ET_STYLE_GO, 60), "done"), ), ) // simulating some work @@ -63,32 +60,30 @@ p.Wait() } -func variadicName(wc decor.WC) decor.Decorator { +func newVariadicSpinner(wc decor.WC) decor.Decorator { wc.Init() - d := &varName{ + d := &variadicSpinner{ WC: wc, + d: decor.Spinner(nil), } return d } -type varName struct { +type variadicSpinner struct { decor.WC + d decor.Decorator complete *string } -func (d *varName) Decor(st *decor.Statistics) string { +func (d *variadicSpinner) Decor(st *decor.Statistics) string { if st.Completed && d.complete != nil { return d.FormatMsg(*d.complete) } - if st.Current < 30 { - return d.FormatMsg("low low low") - } else if st.Current < 70 { - return d.FormatMsg("medium medium medium") - } else { - return d.FormatMsg("high high high high high high") - } + msg := d.d.Decor(st) + msg = strings.Repeat(msg, int(st.Current/3)) + return d.FormatMsg(msg) } -func (d *varName) OnCompleteMessage(msg string) { +func (d *variadicSpinner) OnCompleteMessage(msg string) { d.complete = &msg }