Codebase list golang-github-vbauerster-mpb / ca743b3
Readme update Vladimir Bauer 8 years ago
1 changed file(s) with 26 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
1212 * __Dynamic Total__: [Set total](https://github.com/vbauerster/mpb/issues/9#issuecomment-344448984) while bar is running
1313 * __Dynamic Add/Remove__: Dynamically add or remove bars
1414 * __Cancellation__: Cancel whole rendering process
15 * __Predefined Decorators__: Elapsed time, [Ewmaest](https://github.com/dgryski/trifles/tree/master/ewmaest) based ETA, Percentage, Bytes counter
15 * __Predefined Decorators__: Elapsed time, [ewma](https://github.com/VividCortex/ewma) based ETA, Percentage, Bytes counter
1616 * __Decorator's width sync__: Synchronized decorator's width among multiple bars
1717
1818 ## Installation
3838
3939 total := 100
4040 name := "Single Bar:"
41 startBlock := make(chan time.Time)
4142 // adding a single bar
4243 bar := p.AddBar(int64(total),
4344 mpb.PrependDecorators(
44 // Display our static name with one space on the right
45 decor.StaticName(name, len(name)+1, decor.DidentRight),
46 // ETA decorator with width reservation of 3 runes
47 decor.ETA(3, 0),
45 // Display our name with one space on the right
46 decor.Name(name, decor.WC{W: len(name) + 1, C: decor.DidentRight}),
47 // Replace ETA decorator with message, OnComplete event
48 decor.OnComplete(
49 // ETA decorator with default eta age, and width reservation of 4
50 decor.ETA(decor.ET_STYLE_GO, 0, startBlock, decor.WC{W: 4}),
51 "done",
52 ),
4853 ),
4954 mpb.AppendDecorators(
50 // Percentage decorator with width reservation of 5 runes
51 decor.Percentage(5, 0),
55 decor.Percentage(),
5256 ),
5357 )
5458
5559 // simulating some work
5660 max := 100 * time.Millisecond
5761 for i := 0; i < total; i++ {
58 bar.StartBlock() // optional call, required for ETA
62 // update start block time, required for ETA calculation
63 startBlock <- time.Now()
5964 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
60 // increment by 1 (there is bar.IncrBy(int) method, if needed)
65 // Increment by 1 (there is bar.IncrBy(int) method, if needed)
6166 bar.Increment()
6267 }
6368 // wait for our bar to complete and flush
7378
7479 for i := 0; i < numBars; i++ {
7580 name := fmt.Sprintf("Bar#%d:", i)
81 startBlock := make(chan time.Time)
7682 bar := p.AddBar(int64(total),
7783 mpb.PrependDecorators(
78 // Display our static name with one space on the right
79 decor.StaticName(name, len(name)+1, decor.DidentRight),
80 // DwidthSync bit enables same column width synchronization
81 decor.Percentage(0, decor.DwidthSync),
84 // Display our name with one space on the right
85 decor.Name(name, decor.WC{W: len(name) + 1, C: decor.DidentRight}),
86 // decor.DSyncWidth bit enables same column width synchronization
87 decor.Percentage(decor.WCSyncWidth),
8288 ),
8389 mpb.AppendDecorators(
84 // Replace our ETA decorator with "done!", on bar completion event
85 decor.OnComplete(decor.ETA(3, 0), "done!", 0, 0),
90 // Replace ETA decorator with message, OnComplete event
91 decor.OnComplete(
92 // ETA decorator with default eta age, and width reservation of 3
93 decor.ETA(decor.ET_STYLE_GO, 0, startBlock, decor.WC{W: 3}),
94 "done!",
95 ),
8696 ),
8797 )
8898 // simulating some work
90100 defer wg.Done()
91101 max := 100 * time.Millisecond
92102 for i := 0; i < total; i++ {
93 bar.StartBlock()
103 startBlock <- time.Now()
94104 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
95105 bar.Increment()
96106 }
97107 }()
98108 }
99 // first wait for provided wg, then
100109 // wait for all bars to complete and flush
101110 p.Wait()
102111 ```