Documentation ¶
Overview ¶
Package progress provide terminal progress bar display. Such as: `Txt`, `Bar`, `Loading`, `RoundTrip`, `DynamicText` ...
Index ¶
- Constants
- Variables
- func GetCharTheme(index int) rune
- func GetCharsTheme(index int) []rune
- func MaxSteps(maxStep int) func(p *Progress)
- func RandomCharTheme() rune
- func RandomCharsTheme() []rune
- func RenderFormat(f string) func(p *Progress)
- type BarChars
- type BuilderFunc
- type Progress
- func Bar(maxSteps ...int) *Progress
- func Counter(maxSteps ...int) *Progress
- func CustomBar(width int, cs BarChars, maxSteps ...int) *Progress
- func DynamicText(messages map[int]string, maxSteps ...int) *Progress
- func Full(maxSteps ...int) *Progress
- func LoadBar(chars []rune, maxSteps ...int) *Progress
- func LoadingBar(chars []rune, maxSteps ...int) *Progress
- func New(maxSteps ...int) *Progress
- func NewWithConfig(fn func(p *Progress), maxSteps ...int) *Progress
- func RoundTrip(char rune, charNumAndBoxWidth ...int) *Progress
- func RoundTripBar(char rune, charNumAndBoxWidth ...int) *Progress
- func SpinnerBar(chars []rune, maxSteps ...int) *Progress
- func Tape(maxSteps ...int) *Progress
- func Txt(maxSteps ...int) *Progress
- func (p *Progress) AddMessage(name, message string)
- func (p *Progress) AddMessages(msgMap map[string]string)
- func (p *Progress) AddWidget(name string, handler WidgetFunc) *Progress
- func (p *Progress) AddWidgets(widgets map[string]WidgetFunc)
- func (p *Progress) Advance(steps ...uint)
- func (p *Progress) AdvanceTo(step uint)
- func (p *Progress) Binding(data any) *Progress
- func (p *Progress) Bound() any
- func (p *Progress) Config(fn func(p *Progress)) *Progress
- func (p *Progress) Destroy()
- func (p *Progress) Display()
- func (p *Progress) Finish(message ...string)
- func (p *Progress) FinishedAt() time.Time
- func (p *Progress) Handler(name string) WidgetFunc
- func (p *Progress) Percent() float32
- func (p *Progress) Progress() uint
- func (p *Progress) SetWidget(name string, handler WidgetFunc) *Progress
- func (p *Progress) Start(maxSteps ...int)
- func (p *Progress) StartedAt() time.Time
- func (p *Progress) Step() uint
- func (p *Progress) WithMaxSteps(maxSteps ...int) *Progress
- func (p *Progress) WithOptions(fns ...func(p *Progress)) *Progress
- type Progresser
- type SpinnerFactory
- func LoadingSpinner(chars []rune, speed time.Duration) *SpinnerFactory
- func RoundTripLoading(char rune, speed time.Duration, charNumAndBoxWidth ...int) *SpinnerFactory
- func RoundTripSpinner(char rune, speed time.Duration, charNumAndBoxWidth ...int) *SpinnerFactory
- func Spinner(speed time.Duration) *SpinnerFactory
- type WidgetFunc
Examples ¶
Constants ¶
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
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 text progress
const ( // BarWidth default bar width BarWidth = 40 BarFormat = "{@bar} {@percent:4s}%({@current}/{@max}){@message}" // MdlBarFormat more format MdlBarFormat = "{@bar} {@percent:4s}%({@current}/{@max}) {@elapsed:7s}/{@estimated:-7s}" FullBarFormat = "{@bar} {@percent:4s}%({@current}/{@max}) {@elapsed:7s}/{@estimated:-7s} {@memory:6s}" )
internal format for ProgressBar
Variables ¶
var BarStyles = []BarChars{
{'=', '>', ' '},
{'=', '>', '-'},
{'#', '>', ' '},
{'#', '>', '-'},
{'*', '>', '-'},
{'▉', '▉', '░'},
{'■', '■', ' '},
{'■', '■', '▢'},
{'■', '▶', ' '},
}
BarStyles some built in BarChars style collection
var CharThemes = []rune{ CharEqual, CharCenter, CharSquare, CharSquare1, CharSquare2, }
CharThemes collection. can use for Progress bar, RoundTripSpinner
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 ¶
GetCharTheme by index number. if index not exist, will return a random theme
func RenderFormat ¶
RenderFormat set rendered format option
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 // Newline render progress on newline Newline bool // 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 ¶
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 DynamicText ¶
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 LoadingBar ¶
LoadingBar alias of load bar LoadBar()
func NewWithConfig ¶
NewWithConfig create new Progress
func RoundTrip ¶
RoundTrip create a RoundTrip progress bar.
Usage:
p := RoundTrip(CharEqual) // p := RoundTrip('*') // custom char p.Start() .... p.Finish()
func RoundTripBar ¶
RoundTripBar alias of RoundTrip()
func SpinnerBar ¶
SpinnerBar alias of load bar LoadBar()
func (*Progress) AddMessage ¶
AddMessage to progress instance
func (*Progress) AddMessages ¶
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) 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 ¶
Finish the progress output. if provide finish message, will delete progress bar then print the message.
func (*Progress) Handler ¶
func (p *Progress) Handler(name string) WidgetFunc
Handler get widget handler by widget name
func (*Progress) SetWidget ¶
func (p *Progress) SetWidget(name string, handler WidgetFunc) *Progress
SetWidget to progress instance
func (*Progress) WithMaxSteps ¶
WithMaxSteps setting max steps
func (*Progress) WithOptions ¶
WithOptions add more option at once for the progress instance
type Progresser ¶
type Progresser interface { Start(maxSteps ...int) Advance(steps ...uint) AdvanceTo(step uint) Finish(msg ...string) Bound() any }
Progresser progress interface
type SpinnerFactory ¶
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 ¶
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 ¶
func RoundTripLoading(char rune, speed time.Duration, charNumAndBoxWidth ...int) *SpinnerFactory
RoundTripLoading create
func RoundTripSpinner ¶
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 (*SpinnerFactory) Restart ¶
func (s *SpinnerFactory) Restart()
Restart will stop and start the spinner
func (*SpinnerFactory) WithBuilder ¶
func (s *SpinnerFactory) WithBuilder(builder BuilderFunc) *SpinnerFactory
WithBuilder set spinner text builder
type WidgetFunc ¶
WidgetFunc handler func for progress widget
func BarWidget ¶
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 loading progress widget
func RoundTripWidget ¶
func RoundTripWidget(char rune, charNum, boxWidth int) WidgetFunc
RoundTripWidget create a round-trip widget for progress bar.
Output like `[ ==== ]`