Codebase list golang-github-vbauerster-mpb / 1d872f0
simplify poplog example Vladimir Bauer 5 years ago
1 changed file(s) with 18 addition(s) and 43 deletion(s). Raw diff Collapse all Expand all
11
22 import (
33 "fmt"
4 "io"
54 "math/rand"
6 "sync"
75 "time"
86
97 "github.com/vbauerster/mpb/v5"
1311 func main() {
1412 p := mpb.New(mpb.PopCompletedMode())
1513
16 total, numBars := 100, 2
14 total, numBars := 100, 4
1715 for i := 0; i < numBars; i++ {
1816 name := fmt.Sprintf("Bar#%d:", i)
1917 bar := p.AddBar(int64(total),
20 mpb.BarNoPop(),
18 mpb.BarFillerOnComplete(fmt.Sprintf("%s has been completed", name)),
19 mpb.BarFillerTrim(),
2120 mpb.PrependDecorators(
22 decor.Name(name),
23 decor.Percentage(decor.WCSyncSpace),
21 decor.OnComplete(decor.Name(name), ""),
22 decor.OnComplete(decor.NewPercentage(" % d "), ""),
2423 ),
2524 mpb.AppendDecorators(
26 decor.OnComplete(
27 decor.EwmaETA(decor.ET_STYLE_GO, 60), "done!",
28 ),
25 decor.OnComplete(decor.Name(" "), ""),
26 decor.OnComplete(decor.EwmaETA(decor.ET_STYLE_GO, 60), ""),
2927 ),
3028 )
3129 // simulating some work
32 go func() {
33 rng := rand.New(rand.NewSource(time.Now().UnixNano()))
34 max := 100 * time.Millisecond
35 for i := 0; i < total; i++ {
36 // start variable is solely for EWMA calculation
37 // EWMA's unit of measure is an iteration's duration
38 start := time.Now()
39 time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10)
40 bar.Increment()
41 // we need to call DecoratorEwmaUpdate to fulfill ewma decorator's contract
42 bar.DecoratorEwmaUpdate(time.Since(start))
43 }
44 }()
30 rng := rand.New(rand.NewSource(time.Now().UnixNano()))
31 max := 100 * time.Millisecond
32 for i := 0; i < total; i++ {
33 // start variable is solely for EWMA calculation
34 // EWMA's unit of measure is an iteration's duration
35 start := time.Now()
36 time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10)
37 bar.Increment()
38 // we need to call DecoratorEwmaUpdate to fulfill ewma decorator's contract
39 bar.DecoratorEwmaUpdate(time.Since(start))
40 }
4541 }
4642
47 var wg sync.WaitGroup
48 wg.Add(1)
49 go func() {
50 defer wg.Done()
51 rng := rand.New(rand.NewSource(time.Now().UnixNano()))
52 max := 3000 * time.Millisecond
53 for i := 0; i < 10; i++ {
54 filler := makeLogBar(fmt.Sprintf("some log: %d", i))
55 p.Add(0, filler).SetTotal(0, true)
56 time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10)
57 }
58 }()
59
60 wg.Wait()
6143 p.Wait()
6244 }
63
64 func makeLogBar(msg string) mpb.BarFiller {
65 limit := "%%.%ds"
66 return mpb.BarFillerFunc(func(w io.Writer, _ int, st decor.Statistics) {
67 fmt.Fprintf(w, fmt.Sprintf(limit, st.AvailableWidth), msg)
68 })
69 }