progress

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2019 License: MIT Imports: 10 Imported by: 0

README

progress display

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

spinner

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 = '▒'
	CharSquare5 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 BarStyles = []BarChars{
	{'=', '>', ' '},
	{'#', '>', ' '},
	{'-', '>', '-'},
	{'▉', '▉', '░'},
	{'■', '■', ' '},
	{'■', '■', '▢'},
	{'■', '▶', ' '},
}

BarStyles some built in BarChars style collection

CharThemes collection. can use for Progress bar, RoundTripSpinner

View Source
var CharsThemes = [][]rune{
	{'卍', '卐'},
	{'☺', '☻'},
	{'░', '▒', '▓'},
	{'⊘', '⊖', '⊕', '⊗'},
	{'◐', '◒', '◓', '◑'},
	{'✣', '✤', '✥', '❉'},
	{'-', '\\', '|', '/'},
	{'▢', '■', '▢', '■'},
	[]rune("▖▘▝▗"),
	[]rune("◢◣◤◥"),
	[]rune("⌞⌟⌝⌜"),
	[]rune("◎●◯◌○⊙"),
	[]rune("◡◡⊙⊙◠◠"),
	[]rune("⇦⇧⇨⇩"),
	[]rune("✳✴✵✶✷✸✹"),
	[]rune("←↖↑↗→↘↓↙"),
	[]rune("➩➪➫➬➭➮➯➱"),
	[]rune("①②③④"),
	[]rune("㊎㊍㊌㊋㊏"),
	[]rune("⣾⣽⣻⢿⡿⣟⣯⣷"),
	[]rune("⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"),
	[]rune("▉▊▋▌▍▎▏▎▍▌▋▊▉"),
	[]rune("🌍🌎🌏"),
	[]rune("☰☱☲☳☴☵☶☷"),
	[]rune("⠋⠙⠚⠒⠂⠂⠒⠲⠴⠦⠖⠒⠐⠐⠒⠓⠋"),
	[]rune("🕐🕑🕒🕓🕔🕕🕖🕗🕘🕙🕚🕛"),
}

CharsThemes collection. can use for LoadingBar, LoadingSpinner

Functions

func GetCharTheme added in v1.1.4

func GetCharTheme(index int) rune

GetCharTheme by index number

func GetCharsTheme added in v1.1.4

func GetCharsTheme(index int) []rune

GetCharsTheme by index number

func RandomCharTheme added in v1.1.4

func RandomCharTheme() rune

RandomCharTheme get

func RandomCharsTheme added in v1.1.4

func RandomCharsTheme() []rune

RandomCharsTheme get

Types

type BarChars

type BarChars struct {
	Completed, Processing, Remaining rune
}

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

func RandomBarStyle added in v1.1.4

func RandomBarStyle() BarChars

RandomBarStyle get

type BuilderFunc added in v1.1.4

type BuilderFunc func() string

BuilderFunc build char string

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. Preview:

	1 [->--------------------------]
	3 [■■■>------------------------]
25/50 [==============>-------------]  50%
Example
maxStep := 105
p := CustomBar(60, BarStyles[0], maxStep)
p.MaxSteps = uint(maxStep)
p.Format = FullBarFormat

p.Start()
for i := 0; i < maxStep; i++ {
	time.Sleep(80 * time.Millisecond)
	p.Advance()
}
p.Finish()
Output:

func Counter

func Counter(maxSteps ...int) *Progress

Counter progress bar create

func CustomBar

func CustomBar(width int, cs BarChars, maxSteps ...int) *Progress

CustomBar create a custom progress bar.

func DynamicText

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

DynamicText progress bar create

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

maxStep := 105
p := DynamicText(messages, maxStep)

p.Start()

for i := 0; i < maxStep; i++ {
	time.Sleep(80 * time.Millisecond)
	p.Advance()
}

p.Finish()
Output:

func Full

func Full(maxSteps ...int) *Progress

Full text progress bar create.

func LoadBar

func LoadBar(chars []rune, maxSteps ...int) *Progress

LoadBar create a loading progress bar

func LoadingBar

func LoadingBar(chars []rune, maxSteps ...int) *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 RoundTripBar

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

RoundTripBar alias of RoundTrip()

func SpinnerBar added in v1.1.4

func SpinnerBar(chars []rune, maxSteps ...int) *Progress

SpinnerBar alias of load bar LoadBar()

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 ProgressFace

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

ProgressFace interface

type SpinnerFactory added in v1.1.4

type SpinnerFactory struct {
	// Speed is the running speed
	Speed time.Duration
	// Format setting display format
	Format string
	// Builder build custom spinner text
	Builder BuilderFunc
	// contains filtered or unexported fields
}

SpinnerFactory definition. ref https://github.com/briandowns/spinner

func LoadingSpinner added in v1.1.4

func LoadingSpinner(chars []rune, speed time.Duration) *SpinnerFactory

LoadingSpinner instance create

Example
s := LoadingSpinner(RandomCharsTheme(), 100*time.Millisecond)

s.Start("%s work handling ... ...")
// Run for some time to simulate work
time.Sleep(4 * time.Second)
s.Stop("work handle complete")
Output:

func RoundTripLoading added in v1.1.4

func RoundTripLoading(char rune, speed time.Duration, charNumAndBoxWidth ...int) *SpinnerFactory

RoundTripLoading create

func RoundTripSpinner added in v1.1.4

func RoundTripSpinner(char rune, speed time.Duration, charNumAndBoxWidth ...int) *SpinnerFactory

RoundTripSpinner instance create

Example
s := RoundTripSpinner(RandomCharTheme(), 100*time.Millisecond)

s.Start("%s work handling ... ...")
// Run for some time to simulate work
time.Sleep(4 * time.Second)
s.Stop("work handle complete")
Output:

func Spinner added in v1.1.4

func Spinner(speed time.Duration) *SpinnerFactory

Spinner instance

func (*SpinnerFactory) Active added in v1.1.4

func (s *SpinnerFactory) Active() bool

Active status

func (*SpinnerFactory) Restart added in v1.1.4

func (s *SpinnerFactory) Restart()

Restart will stop and start the spinner

func (*SpinnerFactory) Start added in v1.1.4

func (s *SpinnerFactory) Start(format ...string)

Start run spinner

func (*SpinnerFactory) Stop added in v1.1.4

func (s *SpinnerFactory) Stop(finalMsg ...string)

Stop run spinner

func (*SpinnerFactory) WithBuilder added in v1.1.4

func (s *SpinnerFactory) WithBuilder(builder BuilderFunc) *SpinnerFactory

WithBuilder set spinner text builder

type WidgetFunc

type WidgetFunc func(p *Progress) string

WidgetFunc handler func for progress widget

func BarWidget added in v1.2.1

func BarWidget(width int, cs BarChars) WidgetFunc

BarWidget create a progress bar widget.

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

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.

func LoadingWidget

func LoadingWidget(chars []rune) WidgetFunc

LoadingWidget create a loading progress widget

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