Documentation ¶
Overview ¶
Package uiprogress is a library to render progress bars in terminal applications
Example ¶
package main import ( "time" "github.com/gosuri/uiprogress" ) func main() { uiprogress.Start() // start rendering bar := uiprogress.AddBar(100) // Add a new bar // optionally, append and prepend completion and elapsed time bar.AppendCompleted() bar.PrependElapsed() for bar.Incr() { time.Sleep(time.Millisecond * 20) } }
Output:
Index ¶
- Variables
- func Listen()
- func Start()
- func Stop()
- type Bar
- func (b *Bar) AppendCompleted() *Bar
- func (b *Bar) AppendElapsed() *Bar
- func (b *Bar) AppendFunc(f DecoratorFunc) *Bar
- func (b *Bar) Bytes() []byte
- func (b *Bar) CompletedPercent() float64
- func (b *Bar) CompletedPercentString() string
- func (b *Bar) Current() int
- func (b *Bar) Incr() bool
- func (b *Bar) PrependCompleted() *Bar
- func (b *Bar) PrependElapsed() *Bar
- func (b *Bar) PrependFunc(f DecoratorFunc) *Bar
- func (b *Bar) Set(n int) error
- func (b *Bar) String() string
- func (b *Bar) TimeElapsed() time.Duration
- func (b *Bar) TimeElapsedString() string
- type DecoratorFunc
- type Progress
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // Fill is the default character representing completed progress Fill byte = '=' // Head is the default character that moves when progress is updated Head byte = '>' // Empty is the default character that represents the empty progress Empty byte = '-' // LeftEnd is the default character in the left most part of the progress indicator LeftEnd byte = '[' // RightEnd is the default character in the right most part of the progress indicator RightEnd byte = ']' // Width is the default width of the progress bar Width = 70 // ErrMaxCurrentReached is error when trying to set current value that exceeds the total value ErrMaxCurrentReached = errors.New("errors: current value is greater total value") )
var Out = os.Stdout
Out is the default writer to render progress bars to
var RefreshInterval = time.Millisecond * 10
RefreshInterval in the default time duration to wait for refreshing the output
Functions ¶
Types ¶
type Bar ¶
type Bar struct { // Total of the total for the progress bar Total int // LeftEnd is character in the left most part of the progress indicator. Defaults to '[' LeftEnd byte // RightEnd is character in the right most part of the progress indicator. Defaults to ']' RightEnd byte // Fill is the character representing completed progress. Defaults to '=' Fill byte // Head is the character that moves when progress is updated. Defaults to '>' Head byte // Empty is the character that represents the empty progress. Default is '-' Empty byte // TimeStated is time progress began TimeStarted time.Time // Width is the width of the progress bar Width int // contains filtered or unexported fields }
Bar represents a progress bar
func (*Bar) AppendCompleted ¶
AppendCompleted appends the completion percent to the progress bar
func (*Bar) AppendElapsed ¶
AppendElapsed appends the time elapsed the be progress bar
func (*Bar) AppendFunc ¶
func (b *Bar) AppendFunc(f DecoratorFunc) *Bar
AppendFunc runs the decorator function and renders the output on the right of the progress bar
func (*Bar) CompletedPercent ¶
CompletedPercent return the percent completed
func (*Bar) CompletedPercentString ¶
CompletedPercentString returns the formatted string representation of the completed percent
func (*Bar) Incr ¶
Incr increments the current value by 1, time elapsed to current time and returns true. It returns false if the cursor has reached or exceeds total value.
Example ¶
package main import ( "fmt" "math/rand" "runtime" "sync" "time" "github.com/gosuri/uiprogress" ) func main() { runtime.GOMAXPROCS(runtime.NumCPU()) // use all available cpu cores // create a new bar and prepend the task progress to the bar count := 1000 bar := uiprogress.AddBar(count).AppendCompleted().PrependElapsed() bar.PrependFunc(func(b *uiprogress.Bar) string { return fmt.Sprintf("Task (%d/%d)", b.Current(), count) }) uiprogress.Start() var wg sync.WaitGroup // fanout into 1k go routines for i := 0; i < count; i++ { wg.Add(1) go func() { defer wg.Done() time.Sleep(time.Millisecond * time.Duration(rand.Intn(500))) bar.Incr() }() } time.Sleep(time.Second) // wait for a second for all the go routines to finish wg.Wait() uiprogress.Stop() }
Output:
func (*Bar) PrependCompleted ¶
PrependCompleted prepends the precent completed to the progress bar
func (*Bar) PrependElapsed ¶
PrependElapsed prepends the time elapsed to the begining of the bar
func (*Bar) PrependFunc ¶
func (b *Bar) PrependFunc(f DecoratorFunc) *Bar
PrependFunc runs decorator function and render the output left the progress bar
func (*Bar) Set ¶
Set the current count of the bar. It returns ErrMaxCurrentReached when trying n exceeds the total value. This is atomic operation and concurancy safe.
func (*Bar) TimeElapsed ¶
TimeElapsed returns the time elapsed
func (*Bar) TimeElapsedString ¶
TimeElapsedString returns the formatted string represenation of the time elapsed
type DecoratorFunc ¶
DecoratorFunc is a function that can be prepended and appended to the progress bar
Example ¶
package main import ( "time" "github.com/gosuri/uiprogress" ) func main() { var steps = []string{"downloading source", "installing deps", "compiling", "packaging", "seeding database", "deploying", "staring servers"} bar := uiprogress.AddBar(len(steps)) // prepend the current step to the bar bar.PrependFunc(func(b *uiprogress.Bar) string { return "app: " + steps[b.Current()-1] }) for bar.Incr() { time.Sleep(time.Millisecond) } }
Output:
type Progress ¶
type Progress struct { // Out is the writer to render progress bars to Out io.Writer // Width is the width of the progress bars Width int // Bars is the collection of progress bars Bars []*Bar // RefreshInterval in the time duration to wait for refreshing the output RefreshInterval time.Duration // contains filtered or unexported fields }
Progress represents the container that renders progress bars
func (*Progress) AddBar ¶
AddBar creates a new progress bar and adds to the container
Example ¶
package main import ( "sync" "time" "github.com/gosuri/uiprogress" ) func main() { waitTime := time.Millisecond * 100 uiprogress.Start() // start the progress bars in go routines var wg sync.WaitGroup bar1 := uiprogress.AddBar(20).AppendCompleted().PrependElapsed() wg.Add(1) go func() { defer wg.Done() for bar1.Incr() { time.Sleep(waitTime) } }() bar2 := uiprogress.AddBar(40).AppendCompleted().PrependElapsed() wg.Add(1) go func() { defer wg.Done() for bar2.Incr() { time.Sleep(waitTime) } }() time.Sleep(time.Second) bar3 := uiprogress.AddBar(20).PrependElapsed().AppendCompleted() wg.Add(1) go func() { defer wg.Done() for i := 1; i <= bar3.Total; i++ { bar3.Set(i) time.Sleep(waitTime) } }() // wait for all the go routines to finish wg.Wait() }
Output:
func (*Progress) Bypass ¶
Bypass returns a writer which allows non-buffered data to be written to the underlying output
func (*Progress) Listen ¶
func (p *Progress) Listen()
Listen listens for updates and renders the progress bars