Codebase list golang-github-vbauerster-mpb / 0a29900
different width example Vladimir Bauer 7 years ago
1 changed file(s) with 60 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 package main
1
2 import (
3 "fmt"
4 "math/rand"
5 "sync"
6 "time"
7
8 "github.com/vbauerster/mpb"
9 "github.com/vbauerster/mpb/decor"
10 )
11
12 func init() {
13 rand.Seed(time.Now().UnixNano())
14 }
15
16 func main() {
17 var wg sync.WaitGroup
18 p := mpb.New(
19 mpb.WithWaitGroup(&wg),
20 // container's width.
21 mpb.WithWidth(60),
22 )
23 total, numBars := 100, 3
24 wg.Add(numBars)
25
26 for i := 0; i < numBars; i++ {
27 name := fmt.Sprintf("Bar#%d:", i)
28 bar := p.AddBar(int64(total),
29 // set BarWidth 40 for bar 1 and 2
30 mpb.OptionOnCondition(mpb.BarWidth(40), func() bool { return i > 0 }),
31 mpb.PrependDecorators(
32 // simple name decorator
33 decor.Name(name),
34 // decor.DSyncWidth bit enables column width synchronization
35 decor.Percentage(decor.WCSyncSpace),
36 ),
37 mpb.AppendDecorators(
38 // replace ETA decorator with "done" message, OnComplete event
39 decor.OnComplete(
40 // ETA decorator with ewma age of 60
41 decor.EwmaETA(decor.ET_STYLE_GO, 60), "done",
42 ),
43 ),
44 )
45 // simulating some work
46 go func() {
47 defer wg.Done()
48 max := 100 * time.Millisecond
49 for i := 0; i < total; i++ {
50 start := time.Now()
51 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
52 // ewma based decorators require work duration measurement
53 bar.IncrBy(1, time.Since(start))
54 }
55 }()
56 }
57 // wait for all bars to complete and flush
58 p.Wait()
59 }