Codebase list golang-github-vbauerster-mpb / 732f0c8 examples / gifs / io-multiple.gif
README and examples Vladimir Bauer 9 years ago
4 changed file(s) with 89 addition(s) and 69 deletion(s). Raw diff Collapse all Expand all
2828 To get the package, execute:
2929
3030 ```sh
31 go get gopkg.in/vbauerster/mpb.v2
31 go get gopkg.in/vbauerster/mpb.v3
3232 ```
3333
3434 ## Usage
3636 Following is the simplest use case:
3737
3838 ```go
39 // Star mpb's rendering goroutine.
40 p := mpb.New()
41 // Set custom width for every bar, which mpb will render
42 // The default one is 80
43 p.SetWidth(100)
44 // Set custom format for every bar, the default one is "[=>-]"
45 p.Format("╢▌▌░╟")
46 // Set custom refresh rate, the default one is 100 ms
47 p.RefreshRate(120 * time.Millisecond)
39 p := mpb.New(
40 // override default (80) width
41 mpb.WithWidth(100),
42 // override default "[=>-]" format
43 mpb.WithFormat("╢▌▌░╟"),
44 // override default 100ms refresh rate
45 mpb.WithRefreshRate(120*time.Millisecond),
46 )
4847
48 total := 100
49 name := "Single Bar:"
4950 // Add a bar. You're not limited to just one bar, add many if you need.
50 bar := p.AddBar(100).PrependName("Single Bar:", 0, 0).AppendPercentage(5, 0)
51 bar := p.AddBar(int64(total),
52 // Prepending decorators
53 mpb.PrependDecorators(
54 // Name decorator with minWidth and no width sync options
55 decor.Name(name, len(name), 0),
56 // ETA decorator with minWidth and width sync options DwidthSync|DextraSpace
57 decor.ETA(4, decor.DSyncSpace),
58 ),
59 // Appending decorators
60 mpb.AppendDecorators(
61 // Percentage decorator with minWidth and no width sync options
62 decor.Percentage(5, 0),
63 ),
64 )
5165
52 for i := 0; i < 100; i++ {
66 for i := 0; i < total; i++ {
5367 time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
5468 bar.Incr(1) // increment progress bar
5569 }
5670
57 // Don't forget to stop mpb's rendering goroutine
5871 p.Stop()
5972 ```
6073
61 Running [this](example/singleBar/main.go), will produce:
74 Running [this](examples/singleBar/main.go), will produce:
6275
63 ![gif](example/gifs/single.gif)
76 ![gif](examples/gifs/single.gif)
6477
6578 However **mpb** was designed with concurrency in mind. Each new bar renders in its
6679 own goroutine, therefore adding multiple bars is easy and safe:
6780
6881 ```go
82 p := mpb.New()
83 total := 100
84 numBars := 3
6985 var wg sync.WaitGroup
70 p := mpb.New()
71 wg.Add(3) // add wg delta
72 for i := 0; i < 3; i++ {
86 wg.Add(numBars)
87
88 for i := 0; i < numBars; i++ {
7389 name := fmt.Sprintf("Bar#%d:", i)
74 bar := p.AddBar(100).
75 PrependName(name, len(name), 0).
76 // Prepend Percentage decorator and sync width
77 PrependPercentage(3, mpb.DwidthSync|mpb.DextraSpace).
78 // Append ETA and don't sync width
79 AppendETA(2, 0)
90 bar := p.AddBar(int64(total),
91 mpb.PrependDecorators(
92 decor.Name(name, len(name), 0),
93 decor.Percentage(3, decor.DSyncSpace),
94 ),
95 mpb.AppendDecorators(
96 decor.ETA(2, 0),
97 ),
98 )
8099 go func() {
81100 defer wg.Done()
82 // you can p.AddBar() here, but ordering will be non deterministic
83 // if you still need p.AddBar() here and maintain ordering, use
84 // (*mpb.Progress).BeforeRenderFunc(f mpb.BeforeRender)
85 for i := 0; i < 100; i++ {
101 for i := 0; i < total; i++ {
86102 time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
87103 bar.Incr(1)
88104 }
92108 p.Stop() // Stop mpb's rendering goroutine
93109 ```
94110
95 ![simple.gif](example/gifs/simple.gif)
111 ![simple.gif](examples/gifs/simple.gif)
96112
97 The source code: [example/simple/main.go](example/simple/main.go)
113 The source code: [examples/simple/main.go](examples/simple/main.go)
98114
99115 ### Cancel
100116
101 To cancel use either
102 [WithCancel](https://godoc.org/github.com/vbauerster/mpb#Progress.WithCancel) or
103 [WithContext](https://godoc.org/github.com/vbauerster/mpb#Progress.WithContext)
104 method. The last one requires Go 1.7
117 ![cancel.gif](examples/gifs/cancel.gif)
105118
106 ![cancel.gif](example/gifs/cancel.gif)
107
108 The source code: [example/cancel/main.go](example/cancel/main.go)
119 The source code: [examples/cancel/main.go](examples/cancel/main.go)
109120
110121 ### Removing bar
111122
112 ![remove.gif](example/gifs/remove.gif)
123 ![remove.gif](examples/gifs/remove.gif)
113124
114 The source code: [example/remove/main.go](example/remove/main.go)
125 The source code: [examples/remove/main.go](examples/remove/main.go)
115126
116127 ### Sorting bars by progress
117128
118 ![sort.gif](example/gifs/sort.gif)
129 ![sort.gif](examples/gifs/sort.gif)
119130
120 The source code: [example/sort/main.go](example/sort/main.go)
131 The source code: [examples/sort/main.go](examples/sort/main.go)
121132
122133 ### Resizing bars on terminal width change
123134
124 ![resize.gif](example/gifs/resize.gif)
135 ![resize.gif](examples/gifs/resize.gif)
125136
126 The source code: [example/prependETA/main.go](example/prependETA/main.go)
137 The source code: [examples/prependETA/main.go](examples/prependETA/main.go)
127138
128139 ### Multiple io
129140
130 ![io-multiple.gif](example/gifs/io-multiple.gif)
141 ![io-multiple.gif](examples/gifs/io-multiple.gif)
131142
132 The source code: [example/io/multiple/main.go](example/io/multiple/main.go)
133
134 ### Custom Decorators
135
136 Refer to godoc [example](https://godoc.org/github.com/vbauerster/mpb#example-Bar-PrependFunc).
143 The source code: [examples/io/multiple/main.go](examples/io/multiple/main.go)
137144
138145 ## License
139146
88 )
99
1010 func Example() {
11 // Star mpb's rendering goroutine.
1211 p := mpb.New(
1312 // override default (80) width
1413 mpb.WithWidth(100),
1918 )
2019
2120 total := 100
22 barName := "Single Bar:"
21 name := "Single Bar:"
2322 // Add a bar. You're not limited to just one bar, add many if you need.
2423 bar := p.AddBar(int64(total),
24 // Prepending decorators
2525 mpb.PrependDecorators(
26 decor.Name(barName, 0, decor.DwidthSync|decor.DidentRight),
26 // Name decorator with minWidth and no width sync options
27 decor.Name(name, len(name), 0),
28 // ETA decorator with minWidth and width sync options DwidthSync|DextraSpace
2729 decor.ETA(4, decor.DSyncSpace),
2830 ),
29 mpb.AppendDecorators(decor.Percentage(5, 0)),
31 // Appending decorators
32 mpb.AppendDecorators(
33 // Percentage decorator with minWidth and no width sync options
34 decor.Percentage(5, 0),
35 ),
3036 )
3137
32 for i := 0; i < 100; i++ {
38 for i := 0; i < total; i++ {
3339 time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
3440 bar.Incr(1) // increment progress bar
3541 }
3642
37 // Don't forget to stop mpb's rendering goroutine
3843 p.Stop()
3944 }
4045
2020 name := fmt.Sprintf("Bar#%d:", i)
2121 bar := p.AddBar(int64(total),
2222 mpb.PrependDecorators(
23 // Name decorator with minWidth and no width sync
23 // Name decorator with minWidth and no width sync options
2424 decor.Name(name, len(name), 0),
25 // Percentage decorator with DwidthSync and DextraSpace
25 // Percentage decorator with minWidth and width sync options DwidthSync|DextraSpace
2626 decor.Percentage(3, decor.DSyncSpace),
2727 ),
2828 mpb.AppendDecorators(
3030 decor.ETA(2, 0),
3131 ),
3232 )
33
3433 go func() {
3534 defer wg.Done()
3635 for i := 0; i < total; i++ {
00 package main
11
22 import (
3 "fmt"
43 "math/rand"
54 "time"
65
109
1110 func main() {
1211 p := mpb.New(
13 // Override default (80) width
12 // override default (80) width
1413 mpb.WithWidth(100),
15 // Override default "[=>-]" format
14 // override default "[=>-]" format
1615 mpb.WithFormat("╢▌▌░╟"),
17 // Override default 100ms refresh rate
16 // override default 100ms refresh rate
1817 mpb.WithRefreshRate(120*time.Millisecond),
1918 )
2019
20 total := 100
21 name := "Single Bar:"
2122 // Add a bar. You're not limited to just one bar, add many if you need.
22 bar := p.AddBar(100,
23 mpb.PrependDecorators(decor.Name("Single Bar:", 0, 0)),
24 mpb.AppendDecorators(decor.Percentage(5, 0)),
23 bar := p.AddBar(int64(total),
24 // Prepending decorators
25 mpb.PrependDecorators(
26 // Name decorator with minWidth and no width sync options
27 decor.Name(name, len(name), 0),
28 // ETA decorator with minWidth and width sync options DwidthSync|DextraSpace
29 decor.ETA(4, decor.DSyncSpace),
30 ),
31 // Appending decorators
32 mpb.AppendDecorators(
33 // Percentage decorator with minWidth and no width sync options
34 decor.Percentage(5, 0),
35 ),
2536 )
2637
27 for i := 0; i < 100; i++ {
38 for i := 0; i < total; i++ {
39 time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
2840 bar.Incr(1) // increment progress bar
29 time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
3041 }
3142
32 // Don't forget to stop mpb's rendering goroutine
3343 p.Stop()
34 fmt.Println("Stop")
3544 }