Documentation
¶
Index ¶
- Constants
- Variables
- func GetCharTheme(index int) rune
- func GetCharsTheme(index int) []rune
- func HowLongAgo(sec int64) string
- func RandomCharTheme() rune
- func RandomCharsTheme() []rune
- 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 interface{}) *Progress
- func (p *Progress) Bound() interface{}
- 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
- type ProgressBar
- type ProgressFace
- 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 ( 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
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 ¶
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 GetCharsTheme ¶ added in v1.1.4
GetCharsTheme by index number
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 ¶
Bar create a default progress bar.
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()
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()
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 ¶ added in v1.1.4
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
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 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")
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")
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 ¶
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.
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 `[ ==== ]`