Codebase list golang-github-vbauerster-mpb / 2575fa5 example / gifs / io-multiple.gif
readme update Vladimir Bauer 9 years ago
3 changed file(s) with 28 addition(s) and 14 deletion(s). Raw diff Collapse all Expand all
1212 * __Dynamic Addition__: Add additional progress bar at any time
1313 * __Dynamic Removal__: Remove rendering progress bar at any time
1414 * __Dynamic Sorting__: Sort bars by progression
15 * __Dynamic Resize: Resize bars on terminal width change
1516 * __Custom Decorator Functions__: Add custom functions around the bar along with helper functions
1617 * __Predefined Decoratros__: Elapsed time, [Ewmaest](https://github.com/dgryski/trifles/tree/master/ewmaest) based ETA, Percentage, Bytes counter
1718
1819 ## Usage
1920
2021 Following is the simplest use case:
22
23 ```go
24 name := "Single bar:"
25 p := mpb.New()
26 bar := p.AddBar(100).PrependName(name, 0).AppendPercentage()
27
28 for i := 0; i < 100; i++ {
29 time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
30 bar.Incr(1)
31 }
32
33 p.Stop()
34 ```
35 The source code: [example/singleBar/main.go](example/singleBar/main.go)
36
37 However **mpb** was designed with concurrency in mind, each new bar renders in its
38 own goroutine. Therefore adding multiple bars is easy and safe:
2139
2240 ```go
2341 var wg sync.WaitGroup
5977
6078 The source code: [example/sort/main.go](example/sort/main.go)
6179
80 ### Resizing bars on terminal width change
81
82 ![example](example/gifs/resize.gif)
83
84 The source code: [example/prependETA/main.go](example/prependETA/main.go)
85
6286 ### Multiple io
6387
6488 ![example](example/gifs/io-multiple.gif)
Binary diff not shown
77 "github.com/vbauerster/mpb"
88 )
99
10 const (
11 totalItem = 100
12 maxBlockSize = 10
13 )
14
1510 func main() {
1611
17 name := "Single:"
12 name := "Single bar:"
1813 p := mpb.New()
19 bar := p.AddBar(totalItem).
20 PrependName(name, 0).
21 AppendPercentage().
22 TrimRightSpace()
14 bar := p.AddBar(100).PrependName(name, 0).AppendPercentage()
2315
24 blockSize := rand.Intn(maxBlockSize) + 1
25 for i := 0; i < totalItem; i++ {
26 time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond)))))
16 for i := 0; i < 100; i++ {
17 time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
2718 bar.Incr(1)
28 blockSize = rand.Intn(maxBlockSize) + 1
2919 }
3020
3121 p.Stop()