Documentation
¶
Overview ¶
Package mpb is a library for rendering progress bars in terminal applications.
Example ¶
package main
import (
"math/rand"
"time"
"github.com/vbauerster/mpb"
)
func main() {
// 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 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++ {
bar.Incr(1) // increment progress bar
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
}
// 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
}
Index ¶
- Constants
- Variables
- func Format(i int64) *formatter
- type Bar
- func (b *Bar) AppendETA(padding int) *Bar
- func (b *Bar) AppendElapsed() *Bar
- func (b *Bar) AppendFunc(f DecoratorFunc) *Bar
- func (b *Bar) AppendPercentage() *Bar
- func (b *Bar) Completed()
- func (b *Bar) Format(format string) *Bar
- func (b *Bar) GetAppenders() []DecoratorFunc
- func (b *Bar) GetID() int
- func (b *Bar) GetPrependers() []DecoratorFunc
- func (b *Bar) GetStatistics() *Statistics
- func (b *Bar) InProgress() bool
- func (b *Bar) Incr(n int)
- func (b *Bar) IncrWithReFill(n int, r rune)
- func (b *Bar) PrependCounters(unit Units, padding int) *Bar
- func (b *Bar) PrependETA(padding int) *Bar
- func (b *Bar) PrependElapsed(padding int) *Bar
- func (b *Bar) PrependFunc(f DecoratorFunc) *Bar
- func (b *Bar) PrependName(name string, padding int) *Bar
- func (b *Bar) PrependPercentage(padding int) *Bar
- func (b *Bar) ProxyReader(r io.Reader) *Reader
- func (b *Bar) RemoveAllAppenders()
- func (b *Bar) RemoveAllPrependers()
- func (b *Bar) SetEtaAlpha(a float64) *Bar
- func (b *Bar) SetWidth(n int) *Bar
- func (b *Bar) TrimLeftSpace() *Bar
- func (b *Bar) TrimRightSpace() *Bar
- type BeforeRender
- type DecoratorFunc
- type Progress
- func (p *Progress) AddBar(total int64) *Bar
- func (p *Progress) AddBarWithID(id int, total int64) *Bar
- func (p *Progress) BarCount() int
- func (p *Progress) BeforeRenderFunc(f BeforeRender) *Progress
- func (p *Progress) Format(format string) *Progress
- func (p *Progress) RefreshRate(d time.Duration) *Progress
- func (p *Progress) RemoveBar(b *Bar) bool
- func (p *Progress) SetOut(w io.Writer) *Progress
- func (p *Progress) SetWidth(n int) *Progress
- func (p *Progress) Stop()
- type Reader
- type Statistics
- type Units
Examples ¶
Constants ¶
const (
UnitBytes
)
Variables ¶
var ErrCallAfterStop = errors.New("method call on stopped Progress instance")
ErrCallAfterStop thrown by panic, if Progress methods like (*Progress).AddBar() are called after (*Progress).Stop() has been called
Functions ¶
Types ¶
type Bar ¶
type Bar struct {
// contains filtered or unexported fields
}
Bar represents a progress Bar
func (*Bar) AppendElapsed ¶
func (*Bar) AppendFunc ¶
func (b *Bar) AppendFunc(f DecoratorFunc) *Bar
AppendFunc appends DecoratorFunc
Example ¶
package main
import (
"fmt"
"math/rand"
"time"
"github.com/vbauerster/mpb"
)
func main() {
decor := func(s *mpb.Statistics) string {
str := fmt.Sprintf("%d/%d", s.Current, s.Total)
return fmt.Sprintf("%8s", str)
}
totalItem := 100
p := mpb.New(nil)
bar := p.AddBar(int64(totalItem)).AppendFunc(decor)
for i := 0; i < totalItem; i++ {
bar.Incr(1) // increment progress bar
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
}
}
func (*Bar) AppendPercentage ¶
func (*Bar) Completed ¶
func (b *Bar) Completed()
Completed signals to the bar, that process has been completed. You should call this method when total is unknown and you've reached the point of process completion.
func (*Bar) GetAppenders ¶
func (b *Bar) GetAppenders() []DecoratorFunc
GetAppenders returns slice of appender DecoratorFunc
func (*Bar) GetPrependers ¶
func (b *Bar) GetPrependers() []DecoratorFunc
GetPrependers returns slice of prepender DecoratorFunc
func (*Bar) GetStatistics ¶
func (b *Bar) GetStatistics() *Statistics
GetStatistics returs *Statistics, which contains information like Tottal, Current, TimeElapsed and TimePerItemEstimate
func (*Bar) InProgress ¶
InProgress returns true, while progress is running Can be used as condition in for loop
Example ¶
package main
import (
"time"
"github.com/vbauerster/mpb"
)
func main() {
p := mpb.New(nil)
bar := p.AddBar(100).AppendPercentage()
for bar.InProgress() {
bar.Incr(1)
time.Sleep(time.Millisecond * 20)
}
}
func (*Bar) IncrWithReFill ¶
IncrWithReFill increments pb with different fill character
func (*Bar) PrependETA ¶
func (*Bar) PrependElapsed ¶
func (*Bar) PrependFunc ¶
func (b *Bar) PrependFunc(f DecoratorFunc) *Bar
PrependFunc prepends DecoratorFunc
Example ¶
package main
import (
"fmt"
"math/rand"
"time"
"github.com/vbauerster/mpb"
)
func main() {
decor := func(s *mpb.Statistics) string {
str := fmt.Sprintf("%d/%d", s.Current, s.Total)
return fmt.Sprintf("%8s", str)
}
totalItem := 100
p := mpb.New(nil)
bar := p.AddBar(int64(totalItem)).PrependFunc(decor)
for i := 0; i < totalItem; i++ {
bar.Incr(1) // increment progress bar
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
}
}
func (*Bar) PrependPercentage ¶
func (*Bar) ProxyReader ¶
ProxyReader wrapper for io operations, like io.Copy
func (*Bar) RemoveAllAppenders ¶
func (b *Bar) RemoveAllAppenders()
RemoveAllAppenders removes all append functions
func (*Bar) RemoveAllPrependers ¶
func (b *Bar) RemoveAllPrependers()
RemoveAllPrependers removes all prepend functions
func (*Bar) SetEtaAlpha ¶
SetEtaAlpha sets alfa for exponential-weighted-moving-average ETA estimator Defaults to 0.25 Normally you shouldn't touch this
func (*Bar) TrimLeftSpace ¶
TrimLeftSpace removes space befor LeftEnd charater
func (*Bar) TrimRightSpace ¶
TrimRightSpace removes space after RightEnd charater
type BeforeRender ¶
type BeforeRender func([]*Bar)
BeforeRender is a func, which gets called before render process
type DecoratorFunc ¶
type DecoratorFunc func(s *Statistics) string
DecoratorFunc is a function that can be prepended and appended to the progress bar
type Progress ¶
type Progress struct {
// contains filtered or unexported fields
}
Progress represents the container that renders Progress bars
func New ¶
New creates new Progress instance, which will orchestrate bars rendering process. It acceepts context.Context, for cancellation. If you don't plan to cancel, it is safe to feed with nil
func (*Progress) AddBar ¶
AddBar creates a new progress bar and adds to the container pancis, if called on stopped Progress instance, i.e after (*Progress).Stop()
func (*Progress) AddBarWithID ¶
AddBarWithID creates a new progress bar and adds to the container pancis, if called on stopped Progress instance, i.e after (*Progress).Stop()
func (*Progress) BarCount ¶
BarCount returns bars count in the container. Pancis if called on stopped Progress instance, i.e after (*Progress).Stop()
func (*Progress) BeforeRenderFunc ¶
func (p *Progress) BeforeRenderFunc(f BeforeRender) *Progress
BeforeRenderFunc accepts a func, which gets called before render process.
func (*Progress) Format ¶
Format sets custom format for underlying bar(s). The default one is "[=>-]"
func (*Progress) RefreshRate ¶
RefreshRate overrides default (100ms) refresh rate value pancis, if called on stopped Progress instance, i.e after (*Progress).Stop()
func (*Progress) RemoveBar ¶
RemoveBar removes bar at any time. Pancis, if called on stopped Progress instance, i.e after (*Progress).Stop()
func (*Progress) SetOut ¶
SetOut sets underlying writer of progress. Default is os.Stdout pancis, if called on stopped Progress instance, i.e after (*Progress).Stop()
type Statistics ¶
Statistics represents statistics of the progress bar. Cantains: Total, Current, TimeElapsed, TimePerItemEstimate
func (*Statistics) Eta ¶
func (s *Statistics) Eta() time.Duration
Eta returns exponential-weighted-moving-average ETA estimator
Directories
¶
| Path | Synopsis |
|---|---|
|
_examples
|
|
|
barExtender
module
|
|
|
barExtenderRev
module
|
|
|
cancel
module
|
|
|
complex
module
|
|
|
dynTotal
module
|
|
|
io
module
|
|
|
merge
module
|
|
|
mexicanBar
module
|
|
|
multiBars
module
|
|
|
panic
module
|
|
|
poplog
module
|
|
|
remove
module
|
|
|
spinTipBar
module
|
|
|
spinnerBar
module
|
|
|
spinnerDecorator
module
|
|
|
stress
module
|
|
|
suppressBar
module
|
|
|
tipOnComplete
module
|
|
|
example
|
|
|
cancel
command
|
|
|
hang
command
|
|
|
io/multiple
command
|
|
|
io/single
command
|
|
|
prependETA
command
|
|
|
prependElapsed
command
|
|
|
remove
command
|
|
|
simple
command
|
|
|
singleBar
command
|
|
|
sort
command
|
|
|
stress
command
|






