Codebase list golang-github-vbauerster-mpb / 7754c4d
Package Example update Vladimir Bauer 9 years ago
4 changed file(s) with 43 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
2222 Following is the simplest use case:
2323
2424 ```go
25 name := "Single bar:"
2625 // Star mpb's rendering goroutine.
2726 // If you don't plan to cancel, feed with nil
2827 // otherwise provide context.Context, see cancel example
2928 p := mpb.New(nil)
30 // Set custom format, the default one is "[=>-]"
29 // Set custom width for every bar, which mpb will contain
30 // The default one in 70
31 p.SetWidth(80)
32 // Set custom format for every bar, the default one is "[=>-]"
3133 p.Format("╢▌▌░╟")
34 // Set custom refresh rate, the default one is 100 ms
35 p.RefreshRate(120 * time.Millisecond)
3236
33 bar := p.AddBar(100).PrependName(name, 0).AppendPercentage()
37 // Add a bar. You're not limited to just one bar, add many if you need.
38 bar := p.AddBar(100).PrependName("Single Bar:", 0).AppendPercentage()
3439
3540 for i := 0; i < 100; i++ {
3641 time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
3742 bar.Incr(1)
3843 }
3944
45 // Don't forget to stop mpb's rendering goroutine
4046 p.Stop()
47
48 // You cannot add bars after p.Stop() has been called
49 // p.AddBar(100) // will panic
4150 ```
4251
4352 Running [this](example/singleBar/main.go), will produce:
00 package main
11
22 import (
3 "fmt"
43 "math/rand"
54 "time"
65
87 )
98
109 func main() {
11
12 name := "Single bar:"
1310 // Star mpb's rendering goroutine.
1411 // If you don't plan to cancel, feed with nil
1512 // otherwise provide context.Context, see cancel example
1613 p := mpb.New(nil)
17 // Set custom format, the default one is "[=>-]"
14 // Set custom width for every bar, which mpb will contain
15 // The default one in 70
16 p.SetWidth(80)
17 // Set custom format for every bar, the default one is "[=>-]"
1818 p.Format("╢▌▌░╟")
19 // Set custom refresh rate, the default one is 100 ms
20 p.RefreshRate(120 * time.Millisecond)
1921
20 bar := p.AddBar(100).PrependName(name, 0).AppendPercentage()
22 // Add a bar. You're not limited to just one bar, add many if you need.
23 bar := p.AddBar(100).PrependName("Single Bar:", 0).AppendPercentage()
2124
2225 for i := 0; i < 100; i++ {
2326 time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
2427 bar.Incr(1)
2528 }
2629
30 // Don't forget to stop mpb's rendering goroutine
2731 p.Stop()
28 fmt.Println("stop")
32
33 // You cannot add bars after p.Stop() has been called
34 // p.AddBar(100) // will panic
2935 }
1111 // If you don't plan to cancel, feed with nil
1212 // otherwise provide context.Context, see cancel example
1313 p := mpb.New(nil)
14 // Set custom format, the default one is "[=>-]"
14 // Set custom width for every bar, which mpb will contain
15 // The default one in 70
16 p.SetWidth(80)
17 // Set custom format for every bar, the default one is "[=>-]"
1518 p.Format("╢▌▌░╟")
19 // Set custom refresh rate, the default one is 100 ms
20 p.RefreshRate(120 * time.Millisecond)
1621
22 // Add a bar. You're not limited to just one bar, add many if you need.
1723 bar := p.AddBar(100).PrependName("Single Bar:", 0).AppendPercentage()
1824
1925 for i := 0; i < 100; i++ {
2329
2430 // Don't forget to stop mpb's rendering goroutine
2531 p.Stop()
32
33 // You cannot add bars after p.Stop() has been called
34 // p.AddBar(100) // will panic
2635 }
1414
1515 var logger = log.New(os.Stderr, "mpb: ", log.LstdFlags|log.Lshortfile)
1616
17 // ErrCallAfterStop thrown by panic, if Progress methods like AddBar() are called
18 // after Stop() has been called
17 // ErrCallAfterStop thrown by panic, if Progress methods like (*Progress).AddBar()
18 // are called after (*Progress).Stop() has been called
1919 var ErrCallAfterStop = errors.New("method call on stopped Progress instance")
2020
2121 type (
106106 }
107107
108108 // SetOut sets underlying writer of progress. Default is os.Stdout
109 // pancis, if called on stopped Progress instance, i.e after Stop()
109 // pancis, if called on stopped Progress instance, i.e after (*Progress).Stop()
110110 func (p *Progress) SetOut(w io.Writer) *Progress {
111111 if isClosed(p.done) {
112112 panic(ErrCallAfterStop)
119119 }
120120
121121 // RefreshRate overrides default (100ms) refresh rate value
122 // pancis, if called on stopped Progress instance, i.e after Stop()
122 // pancis, if called on stopped Progress instance, i.e after (*Progress).Stop()
123123 func (p *Progress) RefreshRate(d time.Duration) *Progress {
124124 if isClosed(p.done) {
125125 panic(ErrCallAfterStop)
138138 }
139139
140140 // AddBar creates a new progress bar and adds to the container
141 // pancis, if called on stopped Progress instance, i.e after Stop()
141 // pancis, if called on stopped Progress instance, i.e after (*Progress).Stop()
142142 func (p *Progress) AddBar(total int64) *Bar {
143143 return p.AddBarWithID(0, total)
144144 }
145145
146146 // AddBarWithID creates a new progress bar and adds to the container
147 // pancis, if called on stopped Progress instance, i.e after Stop()
147 // pancis, if called on stopped Progress instance, i.e after (*Progress).Stop()
148148 func (p *Progress) AddBarWithID(id int, total int64) *Bar {
149149 if isClosed(p.done) {
150150 panic(ErrCallAfterStop)
158158 return bar
159159 }
160160
161 // RemoveBar removes bar at any time
162 // pancis, if called on stopped Progress instance, i.e after Stop()
161 // RemoveBar removes bar at any time.
162 // Pancis, if called on stopped Progress instance, i.e after (*Progress).Stop()
163163 func (p *Progress) RemoveBar(b *Bar) bool {
164164 if isClosed(p.done) {
165165 panic(ErrCallAfterStop)
170170 }
171171
172172 // BarCount returns bars count in the container.
173 // Pancis if called on stopped Progress instance, i.e after Stop()
173 // Pancis if called on stopped Progress instance, i.e after (*Progress).Stop()
174174 func (p *Progress) BarCount() int {
175175 if isClosed(p.done) {
176176 panic(ErrCallAfterStop)