Skip to content

Import path: gitlab.soludian.com/soludian/ftkit/pkg/progress_bar

progress_bar

go
import "gitlab.soludian.com/soludian/ftkit/pkg/progress_bar"
Example (X Out Of Y)

go
bar := NewOptions(100, OptionSetPredictTime(true))

for i := 0; i < 100; i++ {
	bar.Add(1)
	time.Sleep(1 * time.Millisecond)
}

Index

type Option

Option is the type all options need to adhere to

go
type Option func(p *ProgressBar)

func OptionClearOnFinish

go
func OptionClearOnFinish() Option

OptionClearOnFinish will clear the bar once its finished

Example

go
bar := NewOptions(100, OptionSetWidth(10), OptionClearOnFinish())
bar.Reset()
bar.Finish()
fmt.Println("Finished")
// Output:
// Finished

Output

Finished

func OptionEnableColorCodes

go
func OptionEnableColorCodes(colorCodes bool) Option

OptionEnableColorCodes enables or disables support for color codes using mitchellh/colorstring

func OptionFullWidth

go
func OptionFullWidth() Option

OptionFullWidth sets the bar to be full width

func OptionOnCompletion

go
func OptionOnCompletion(cmpl func()) Option

OptionOnCompletion will invoke cmpl function once its finished

func OptionSetDescription

go
func OptionSetDescription(description string) Option

OptionSetDescription sets the description of the bar to render in front of it

func OptionSetElapsedTime

go
func OptionSetElapsedTime(elapsedTime bool) Option

OptionSetElapsedTime will enable elapsed time. Always enabled if OptionSetPredictTime is true.

func OptionSetItsString

go
func OptionSetItsString(iterationString string) Option

OptionSetItsString sets what's displayed for iterations a second. The default is "it" which would display: "it/s"

func OptionSetPredictTime

go
func OptionSetPredictTime(predictTime bool) Option

OptionSetPredictTime will also attempt to predict the time remaining.

Example

go
bar := NewOptions(100, OptionSetWidth(10), OptionSetPredictTime(false))
_ = bar.Add(10)
// Output:
// 10% |█         |

Output

10% |█         |

func OptionSetRenderBlankState

go
func OptionSetRenderBlankState(r bool) Option

OptionSetRenderBlankState sets whether or not to render a 0% bar on construction

Example

go
NewOptions(10, OptionSetWidth(10), OptionSetRenderBlankState(true))
// Output:
// 0% |          |  [0s:0s]

Output

0% |          |  [0s:0s]

func OptionSetTheme

go
func OptionSetTheme(t Theme) Option

OptionSetTheme sets the elements the bar is constructed of

func OptionSetVisibility

go
func OptionSetVisibility(visibility bool) Option

OptionSetVisibility sets the visibility

func OptionSetWidth

go
func OptionSetWidth(s int) Option

OptionSetWidth sets the width of the bar

func OptionSetWriter

go
func OptionSetWriter(w io.Writer) Option

OptionSetWriter sets the output writer (defaults to os.StdOut)

func OptionShowBytes

go
func OptionShowBytes(val bool) Option

OptionShowBytes will update the progress bar configuration settings to display/hide kBytes/Sec

Example (Spinner)

go
/*
 Spinner test with iterations and count
*/
bar := NewOptions(-1,
	OptionSetWidth(10),
	OptionShowBytes(true),
)

bar.Reset()
time.Sleep(1 * time.Second)
// since 10 is the width and we don't know the max bytes
// it will do a infinite scrolling.
bar.Add(11)

// Output:
// -  (11 B/s) [1s]

Output

-  (11 B/s) [1s]

func OptionShowCount

go
func OptionShowCount() Option

OptionShowCount will also print current count out of total

Example (Minuscule)

go
bar := NewOptions(10000, OptionSetWidth(10), OptionShowCount(), OptionSetPredictTime(false))
bar.Add(1)
// Output:
// 0% |          | (1/10000)

Output

0% |          | (1/10000)

func OptionShowDescriptionAtLineEnd

go
func OptionShowDescriptionAtLineEnd() Option

OptionShowDescriptionAtLineEnd defines whether description should be written at line end instead of line start

Example

go
bar := NewOptions(100, OptionSetWidth(10), OptionShowDescriptionAtLineEnd(), OptionSetDescription("hello"))
_ = bar.Add(10)
// Output:
// 10% |█         |  [0s:0s] hello

Output

10% |█         |  [0s:0s] hello

Example (Spinner)

go
bar := NewOptions(-1, OptionSetWidth(10), OptionShowDescriptionAtLineEnd(), OptionSetDescription("hello"))
_ = bar.Add(1)
// Output:
// |  [0s] hello

Output

|  [0s] hello

func OptionShowElapsedTimeOnFinish

go
func OptionShowElapsedTimeOnFinish() Option

OptionShowElapsedOnFinish will keep the display of elapsed time on finish

func OptionShowIts

go
func OptionShowIts() Option

OptionShowIts will also print the iterations/second

Example

go
bar := NewOptions(100, OptionSetWidth(10), OptionShowIts(), OptionSetPredictTime(false))
bar.Reset()
time.Sleep(1 * time.Second)
bar.Add(10)
// Output:
// 10% |█         | (10 it/s)

Output

10% |█         | (10 it/s)

Example (Count)

go
bar := NewOptions(100, OptionSetWidth(10), OptionShowIts(), OptionShowCount())
bar.Reset()
time.Sleep(1 * time.Second)
bar.Add(10)
// Output:
// 10% |█         | (10/100, 10 it/s) [1s:9s]

Output

10% |█         | (10/100, 10 it/s) [1s:9s]

Example (Spinner)

go
/*
 Spinner test with iteration count and iteration rate
*/
bar := NewOptions(-1,
	OptionSetWidth(10),
	OptionShowIts(),
	OptionShowCount(),
)
bar.Reset()
time.Sleep(1 * time.Second)
bar.Add(5)

// Output:
// -  (5/-, 5 it/s) [1s]

Output

-  (5/-, 5 it/s) [1s]

func OptionSpinnerCustom

go
func OptionSpinnerCustom(spinner []string) Option

OptionSpinnerCustom sets the spinner used for indeterminate bars to the passed slice of string

func OptionSpinnerType

go
func OptionSpinnerType(spinnerType int) Option

OptionSpinnerType sets the type of spinner used for indeterminate bars

func OptionThrottle

go
func OptionThrottle(duration time.Duration) Option

OptionThrottle will wait the specified duration before updating again. The default duration is 0 seconds.

Example

go
bar := NewOptions(100, OptionSetWidth(10), OptionThrottle(100*time.Millisecond))
bar.Reset()
bar.Add(5)
time.Sleep(150 * time.Millisecond)
bar.Add(5)
bar.Add(10)
// Output:
// 10% |█         |  [0s:1s]

Output

10% |█         |  [0s:1s]

func OptionUseANSICodes

go
func OptionUseANSICodes(val bool) Option

OptionUseANSICodes will use more optimized terminal i/o.

Only useful in environments with support for ANSI escape sequences.

type ProgressBar

ProgressBar is a thread-safe, simple progress bar

go
type ProgressBar struct {
    // contains filtered or unexported fields
}
Example

go
bar := New(100)
bar.Add(10)
// Output:
// 10% |████                                    |  [0s:0s]

Output

10% |████                                    |  [0s:0s]

Example (Basic)

go
bar := NewOptions(100, OptionSetWidth(10))
bar.Reset()
time.Sleep(1 * time.Second)
bar.Add(10)
// Output:
// 10% |█         |  [1s:9s]

Output

10% |█         |  [1s:9s]

Example (Invisible)

go
bar := NewOptions(100, OptionSetWidth(10), OptionSetRenderBlankState(true), OptionSetVisibility(false))
bar.Reset()
fmt.Println("hello, world")
time.Sleep(1 * time.Second)
bar.Add(10)
// Output:
// hello, world

Output

hello, world

func Default

go
func Default(max int64, description ...string) *ProgressBar

Default provides a progressbar with recommended defaults. Set max to -1 to use as a spinner.

Example

go
bar := Default(100)
for i := 0; i < 50; i++ {
	bar.Add(1)
	time.Sleep(10 * time.Millisecond)
}
// Output:
//

Output

func DefaultBytes

go
func DefaultBytes(maxBytes int64, description ...string) *ProgressBar

DefaultBytes provides a progressbar to measure byte throughput with recommended defaults. Set maxBytes to -1 to use as a spinner.

func DefaultBytesSilent

go
func DefaultBytesSilent(maxBytes int64, description ...string) *ProgressBar

DefaultBytesSilent is the same as DefaultBytes, but does not output anywhere. String() can be used to get the output instead.

func DefaultSilent

go
func DefaultSilent(max int64, description ...string) *ProgressBar

DefaultSilent is the same as Default, but does not output anywhere. String() can be used to get the output instead.

func New

go
func New(max int) *ProgressBar

New returns a new ProgressBar with the specified maximum

func New64

go
func New64(max int64) *ProgressBar

New64 returns a new ProgressBar with the specified maximum

func NewOptions

go
func NewOptions(max int, options ...Option) *ProgressBar

NewOptions constructs a new instance of ProgressBar, with any options you specify

func NewOptions64

go
func NewOptions64(max int64, options ...Option) *ProgressBar

NewOptions64 constructs a new instance of ProgressBar, with any options you specify

func (*ProgressBar) Add

go
func (p *ProgressBar) Add(num int) error

Add will add the specified amount to the progressbar

func (*ProgressBar) Add64

go
func (p *ProgressBar) Add64(num int64) error

Add64 will add the specified amount to the progressbar

func (*ProgressBar) ChangeMax

go
func (p *ProgressBar) ChangeMax(newMax int)

ChangeMax takes in a int and changes the max value of the progress bar

Example

go
bar := NewOptions(100, OptionSetWidth(10), OptionSetPredictTime(false))
bar.ChangeMax(50)
bar.Add(50)
// Output:
// 100% |██████████|

Output

100% |██████████|

func (*ProgressBar) ChangeMax64

go
func (p *ProgressBar) ChangeMax64(newMax int64)

ChangeMax64 is basically the same as ChangeMax, but takes in a int64 to avoid casting

func (*ProgressBar) Clear

go
func (p *ProgressBar) Clear() error

Clear erases the progress bar from the current line

func (*ProgressBar) Close

go
func (p *ProgressBar) Close() (err error)

func (*ProgressBar) Describe

go
func (p *ProgressBar) Describe(description string)

Describe will change the description shown before the progress, which can be changed on the fly (as for a slow running process).

func (*ProgressBar) Exit

go
func (p *ProgressBar) Exit() error

Exit will exit the bar to keep current state

func (*ProgressBar) Finish

go
func (p *ProgressBar) Finish() error

Finish will fill the bar to full

Example

go
bar := NewOptions(100, OptionSetWidth(10))
bar.Finish()
// Output:
// 100% |██████████|

Output

100% |██████████|

func (*ProgressBar) GetMax

go
func (p *ProgressBar) GetMax() int

GetMax returns the max of a bar

func (*ProgressBar) GetMax64

go
func (p *ProgressBar) GetMax64() int64

GetMax64 returns the current max

func (*ProgressBar) IsFinished

go
func (p *ProgressBar) IsFinished() bool

IsFinished returns true if progress bar is completed

func (*ProgressBar) Read

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

Read implement io.Reader

func (*ProgressBar) RenderBlank

go
func (p *ProgressBar) RenderBlank() error

RenderBlank renders the current bar state, you can use this to render a 0% state

func (*ProgressBar) Reset

go
func (p *ProgressBar) Reset()

Reset will reset the clock that is used to calculate current time and the time left.

func (*ProgressBar) Set

go
func (p *ProgressBar) Set(num int) error

Set will set the bar to a current number

Example

go
bar := New(100)
bar.Set(10)
// Output:
// 10% |████                                    |  [0s:0s]

Output

10% |████                                    |  [0s:0s]

func (*ProgressBar) Set64

go
func (p *ProgressBar) Set64(num int64) error

Set64 will set the bar to a current number

Example

go
bar := New(100)
bar.Set64(10)
// Output:
// 10% |████                                    |  [0s:0s]

Output

10% |████                                    |  [0s:0s]

func (*ProgressBar) State

go
func (p *ProgressBar) State() State

State returns the current state

func (*ProgressBar) String

go
func (p *ProgressBar) String() string

String returns the current rendered version of the progress bar. It will never return an empty string while the progress bar is running.

func (*ProgressBar) Write

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

Write implement io.Writer

type Reader

Reader is the progressbar io.Reader struct

go
type Reader struct {
    io.Reader
    // contains filtered or unexported fields
}

func NewReader

go
func NewReader(r io.Reader, bar *ProgressBar) Reader

NewReader return a new Reader with a given progress bar.

func (*Reader) Close

go
func (r *Reader) Close() (err error)

Close the reader when it implements io.Closer

func (*Reader) Read

go
func (r *Reader) Read(p []byte) (n int, err error)

Read will read the data and add the number of bytes to the progressbar

type State

State is the basic properties of the bar

go
type State struct {
    CurrentPercent float64
    CurrentBytes   float64
    SecondsSince   float64
    SecondsLeft    float64
    KBsPerSecond   float64
}

type Theme

Theme defines the elements of the bar

go
type Theme struct {
    Saucer        string
    AltSaucerHead string
    SaucerHead    string
    SaucerPadding string
    BarStart      string
    BarEnd        string
}

Generated by gomarkdoc