uiprogress: github.com/gosuri/uiprogress Index | Examples | Files | Directories

package uiprogress

import "github.com/gosuri/uiprogress"

Package uiprogress is a library to render progress bars in terminal applications

Code:

uiprogress.Start()            // start rendering
bar := uiprogress.AddBar(100) // Add a new bar

// optionally, append and prepend completion and elapsed time
bar.AppendCompleted()
bar.PrependElapsed()

for bar.Incr() {
    time.Sleep(time.Millisecond * 20)
}

Index

Examples

Package Files

bar.go doc.go progress.go

Variables

var (
    // Fill is the default character representing completed progress
    Fill byte = '='

    // Head is the default character that moves when progress is updated
    Head byte = '>'

    // Empty is the default character that represents the empty progress
    Empty byte = '-'

    // LeftEnd is the default character in the left most part of the progress indicator
    LeftEnd byte = '['

    // RightEnd is the default character in the right most part of the progress indicator
    RightEnd byte = ']'

    // Width is the default width of the progress bar
    Width = 70

    // ErrMaxCurrentReached is error when trying to set current value that exceeds the total value
    ErrMaxCurrentReached = errors.New("errors: current value is greater total value")
)
var Out = os.Stdout

Out is the default writer to render progress bars to

var RefreshInterval = time.Millisecond * 10

RefreshInterval in the default time duration to wait for refreshing the output

func Listen Uses

func Listen()

Listen listens for updates and renders the progress bars

func Start Uses

func Start()

Start starts the rendering the progress of progress bars using the DefaultProgress. It listens for updates using `bar.Set(n)` and new bars when added using `AddBar`

func Stop Uses

func Stop()

Stop stops listening

type Bar Uses

type Bar struct {
    // Total of the total  for the progress bar
    Total int

    // LeftEnd is character in the left most part of the progress indicator. Defaults to '['
    LeftEnd byte

    // RightEnd is character in the right most part of the progress indicator. Defaults to ']'
    RightEnd byte

    // Fill is the character representing completed progress. Defaults to '='
    Fill byte

    // Head is the character that moves when progress is updated.  Defaults to '>'
    Head byte

    // Empty is the character that represents the empty progress. Default is '-'
    Empty byte

    // TimeStated is time progress began
    TimeStarted time.Time

    // Width is the width of the progress bar
    Width int
    // contains filtered or unexported fields
}

Bar represents a progress bar

func AddBar Uses

func AddBar(total int) *Bar

AddBar creates a new progress bar and adds it to the default progress container

func NewBar Uses

func NewBar(total int) *Bar

NewBar returns a new progress bar

func (*Bar) AppendCompleted Uses

func (b *Bar) AppendCompleted() *Bar

AppendCompleted appends the completion percent to the progress bar

func (*Bar) AppendElapsed Uses

func (b *Bar) AppendElapsed() *Bar

AppendElapsed appends the time elapsed the be progress bar

func (*Bar) AppendFunc Uses

func (b *Bar) AppendFunc(f DecoratorFunc) *Bar

AppendFunc runs the decorator function and renders the output on the right of the progress bar

func (*Bar) Bytes Uses

func (b *Bar) Bytes() []byte

Bytes returns the byte presentation of the progress bar

func (*Bar) CompletedPercent Uses

func (b *Bar) CompletedPercent() float64

CompletedPercent return the percent completed

func (*Bar) CompletedPercentString Uses

func (b *Bar) CompletedPercentString() string

CompletedPercentString returns the formatted string representation of the completed percent

func (*Bar) Current Uses

func (b *Bar) Current() int

Current returns the current progress of the bar

func (*Bar) Incr Uses

func (b *Bar) Incr() bool

Incr increments the current value by 1, time elapsed to current time and returns true. It returns false if the cursor has reached or exceeds total value.

Code:

runtime.GOMAXPROCS(runtime.NumCPU()) // use all available cpu cores

// create a new bar and prepend the task progress to the bar
count := 1000
bar := uiprogress.AddBar(count).AppendCompleted().PrependElapsed()
bar.PrependFunc(func(b *uiprogress.Bar) string {
    return fmt.Sprintf("Task (%d/%d)", b.Current(), count)
})

uiprogress.Start()
var wg sync.WaitGroup

// fanout into 1k go routines
for i := 0; i < count; i++ {
    wg.Add(1)
    go func() {
        defer wg.Done()
        time.Sleep(time.Millisecond * time.Duration(rand.Intn(500)))
        bar.Incr()
    }()
}
time.Sleep(time.Second) // wait for a second for all the go routines to finish
wg.Wait()
uiprogress.Stop()

func (*Bar) PrependCompleted Uses

func (b *Bar) PrependCompleted() *Bar

PrependCompleted prepends the precent completed to the progress bar

func (*Bar) PrependElapsed Uses

func (b *Bar) PrependElapsed() *Bar

PrependElapsed prepends the time elapsed to the begining of the bar

func (*Bar) PrependFunc Uses

func (b *Bar) PrependFunc(f DecoratorFunc) *Bar

PrependFunc runs decorator function and render the output left the progress bar

func (*Bar) Set Uses

func (b *Bar) Set(n int) error

Set the current count of the bar. It returns ErrMaxCurrentReached when trying n exceeds the total value. This is atomic operation and concurancy safe.

func (*Bar) String Uses

func (b *Bar) String() string

String returns the string representation of the bar

func (*Bar) TimeElapsed Uses

func (b *Bar) TimeElapsed() time.Duration

TimeElapsed returns the time elapsed

func (*Bar) TimeElapsedString Uses

func (b *Bar) TimeElapsedString() string

TimeElapsedString returns the formatted string represenation of the time elapsed

type DecoratorFunc Uses

type DecoratorFunc func(b *Bar) string

DecoratorFunc is a function that can be prepended and appended to the progress bar

Code:

var steps = []string{"downloading source", "installing deps", "compiling", "packaging", "seeding database", "deploying", "staring servers"}
bar := uiprogress.AddBar(len(steps))

// prepend the current step to the bar
bar.PrependFunc(func(b *uiprogress.Bar) string {
    return "app: " + steps[b.Current()-1]
})

for bar.Incr() {
    time.Sleep(time.Millisecond)
}

type Progress Uses

type Progress struct {
    // Out is the writer to render progress bars to
    Out io.Writer

    // Width is the width of the progress bars
    Width int

    // Bars is the collection of progress bars
    Bars []*Bar

    // RefreshInterval in the time duration to wait for refreshing the output
    RefreshInterval time.Duration
    // contains filtered or unexported fields
}

Progress represents the container that renders progress bars

func New Uses

func New() *Progress

New returns a new progress bar with defaults

func (*Progress) AddBar Uses

func (p *Progress) AddBar(total int) *Bar

AddBar creates a new progress bar and adds to the container

Code:

waitTime := time.Millisecond * 100
uiprogress.Start()
// start the progress bars in go routines
var wg sync.WaitGroup

bar1 := uiprogress.AddBar(20).AppendCompleted().PrependElapsed()
wg.Add(1)
go func() {
    defer wg.Done()
    for bar1.Incr() {
        time.Sleep(waitTime)
    }
}()

bar2 := uiprogress.AddBar(40).AppendCompleted().PrependElapsed()
wg.Add(1)
go func() {
    defer wg.Done()
    for bar2.Incr() {
        time.Sleep(waitTime)
    }
}()

time.Sleep(time.Second)
bar3 := uiprogress.AddBar(20).PrependElapsed().AppendCompleted()
wg.Add(1)
go func() {
    defer wg.Done()
    for i := 1; i <= bar3.Total; i++ {
        bar3.Set(i)
        time.Sleep(waitTime)
    }
}()
// wait for all the go routines to finish
wg.Wait()

func (*Progress) Bypass Uses

func (p *Progress) Bypass() io.Writer

Bypass returns a writer which allows non-buffered data to be written to the underlying output

func (*Progress) Listen Uses

func (p *Progress) Listen()

Listen listens for updates and renders the progress bars

func (*Progress) SetOut Uses

func (p *Progress) SetOut(o io.Writer)

func (*Progress) SetRefreshInterval Uses

func (p *Progress) SetRefreshInterval(interval time.Duration)

func (*Progress) Start Uses

func (p *Progress) Start()

Start starts the rendering the progress of progress bars. It listens for updates using `bar.Set(n)` and new bars when added using `AddBar`

func (*Progress) Stop Uses

func (p *Progress) Stop()

Stop stops listening

Directories

PathSynopsis
util/strutilPackage strutil provides various utilities for manipulating strings

Package uiprogress imports 9 packages (graph) and is imported by 51 packages. Updated 2017-11-10. Refresh now. Tools for package owners.