pb

package module
v0.0.0-...-1389bea Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 12, 2020 License: BSD-3-Clause Imports: 9 Imported by: 1

README

Terminal progress bar for Go

Simple progress bar for console programms.

Installation

go get github.com/cheggaaa/pb

Usage

package main

import (
	"github.com/cheggaaa/pb"
	"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

More functions?

// create bar
bar := pb.New(count)

// refresh info every second (default 200ms)
bar.RefreshRate = time.Second

// show percents (by default already true)
bar.ShowPercent = true

// show bar (by default already true)
bar.ShowBar = true

// no need counters
bar.ShowCounters = false

// show "time left"
bar.ShowTimeLeft = true

// show average speed    
bar.ShowSpeed = true

// convert output to readable format (like KB, MB)     
bar.Units = pb.U_BYTES

// and start
bar.Start()

Want handle progress of io operations?

// create and start bar
bar := pb.New(myDataLen)
// for output like MB, KB
bar.Units = pb.U_BYTES
bar.Start()

// my io.Reader
r := myReader

// my io.Writer
w := myWriter

// create multi writer
writer := io.MultiWriter(w, bar)

// and copy
io.Copy(writer, r)

// show example/copy/copy.go for advanced example

Not like the looks?

// insert before usage
pb.BarStart = "<"
pb.BarEnd   = ">"
pb.Empty    = " "
pb.Current  = "-"
pb.CurrentN = "."

Documentation

Index

Constants

View Source
const (
	// By default, without type handle
	U_NO = 0
	// Handle as b, Kb, Mb, etc
	U_BYTES = 1
)
View Source
const (
	TIOCGWINSZ     = 0x5413
	TIOCGWINSZ_OSX = 1074295912
)

Variables

View Source
var (
	// Default refresh rate - 200ms
	DefaultRefreshRate = time.Millisecond * 200

	BarStart = "["
	BarEnd   = "]"
	Empty    = "_"
	Current  = "="
	CurrentN = ">"
)

Functions

func Format

func Format(i int64, units int) string

Format integer

func FormatBytes

func FormatBytes(i int64) (result string)

Convert bytes to human readable string. Like a 2 MiB, 64.2 KiB, 52 B

Types

type Callback

type Callback func(out string)

Callback for custom output For example:

bar.Callback = func(s string) {
    mySuperPrint(s)
}

type ProgressBar

type ProgressBar struct {
	Total                            int64
	RefreshRate                      time.Duration
	ShowPercent, ShowCounters        bool
	ShowSpeed, ShowTimeLeft, ShowBar bool
	Output                           io.Writer
	Callback                         Callback
	NotPrint                         bool
	Units                            int
	// contains filtered or unexported fields
}

func New

func New(total int) *ProgressBar

Create new progress bar object

func StartNew

func StartNew(total int) (pb *ProgressBar)

Create new object and start

func (*ProgressBar) Add

func (pb *ProgressBar) Add(add int) int

Add to current value

func (*ProgressBar) Finish

func (pb *ProgressBar) Finish()

End print

func (*ProgressBar) FinishPrint

func (pb *ProgressBar) FinishPrint(str string)

End print and write string 'str'

func (*ProgressBar) Increment

func (pb *ProgressBar) Increment() int

Increment current value

func (*ProgressBar) Read

func (pb *ProgressBar) Read(p []byte) (n int, err error)

implement io.Reader

func (*ProgressBar) Set

func (pb *ProgressBar) Set(current int)

Set current value

func (*ProgressBar) Start

func (pb *ProgressBar) Start()

Start print

func (*ProgressBar) Write

func (pb *ProgressBar) Write(p []byte) (n int, err error)

implement io.Writer

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL