Package Example update
Vladimir Bauer
9 years ago
| 22 | 22 | Following is the simplest use case: |
| 23 | 23 | |
| 24 | 24 | ```go |
| 25 | name := "Single bar:" | |
| 26 | 25 | // Star mpb's rendering goroutine. |
| 27 | 26 | // If you don't plan to cancel, feed with nil |
| 28 | 27 | // otherwise provide context.Context, see cancel example |
| 29 | 28 | 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 "[=>-]" | |
| 31 | 33 | p.Format("╢▌▌░╟") |
| 34 | // Set custom refresh rate, the default one is 100 ms | |
| 35 | p.RefreshRate(120 * time.Millisecond) | |
| 32 | 36 | |
| 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() | |
| 34 | 39 | |
| 35 | 40 | for i := 0; i < 100; i++ { |
| 36 | 41 | time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond) |
| 37 | 42 | bar.Incr(1) |
| 38 | 43 | } |
| 39 | 44 | |
| 45 | // Don't forget to stop mpb's rendering goroutine | |
| 40 | 46 | p.Stop() |
| 47 | ||
| 48 | // You cannot add bars after p.Stop() has been called | |
| 49 | // p.AddBar(100) // will panic | |
| 41 | 50 | ``` |
| 42 | 51 | |
| 43 | 52 | Running [this](example/singleBar/main.go), will produce: |
| 0 | 0 | package main |
| 1 | 1 | |
| 2 | 2 | import ( |
| 3 | "fmt" | |
| 4 | 3 | "math/rand" |
| 5 | 4 | "time" |
| 6 | 5 | |
| 8 | 7 | ) |
| 9 | 8 | |
| 10 | 9 | func main() { |
| 11 | ||
| 12 | name := "Single bar:" | |
| 13 | 10 | // Star mpb's rendering goroutine. |
| 14 | 11 | // If you don't plan to cancel, feed with nil |
| 15 | 12 | // otherwise provide context.Context, see cancel example |
| 16 | 13 | 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 "[=>-]" | |
| 18 | 18 | p.Format("╢▌▌░╟") |
| 19 | // Set custom refresh rate, the default one is 100 ms | |
| 20 | p.RefreshRate(120 * time.Millisecond) | |
| 19 | 21 | |
| 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() | |
| 21 | 24 | |
| 22 | 25 | for i := 0; i < 100; i++ { |
| 23 | 26 | time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond) |
| 24 | 27 | bar.Incr(1) |
| 25 | 28 | } |
| 26 | 29 | |
| 30 | // Don't forget to stop mpb's rendering goroutine | |
| 27 | 31 | p.Stop() |
| 28 | fmt.Println("stop") | |
| 32 | ||
| 33 | // You cannot add bars after p.Stop() has been called | |
| 34 | // p.AddBar(100) // will panic | |
| 29 | 35 | } |
| 11 | 11 | // If you don't plan to cancel, feed with nil |
| 12 | 12 | // otherwise provide context.Context, see cancel example |
| 13 | 13 | 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 "[=>-]" | |
| 15 | 18 | p.Format("╢▌▌░╟") |
| 19 | // Set custom refresh rate, the default one is 100 ms | |
| 20 | p.RefreshRate(120 * time.Millisecond) | |
| 16 | 21 | |
| 22 | // Add a bar. You're not limited to just one bar, add many if you need. | |
| 17 | 23 | bar := p.AddBar(100).PrependName("Single Bar:", 0).AppendPercentage() |
| 18 | 24 | |
| 19 | 25 | for i := 0; i < 100; i++ { |
| 23 | 29 | |
| 24 | 30 | // Don't forget to stop mpb's rendering goroutine |
| 25 | 31 | p.Stop() |
| 32 | ||
| 33 | // You cannot add bars after p.Stop() has been called | |
| 34 | // p.AddBar(100) // will panic | |
| 26 | 35 | } |
| 14 | 14 | |
| 15 | 15 | var logger = log.New(os.Stderr, "mpb: ", log.LstdFlags|log.Lshortfile) |
| 16 | 16 | |
| 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 | |
| 19 | 19 | var ErrCallAfterStop = errors.New("method call on stopped Progress instance") |
| 20 | 20 | |
| 21 | 21 | type ( |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | // 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() | |
| 110 | 110 | func (p *Progress) SetOut(w io.Writer) *Progress { |
| 111 | 111 | if isClosed(p.done) { |
| 112 | 112 | panic(ErrCallAfterStop) |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | 121 | // 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() | |
| 123 | 123 | func (p *Progress) RefreshRate(d time.Duration) *Progress { |
| 124 | 124 | if isClosed(p.done) { |
| 125 | 125 | panic(ErrCallAfterStop) |
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | // 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() | |
| 142 | 142 | func (p *Progress) AddBar(total int64) *Bar { |
| 143 | 143 | return p.AddBarWithID(0, total) |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | // 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() | |
| 148 | 148 | func (p *Progress) AddBarWithID(id int, total int64) *Bar { |
| 149 | 149 | if isClosed(p.done) { |
| 150 | 150 | panic(ErrCallAfterStop) |
| 158 | 158 | return bar |
| 159 | 159 | } |
| 160 | 160 | |
| 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() | |
| 163 | 163 | func (p *Progress) RemoveBar(b *Bar) bool { |
| 164 | 164 | if isClosed(p.done) { |
| 165 | 165 | panic(ErrCallAfterStop) |
| 170 | 170 | } |
| 171 | 171 | |
| 172 | 172 | // 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() | |
| 174 | 174 | func (p *Progress) BarCount() int { |
| 175 | 175 | if isClosed(p.done) { |
| 176 | 176 | panic(ErrCallAfterStop) |