Terminal progress bar for Go
Simple progress bar for console programs.
Installation
go get gopkg.in/cheggaaa/pb.v1
Usage
package main
import (
"gopkg.in/cheggaaa/pb.v1"
"time"
)
func main() {
count := 100000
bar := pb.StartNew(count)
for i := 0; i < count; i++ {
bar.Increment()
time.Sleep(time.Millisecond)
}
bar.FinishPrint("The End!")
}
Result will be like this:
> go run test.go
37158 / 100000 [================>_______________________________] 37.16% 1m11s
Customization
bar := pb.New(count)
bar.SetRefreshRate(time.Second)
bar.ShowPercent = true
bar.ShowBar = true
bar.ShowCounters = false
bar.ShowTimeLeft = true
bar.ShowSpeed = true
bar.SetWidth(80)
bar.SetMaxWidth(80)
bar.SetUnits(pb.U_BYTES)
bar.Start()
Progress bar for IO Operations
bar := pb.New(myDataLen).SetUnits(pb.U_BYTES)
bar.Start()
r := myReader
w := myWriter
reader := bar.NewProxyReader(r)
io.Copy(w, reader)
bar := pb.New(myDataLen).SetUnits(pb.U_BYTES)
bar.Start()
r := myReader
w := myWriter
writer := io.MultiWriter(w, bar)
io.Copy(writer, r)
bar.Finish()
Custom Progress Bar Look-and-feel
bar.Format("<.- >")
Multiple Progress Bars (experimental and unstable)
Do not print to terminal while pool is active.
package main
import (
"math/rand"
"sync"
"time"
"gopkg.in/cheggaaa/pb.v1"
)
func main() {
first := pb.New(200).Prefix("First ")
second := pb.New(200).Prefix("Second ")
third := pb.New(200).Prefix("Third ")
pool, err := pb.StartPool(first, second, third)
if err != nil {
panic(err)
}
wg := new(sync.WaitGroup)
for _, bar := range []*pb.ProgressBar{first, second, third} {
wg.Add(1)
go func(cb *pb.ProgressBar) {
for n := 0; n < 200; n++ {
cb.Increment()
time.Sleep(time.Millisecond * time.Duration(rand.Intn(100)))
}
cb.Finish()
wg.Done()
}(bar)
}
wg.Wait()
pool.Stop()
}
The result will be as follows:
$ go run example/multiple.go
First 141 / 1000 [===============>---------------------------------------] 14.10 % 44s
Second 139 / 1000 [==============>---------------------------------------] 13.90 % 44s
Third 152 / 1000 [================>--------------------------------------] 15.20 % 40s