progress

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2018 License: MIT Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

progress display

  • progress bar
  • text progress bar
  • pending/loading progress bar
  • counter
  • dynamic Text

Documentation

Index

Examples

Constants

View Source
const (
	CharStar    rune = '*'
	CharPlus    rune = '+'
	CharWell    rune = '#'
	CharEqual   rune = '='
	CharEqual1  rune = '═'
	CharSpace   rune = ' '
	CharCenter  rune = '●'
	CharSquare  rune = '■'
	CharSquare1 rune = '▇'
	CharSquare2 rune = '▉'
	CharSquare3 rune = '░'
	CharSquare4 rune = '▒'
	// Hyphen Minus
	CharHyphen      rune = '-'
	CharCNHyphen    rune = '—'
	CharUnderline   rune = '_'
	CharLeftArrow   rune = '<'
	CharRightArrow  rune = '>'
	CharRightArrow1 rune = '▶'
)

some built in chars

View Source
const (
	DefBarWidth   = 60
	DefBarFormat  = "{@bar} {@percent:4s}%({@current}/{@max}){@message}"
	FullBarFormat = "{@bar} {@percent:4s}%({@current}/{@max}) {@elapsed:7s}/{@estimated:-7s} {@memory:6s}"
)

internal format for ProgressBar

View Source
const (
	MinFormat  = "{@message}{@current}"
	TxtFormat  = "{@message}{@percent:4s}%({@current}/{@max})"
	DefFormat  = "{@message}{@percent:4s}%({@current}/{@max})"
	FullFormat = "{@percent:4s}%({@current}/{@max}) {@elapsed:7s}/{@estimated:-7s} {@memory:6s}"
)

internal format for Progress

Variables

View Source
var (
	BarCharsStyle  = BarChars{'#', '>', ' '}
	BarCharsStyle1 = BarChars{'▉', '▉', '░'}
	BarCharsStyle2 = BarChars{'■', '■', ' '}
	BarCharsStyle3 = BarChars{'■', '▶', ' '}
	BarCharsStyle4 = BarChars{'=', '>', ' '}
)

some built in BarChars style

View Source
var (
	LoadingTheme1 = []rune{'-', '\\', '|', '/'}
	LoadingTheme2 = []rune{'◐', '◒', '◓', '◑'}
	LoadingTheme3 = []rune{'✣', '✤', '✥', '❉'}
	LoadingTheme4 = []rune{'卍', '卐'}
	LoadingTheme5 = []rune("⌞⌟⌝⌜")
	LoadingTheme6 = []rune("◎●◯◌○⊙")
	LoadingTheme7 = []rune("㊎㊍㊌㊋㊏")
)

default spinner chars: -\|/

Functions

func HowLongAgo

func HowLongAgo(sec int64) string

HowLongAgo format a seconds, get how lang ago

Types

type BarChars

type BarChars struct {
	Completed, Processing, Remaining rune
}

BarChars setting for a progress bar. default {'#', '>', ' '}

type Progress

type Progress struct {
	// Format string the bar format
	Format string
	// MaxSteps maximal steps.
	MaxSteps uint
	// StepWidth the width for display "{@current}". default is 2
	StepWidth uint8
	// Overwrite prev output. default is True
	Overwrite bool
	// RedrawFreq redraw freq. default is 1
	RedrawFreq uint8
	// Widgets for build the progress bar
	Widgets map[string]WidgetFunc
	// Messages named messages for build progress bar
	// Example:
	// 	{"msg": "downloading ..."}
	// 	"{@percent}% {@msg}" -> "83% downloading ..."
	Messages map[string]string
	// contains filtered or unexported fields
}

Progress definition Refer:

https://github.com/inhere/php-console/blob/master/src/Utils/ProgressBar.php

func Bar

func Bar(maxSteps ...int) *Progress

Bar create a default progress bar.

func Counter

func Counter(maxSteps ...int) *Progress

Counter progress bar create

func CustomBar

func CustomBar(width int, cs BarChars) *Progress

CustomBar create a custom progress bar.

func DynamicText

func DynamicText(messages map[int]string) *Progress

DynamicText progress bar create

func Full

func Full(maxSteps ...int) *Progress

Full text progress bar create.

func LoadBar

func LoadBar(chars []rune) *Progress

LoadBar create a loading progress bar

func LoadingBar

func LoadingBar(chars []rune) *Progress

LoadingBar alias of load bar LoadBar()

func New

func New(maxSteps ...int) *Progress

New Progress instance

func NewWithConfig

func NewWithConfig(fn func(p *Progress), maxSteps ...int) *Progress

NewWithConfig create new Progress

func RoundTrip

func RoundTrip(char rune, charNumAndBoxWidth ...int) *Progress

RoundTrip create a RoundTrip progress bar. Usage:

p := RoundTrip(CharEqual)
// p := RoundTrip('*') // custom char
p.Start()
....
p.Finish()

func Tape

func Tape(maxSteps ...int) *Progress

Tape create new tape progress bar. is alias of Bar()

func Txt

func Txt(maxSteps ...int) *Progress

Txt progress bar create.

func (*Progress) AddMessage

func (p *Progress) AddMessage(name, message string)

AddMessage to progress instance

func (*Progress) AddMessages

func (p *Progress) AddMessages(msgMap map[string]string)

AddMessages to progress instance

func (*Progress) AddWidget

func (p *Progress) AddWidget(name string, handler WidgetFunc) *Progress

AddWidget to progress instance

func (*Progress) AddWidgets

func (p *Progress) AddWidgets(widgets map[string]WidgetFunc)

AddWidgets to progress instance

func (*Progress) Advance

func (p *Progress) Advance(steps ...uint)

Advance specified step size. default is 1

func (*Progress) AdvanceTo

func (p *Progress) AdvanceTo(step uint)

AdvanceTo specified number of steps

func (*Progress) Binding

func (p *Progress) Binding(data interface{}) *Progress

Binding user custom data to instance

func (*Progress) Bound

func (p *Progress) Bound() interface{}

Bound get bound sub struct instance

func (*Progress) Config

func (p *Progress) Config(fn func(p *Progress)) *Progress

Config the progress instance

func (*Progress) Destroy

func (p *Progress) Destroy()

Destroy removes the progress bar from the current line.

This is useful if you wish to write some output while a progress bar is running. Call display() to show the progress bar again.

func (*Progress) Display

func (p *Progress) Display()

Display outputs the current progress string.

func (*Progress) Finish

func (p *Progress) Finish(message ...string)

Finish the progress output. if provide finish message, will delete progress bar then print the message.

func (*Progress) FinishedAt

func (p *Progress) FinishedAt() time.Time

FinishedAt time get

func (*Progress) Handler

func (p *Progress) Handler(name string) WidgetFunc

Handler get widget handler by widget name

func (*Progress) Percent

func (p *Progress) Percent() float32

Percent gets the current percent

func (*Progress) Progress

func (p *Progress) Progress() uint

Progress alias of the Step()

func (*Progress) SetWidget

func (p *Progress) SetWidget(name string, handler WidgetFunc) *Progress

SetWidget to progress instance

func (*Progress) Start

func (p *Progress) Start(maxSteps ...int)

Start the progress bar

func (*Progress) StartedAt

func (p *Progress) StartedAt() time.Time

StartedAt time get

func (*Progress) Step

func (p *Progress) Step() uint

Step gets the current step position.

func (*Progress) WithMaxSteps

func (p *Progress) WithMaxSteps(maxSteps ...int) *Progress

WithMaxSteps setting max steps

type ProgressBar

type ProgressBar struct {
	// Width for the bar. default is 100
	Width int
	// Chars config for the bar. default {'#', '>', ' '}
	Chars BarChars
}

ProgressBar definition. Preview:

	1 [->--------------------------]
	3 [■■■>------------------------]
25/50 [==============>-------------]  50%

func (ProgressBar) Create

func (pb ProgressBar) Create(maxSteps ...int) *Progress

Config the progress instance

type ProgressFace

type ProgressFace interface {
	Start(maxSteps ...int)
	Advance(steps ...uint)
	AdvanceTo(step uint)
	Finish(msg ...string)
	Bound() interface{}
}

ProgressFace interface

type RoundTripBar

type RoundTripBar struct {
	Char     rune
	CharNum  int
	BoxWidth int
}

RoundTripBar config

func (RoundTripBar) Create

func (rt RoundTripBar) Create(maxSteps ...int) *Progress

Create Progress bar from RoundTripBar config.

type WidgetFunc

type WidgetFunc func(p *Progress) string

WidgetFunc handler func for progress widget

func DynamicTextWidget

func DynamicTextWidget(messages map[int]string) WidgetFunc

DynamicTextWidget dynamic text message widget for progress bar. for param messages: int is percent, range is 0 - 100. value is message string. Usage please example.

Example
widget := DynamicTextWidget(map[int]string{
	// int is percent, range is 0 - 100.
	20:  " Prepare ...",
	40:  " Request ...",
	65:  " Transport ...",
	95:  " Saving ...",
	100: " Handle Complete.",
})

maxStep := 105
p := New(maxStep).Config(func(p *Progress) {
	p.Format = "{@percent:4s}%({@current}/{@max}) {@message}"
}).AddWidget("message", widget)

runProgressBar(p, maxStep, 80)

p.Finish()

func LoadingWidget

func LoadingWidget(chars []rune) WidgetFunc

LoadingWidget create a loading progress widget

func ProgressBarWidget

func ProgressBarWidget(width int, cs BarChars) WidgetFunc

ProgressBarWidget create a progress bar widget.

Output like `[==============>-------------]`

func RoundTripWidget

func RoundTripWidget(char rune, charNum, boxWidth int) WidgetFunc

RoundTripWidget create a round-trip widget for progress bar.

Output like `[ ==== ]`

Jump to

Keyboard shortcuts

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