Documentation
¶
Overview ¶
Simple console progress bars
Example ¶
package main
import (
"time"
"github.com/cheggaaa/pb"
)
func main() {
count := 5000
bar := pb.New(count)
// show percents (by default already true)
bar.ShowPercent = true
// show bar (by default already true)
bar.ShowBar = true
bar.ShowCounters = true
bar.ShowTimeLeft = true
// and start
bar.Start()
for i := 0; i < count; i++ {
bar.Increment()
time.Sleep(time.Millisecond)
}
bar.FinishPrint("The End!")
}
Example (Copy) ¶
package main
import (
"fmt"
"io"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/cheggaaa/pb"
)
func main() {
// check args
if len(os.Args) < 3 {
printUsage()
return
}
sourceName, destName := os.Args[1], os.Args[2]
// check source
var source io.Reader
var sourceSize int64
if strings.HasPrefix(sourceName, "http://") {
// open as url
resp, err := http.Get(sourceName)
if err != nil {
fmt.Printf("Can't get %s: %v\n", sourceName, err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Printf("Server return non-200 status: %v\n", resp.Status)
return
}
i, _ := strconv.Atoi(resp.Header.Get("Content-Length"))
sourceSize = int64(i)
source = resp.Body
} else {
// open as file
s, err := os.Open(sourceName)
if err != nil {
fmt.Printf("Can't open %s: %v\n", sourceName, err)
return
}
defer s.Close()
// get source size
sourceStat, err := s.Stat()
if err != nil {
fmt.Printf("Can't stat %s: %v\n", sourceName, err)
return
}
sourceSize = sourceStat.Size()
source = s
}
// create dest
dest, err := os.Create(destName)
if err != nil {
fmt.Printf("Can't create %s: %v\n", destName, err)
return
}
defer dest.Close()
// create bar
bar := pb.New(int(sourceSize)).SetUnits(pb.U_BYTES).SetRefreshRate(time.Millisecond * 10)
bar.ShowSpeed = true
bar.Start()
// create proxy reader
reader := bar.NewProxyReader(source)
// and copy from reader
io.Copy(dest, reader)
bar.Finish()
}
func printUsage() {
fmt.Println("copy [source file or url] [dest file]")
}
Index ¶
- Constants
- func Format(i int64, units Units, width int) string
- func FormatBytes(i int64) (result string)
- func FormatDuration(d time.Duration) string
- type Callback
- type EWMA
- type ProgressBar
- func (pb *ProgressBar) Finish()
- func (pb *ProgressBar) Format(format string) *ProgressBar
- func (pb *ProgressBar) GOString() string
- func (pb *ProgressBar) GetWidth() int
- func (pb *ProgressBar) Postfix(postfix string) *ProgressBar
- func (pb *ProgressBar) Prefix(prefix string) *ProgressBar
- func (pb *ProgressBar) Set64(current int64) *ProgressBar
- func (pb *ProgressBar) SetMaxWidth(width int) *ProgressBar
- func (pb *ProgressBar) SetRefreshRate(rate time.Duration) *ProgressBar
- func (pb *ProgressBar) SetUnits(units Units) *ProgressBar
- func (pb *ProgressBar) SetWidth(width int) *ProgressBar
- func (pb *ProgressBar) Start() *ProgressBar
- func (pb *ProgressBar) Update()
- type Units
Examples ¶
Constants ¶
const ( // AverageMetricAge average over a one-minute period, which means the average // age of the metrics is in the period of 30 seconds AverageMetricAge float64 = 30.0 // Decay formula for computing the decay factor for average metric age Decay float64 = 2 / (float64(AverageMetricAge) + 1) )
const ( // Default refresh rate - 200ms DEFAULT_REFRESH_RATE = time.Millisecond * 200 FORMAT = "[=>-]" )
Variables ¶
This section is empty.
Functions ¶
func FormatBytes ¶
Convert bytes to human readable string. Like a 2 MB, 64.2 KB, 52 B
func FormatDuration ¶
Types ¶
type Callback ¶
type Callback func(out string)
Callback for custom output For example:
bar.Callback = func(s string) {
mySuperPrint(s)
}
type EWMA ¶
type EWMA struct {
// contains filtered or unexported fields
}
EWMA represents the exponentially weighted moving average of a series of numbers.
type ProgressBar ¶
type ProgressBar struct {
Total int64
RefreshRate time.Duration
ShowPercent, ShowCounters bool
ShowSpeed, ShowTimeLeft, ShowBar bool
ShowFinalTime bool
Output io.Writer
Callback Callback
NotPrint bool
Units Units
Width int
ForceWidth bool
ManualUpdate bool
// default width for unit numbers and time box
UnitsWidth int
TimeBoxWidth int
BarWidth int
BarStart string
BarEnd string
Empty string
Current string
CurrentN string
AlwaysUpdate bool
// contains filtered or unexported fields
}
func New64 ¶
func New64(total int64) *ProgressBar
Create new progress bar object uding int64 as total
func (*ProgressBar) Format ¶
func (pb *ProgressBar) Format(format string) *ProgressBar
Set custom format for bar Example: bar.Format("[=>_]") Example: bar.Format("[\x00=\x00>\x00-\x00]") // \x00 is the delimiter
func (*ProgressBar) GOString ¶
func (pb *ProgressBar) GOString() string
func (*ProgressBar) GetWidth ¶
func (pb *ProgressBar) GetWidth() int
func (*ProgressBar) Postfix ¶
func (pb *ProgressBar) Postfix(postfix string) *ProgressBar
Set postfix string
func (*ProgressBar) Prefix ¶
func (pb *ProgressBar) Prefix(prefix string) *ProgressBar
Set prefix string
func (*ProgressBar) Set64 ¶
func (pb *ProgressBar) Set64(current int64) *ProgressBar
Set64 sets the current value as int64
func (*ProgressBar) SetMaxWidth ¶
func (pb *ProgressBar) SetMaxWidth(width int) *ProgressBar
Set max width, if width is bigger than terminal width, will be ignored
func (*ProgressBar) SetRefreshRate ¶
func (pb *ProgressBar) SetRefreshRate(rate time.Duration) *ProgressBar
Set bar refresh rate
func (*ProgressBar) SetUnits ¶
func (pb *ProgressBar) SetUnits(units Units) *ProgressBar
Set units bar.SetUnits(U_NO) - by default bar.SetUnits(U_BYTES) - for Mb, Kb, etc
func (*ProgressBar) SetWidth ¶
func (pb *ProgressBar) SetWidth(width int) *ProgressBar
Set bar width
func (*ProgressBar) Update ¶
func (pb *ProgressBar) Update()
Write the current state of the progressbar