Codebase list golang-github-vbauerster-mpb / 1b876c7
merge example upd Vladimir Bauer 7 years ago
1 changed file(s) with 19 addition(s) and 24 deletion(s). Raw diff Collapse all Expand all
00 package main
11
22 import (
3 "fmt"
43 "math/rand"
4 "strings"
55 "sync"
66 "time"
77
1616 func main() {
1717 var wg sync.WaitGroup
1818 // 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))
2020 total, numBars := 100, 3
2121 wg.Add(numBars)
2222
2323 for i := 0; i < numBars; i++ {
24 name := fmt.Sprintf("Bar#%d:", i)
2524 var pdecorators mpb.BarOption
2625 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
2928 decor.Merge(
30 decor.OnComplete(variadicName(decor.WCSyncSpace), "done"),
29 // decor.OnComplete(newVariadicSpinner(decor.WCSyncSpace), "done"),
30 newVariadicSpinner(decor.WCSyncSpace),
3131 decor.WCSyncSpace, // Placeholder
3232 ),
3333 )
3434 } else {
35 pdecorators = mpb.PrependDecorators(decor.Name(name),
35 pdecorators = mpb.PrependDecorators(
3636 decor.CountersNoUnit("% .1d / % .1d", decor.WCSyncSpace),
37 decor.Percentage(decor.WCSyncSpace),
37 decor.OnComplete(decor.Spinner(nil, decor.WCSyncSpace), "done"),
3838 )
3939 }
4040 bar := p.AddBar(int64(total),
4141 pdecorators,
4242 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"),
4744 ),
4845 )
4946 // simulating some work
6259 p.Wait()
6360 }
6461
65 func variadicName(wc decor.WC) decor.Decorator {
62 func newVariadicSpinner(wc decor.WC) decor.Decorator {
6663 wc.Init()
67 d := &varName{
64 d := &variadicSpinner{
6865 WC: wc,
66 d: decor.Spinner(nil),
6967 }
7068 return d
7169 }
7270
73 type varName struct {
71 type variadicSpinner struct {
7472 decor.WC
73 d decor.Decorator
7574 complete *string
7675 }
7776
78 func (d *varName) Decor(st *decor.Statistics) string {
77 func (d *variadicSpinner) Decor(st *decor.Statistics) string {
7978 if st.Completed && d.complete != nil {
8079 return d.FormatMsg(*d.complete)
8180 }
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)
8984 }
9085
91 func (d *varName) OnCompleteMessage(msg string) {
86 func (d *variadicSpinner) OnCompleteMessage(msg string) {
9287 d.complete = &msg
9388 }