diff --git a/README.md b/README.md index b711b11..e42de07 100644 --- a/README.md +++ b/README.md @@ -23,22 +23,31 @@ Following is the simplest use case: ```go - name := "Single bar:" // Star mpb's rendering goroutine. // If you don't plan to cancel, feed with nil // otherwise provide context.Context, see cancel example p := mpb.New(nil) - // Set custom format, the default one is "[=>-]" + // Set custom width for every bar, which mpb will contain + // The default one in 70 + p.SetWidth(80) + // Set custom format for every bar, the default one is "[=>-]" p.Format("╢▌▌░╟") + // Set custom refresh rate, the default one is 100 ms + p.RefreshRate(120 * time.Millisecond) - bar := p.AddBar(100).PrependName(name, 0).AppendPercentage() + // Add a bar. You're not limited to just one bar, add many if you need. + bar := p.AddBar(100).PrependName("Single Bar:", 0).AppendPercentage() for i := 0; i < 100; i++ { time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond) bar.Incr(1) } + // Don't forget to stop mpb's rendering goroutine p.Stop() + + // You cannot add bars after p.Stop() has been called + // p.AddBar(100) // will panic ``` Running [this](example/singleBar/main.go), will produce: diff --git a/example/singleBar/main.go b/example/singleBar/main.go index a176886..0d2878f 100644 --- a/example/singleBar/main.go +++ b/example/singleBar/main.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "math/rand" "time" @@ -9,22 +8,29 @@ ) func main() { - - name := "Single bar:" // Star mpb's rendering goroutine. // If you don't plan to cancel, feed with nil // otherwise provide context.Context, see cancel example p := mpb.New(nil) - // Set custom format, the default one is "[=>-]" + // Set custom width for every bar, which mpb will contain + // The default one in 70 + p.SetWidth(80) + // Set custom format for every bar, the default one is "[=>-]" p.Format("╢▌▌░╟") + // Set custom refresh rate, the default one is 100 ms + p.RefreshRate(120 * time.Millisecond) - bar := p.AddBar(100).PrependName(name, 0).AppendPercentage() + // Add a bar. You're not limited to just one bar, add many if you need. + bar := p.AddBar(100).PrependName("Single Bar:", 0).AppendPercentage() for i := 0; i < 100; i++ { time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond) bar.Incr(1) } + // Don't forget to stop mpb's rendering goroutine p.Stop() - fmt.Println("stop") + + // You cannot add bars after p.Stop() has been called + // p.AddBar(100) // will panic } diff --git a/example_test.go b/example_test.go index dff4594..665bf89 100644 --- a/example_test.go +++ b/example_test.go @@ -12,9 +12,15 @@ // If you don't plan to cancel, feed with nil // otherwise provide context.Context, see cancel example p := mpb.New(nil) - // Set custom format, the default one is "[=>-]" + // Set custom width for every bar, which mpb will contain + // The default one in 70 + p.SetWidth(80) + // Set custom format for every bar, the default one is "[=>-]" p.Format("╢▌▌░╟") + // Set custom refresh rate, the default one is 100 ms + p.RefreshRate(120 * time.Millisecond) + // Add a bar. You're not limited to just one bar, add many if you need. bar := p.AddBar(100).PrependName("Single Bar:", 0).AppendPercentage() for i := 0; i < 100; i++ { @@ -24,4 +30,7 @@ // Don't forget to stop mpb's rendering goroutine p.Stop() + + // You cannot add bars after p.Stop() has been called + // p.AddBar(100) // will panic } diff --git a/progress.go b/progress.go index 06a861d..abda847 100644 --- a/progress.go +++ b/progress.go @@ -15,8 +15,8 @@ var logger = log.New(os.Stderr, "mpb: ", log.LstdFlags|log.Lshortfile) -// ErrCallAfterStop thrown by panic, if Progress methods like AddBar() are called -// after Stop() has been called +// ErrCallAfterStop thrown by panic, if Progress methods like (*Progress).AddBar() +// are called after (*Progress).Stop() has been called var ErrCallAfterStop = errors.New("method call on stopped Progress instance") type ( @@ -107,7 +107,7 @@ } // SetOut sets underlying writer of progress. Default is os.Stdout -// pancis, if called on stopped Progress instance, i.e after Stop() +// pancis, if called on stopped Progress instance, i.e after (*Progress).Stop() func (p *Progress) SetOut(w io.Writer) *Progress { if isClosed(p.done) { panic(ErrCallAfterStop) @@ -120,7 +120,7 @@ } // RefreshRate overrides default (100ms) refresh rate value -// pancis, if called on stopped Progress instance, i.e after Stop() +// pancis, if called on stopped Progress instance, i.e after (*Progress).Stop() func (p *Progress) RefreshRate(d time.Duration) *Progress { if isClosed(p.done) { panic(ErrCallAfterStop) @@ -139,13 +139,13 @@ } // AddBar creates a new progress bar and adds to the container -// pancis, if called on stopped Progress instance, i.e after Stop() +// pancis, if called on stopped Progress instance, i.e after (*Progress).Stop() func (p *Progress) AddBar(total int64) *Bar { return p.AddBarWithID(0, total) } // AddBarWithID creates a new progress bar and adds to the container -// pancis, if called on stopped Progress instance, i.e after Stop() +// pancis, if called on stopped Progress instance, i.e after (*Progress).Stop() func (p *Progress) AddBarWithID(id int, total int64) *Bar { if isClosed(p.done) { panic(ErrCallAfterStop) @@ -159,8 +159,8 @@ return bar } -// RemoveBar removes bar at any time -// pancis, if called on stopped Progress instance, i.e after Stop() +// RemoveBar removes bar at any time. +// Pancis, if called on stopped Progress instance, i.e after (*Progress).Stop() func (p *Progress) RemoveBar(b *Bar) bool { if isClosed(p.done) { panic(ErrCallAfterStop) @@ -171,7 +171,7 @@ } // BarCount returns bars count in the container. -// Pancis if called on stopped Progress instance, i.e after Stop() +// Pancis if called on stopped Progress instance, i.e after (*Progress).Stop() func (p *Progress) BarCount() int { if isClosed(p.done) { panic(ErrCallAfterStop)