go get github.com/cheggaaa/pb/v3
Documentation for v1 bar available here.
package main
import (
"time""github.com/cheggaaa/pb/v3"
)
funcmain() {
count:=100000// create and start new barbar:=pb.StartNew(count)
// start bar from 'default' template// bar := pb.Default.Start(count)// start bar from 'simple' template// bar := pb.Simple.Start(count)// start bar from 'full' template// bar := pb.Full.Start(count)fori:=0; i<count; i++ {
bar.Increment()
time.Sleep(time.Millisecond)
}
// finish barbar.Finish()
}Result will be like this:
> go run test.go
37158 / 100000 [---------------->_______________________________] 37.16% 916 p/s
// create barbar:=pb.New(count)
// refresh info every second (default 200ms)bar.SetRefreshRate(time.Second)
// force set io.Writer, by default it's os.Stderrbar.SetWriter(os.Stdout)
// bar will format numbers as bytes (B, KiB, MiB, etc)bar.Set(pb.Bytes, true)
// bar use SI bytes prefix names (B, kB) instead of IEC (B, KiB)bar.Set(pb.SIBytesPrefix, true)
// set custom bar templatebar.SetTemplateString(myTemplate)
// check for error after template setiferr:=bar.Err(); err!=nil {
return
}
// start barbar.Start()package main
import (
"crypto/rand""io""io/ioutil""github.com/cheggaaa/pb/v3"
)
funcmain() {
varlimitint64=1024*1024*500// we will copy 500 MiB from /dev/rand to /dev/nullreader:=io.LimitReader(rand.Reader, limit)
writer:=ioutil.Discard// start new barbar:=pb.Full.Start64(limit)
// create proxy readerbarReader:=bar.NewProxyReader(reader)
// copy from proxy readerio.Copy(writer, barReader)
// finish barbar.Finish()
}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.
The bar element accepts five standard parts: left border, fill, current, empty, and right border. It also accepts optional sixth and seventh parts for the empty left border and finished right border.
Set UNICODE_PROGRESS_BAR=true to use the built-in Unicode progress bar glyphs for bars that use the default bar elements when the active terminal font supports them.
tmpl:=`{{ red "With funcs:" }} {{ bar . "<" "-" (cycle . "↖" "↗" "↘" "↙" ) "." ">"}} {{speed . | rndcolor }} {{percent .}} {{string . "my_green_string" | green}} {{string . "my_blue_string" | blue}}`// start bar based on our templatebar:=pb.ProgressBarTemplate(tmpl).Start64(limit)
// set values for string elementsbar.Set("my_green_string", "green").Set("my_blue_string", "blue")