merge example upd
Vladimir Bauer
7 years ago
| 0 | 0 | package main |
| 1 | 1 | |
| 2 | 2 | import ( |
| 3 | "fmt" | |
| 4 | 3 | "math/rand" |
| 4 | "strings" | |
| 5 | 5 | "sync" |
| 6 | 6 | "time" |
| 7 | 7 | |
| 16 | 16 | func main() { |
| 17 | 17 | var wg sync.WaitGroup |
| 18 | 18 | // pass &wg (optional), so p will wait for it eventually |
| 19 | p := mpb.New(mpb.WithWaitGroup(&wg)) | |
| 19 | p := mpb.New(mpb.WithWaitGroup(&wg), mpb.WithWidth(60)) | |
| 20 | 20 | total, numBars := 100, 3 |
| 21 | 21 | wg.Add(numBars) |
| 22 | 22 | |
| 23 | 23 | for i := 0; i < numBars; i++ { |
| 24 | name := fmt.Sprintf("Bar#%d:", i) | |
| 25 | 24 | var pdecorators mpb.BarOption |
| 26 | 25 | if i == 0 { |
| 27 | pdecorators = mpb.PrependDecorators(decor.Name(name), | |
| 28 | // Merge to sync width with CountersNoUnit and Percentage decorators | |
| 26 | pdecorators = mpb.PrependDecorators( | |
| 27 | // Merge to sync width with decorators on lines 37 and 38 | |
| 29 | 28 | decor.Merge( |
| 30 | decor.OnComplete(variadicName(decor.WCSyncSpace), "done"), | |
| 29 | // decor.OnComplete(newVariadicSpinner(decor.WCSyncSpace), "done"), | |
| 30 | newVariadicSpinner(decor.WCSyncSpace), | |
| 31 | 31 | decor.WCSyncSpace, // Placeholder |
| 32 | 32 | ), |
| 33 | 33 | ) |
| 34 | 34 | } else { |
| 35 | pdecorators = mpb.PrependDecorators(decor.Name(name), | |
| 35 | pdecorators = mpb.PrependDecorators( | |
| 36 | 36 | decor.CountersNoUnit("% .1d / % .1d", decor.WCSyncSpace), |
| 37 | decor.Percentage(decor.WCSyncSpace), | |
| 37 | decor.OnComplete(decor.Spinner(nil, decor.WCSyncSpace), "done"), | |
| 38 | 38 | ) |
| 39 | 39 | } |
| 40 | 40 | bar := p.AddBar(int64(total), |
| 41 | 41 | pdecorators, |
| 42 | 42 | mpb.AppendDecorators( |
| 43 | decor.OnComplete( | |
| 44 | // ETA decorator with ewma age of 60 | |
| 45 | decor.EwmaETA(decor.ET_STYLE_GO, 60), "done", | |
| 46 | ), | |
| 43 | decor.OnComplete(decor.EwmaETA(decor.ET_STYLE_GO, 60), "done"), | |
| 47 | 44 | ), |
| 48 | 45 | ) |
| 49 | 46 | // simulating some work |
| 62 | 59 | p.Wait() |
| 63 | 60 | } |
| 64 | 61 | |
| 65 | func variadicName(wc decor.WC) decor.Decorator { | |
| 62 | func newVariadicSpinner(wc decor.WC) decor.Decorator { | |
| 66 | 63 | wc.Init() |
| 67 | d := &varName{ | |
| 64 | d := &variadicSpinner{ | |
| 68 | 65 | WC: wc, |
| 66 | d: decor.Spinner(nil), | |
| 69 | 67 | } |
| 70 | 68 | return d |
| 71 | 69 | } |
| 72 | 70 | |
| 73 | type varName struct { | |
| 71 | type variadicSpinner struct { | |
| 74 | 72 | decor.WC |
| 73 | d decor.Decorator | |
| 75 | 74 | complete *string |
| 76 | 75 | } |
| 77 | 76 | |
| 78 | func (d *varName) Decor(st *decor.Statistics) string { | |
| 77 | func (d *variadicSpinner) Decor(st *decor.Statistics) string { | |
| 79 | 78 | if st.Completed && d.complete != nil { |
| 80 | 79 | return d.FormatMsg(*d.complete) |
| 81 | 80 | } |
| 82 | if st.Current < 30 { | |
| 83 | return d.FormatMsg("low low low") | |
| 84 | } else if st.Current < 70 { | |
| 85 | return d.FormatMsg("medium medium medium") | |
| 86 | } else { | |
| 87 | return d.FormatMsg("high high high high high high") | |
| 88 | } | |
| 81 | msg := d.d.Decor(st) | |
| 82 | msg = strings.Repeat(msg, int(st.Current/3)) | |
| 83 | return d.FormatMsg(msg) | |
| 89 | 84 | } |
| 90 | 85 | |
| 91 | func (d *varName) OnCompleteMessage(msg string) { | |
| 86 | func (d *variadicSpinner) OnCompleteMessage(msg string) { | |
| 92 | 87 | d.complete = &msg |
| 93 | 88 | } |