Terminal progress bar for Go
Installation
go get github.com/cheggaaa/pb/v3
Documentation for v1 bar available here.
Quick start
package main
import (
"time"
"github.com/cheggaaa/pb/v3"
)
func main() {
count := 100000
bar := pb.StartNew(count)
for i := 0; i < count; i++ {
bar.Increment()
time.Sleep(time.Millisecond)
}
bar.Finish()
}
Result will be like this:
> go run test.go
37158 / 100000 [---------------->_______________________________] 37.16% 916 p/s
Settings
bar := pb.New(count)
bar.SetRefreshRate(time.Second)
bar.SetWriter(os.Stdout)
bar.Set(pb.Bytes, true)
bar.Set(pb.SIBytesPrefix, true)
bar.SetTemplateString(myTemplate)
if err := bar.Err(); err != nil {
return
}
bar.Start()
Progress bar for IO Operations
package main
import (
"crypto/rand"
"io"
"io/ioutil"
"github.com/cheggaaa/pb/v3"
)
func main() {
var limit int64 = 1024 * 1024 * 500
reader := io.LimitReader(rand.Reader, limit)
writer := ioutil.Discard
bar := pb.Full.Start64(limit)
barReader := bar.NewProxyReader(reader)
io.Copy(writer, barReader)
bar.Finish()
}
Custom Progress Bar templates
Rendering based on builtin text/template package. You can use existing pb's elements or create you own.
All available elements are described in the element.go file.
All in one example:
tmpl := `{{ red "With funcs:" }} {{ bar . "<" "-" (cycle . "↖" "↗" "↘" "↙" ) "." ">"}} {{speed . | rndcolor }} {{percent .}} {{string . "my_green_string" | green}} {{string . "my_blue_string" | blue}}`
bar := pb.ProgressBarTemplate(tmpl).Start64(limit)
bar.Set("my_green_string", "green").Set("my_blue_string", "blue")