yanprogress

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2024 License: MIT Imports: 13 Imported by: 0

README

yanprogress

Yet ANother Progress Bar 😄

Dead simple no-frills progress bar that writes by default to stderr.

There are two types

  • Bounded - where the total number of iterations to process is known in advance. This will produce a bar output like this

    [==============>               ]  50% (19.7 it/s)
    
  • Unbounded - where the total number of iterations to process is not known in advance. This will produce a spinner output like this

    ⠋ (19.8 it/s)
    

If the terminal is not a tty (destination is a file or redirected), then it will produce sequential output like this, without the percentage if unbounded.

  9% (18.0 it/s)
 19% (19.0 it/s)
 28% (19.3 it/s)
 39% (19.5 it/s)
 49% (19.6 it/s)

You may optionally add a status to say what it is doing. This can be called at any point to update the status line, in which case it looks like this

Frobbing the turnips
[==============>               ]  50% (19.7 it/s)

Examples

Bounded progress bar. To use unbounded set the first argument to zero

bar := NewProgressBar(100, 500*time.Millisecond)
bar.SetStatus("Downloading File")
bar.Start()

// Simulate work
for i := 0; i <= 100; i++ {
    time.Sleep(50 * time.Millisecond)
    bar.Inc()

    if i == 50 {
        bar.SetStatus("Half way there!")
    }
}
bar.Complete()

Write to stdout

bar := NewProgressBar(100, 500*time.Millisecond, WithWriter(os.Stdout))

Documentation

Overview

Package yanprogress provides Yet ANother Progressbar!

This is a simple one that provides a bar when the total value is known, or a spinner when it is not. It also detects whether the output device is a real tty. If it is not, then output is line by line suitable for log files or environments that capture the terminal streams e.g. containers or CI systems.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetCursorPosition

func GetCursorPosition() (int, int, error)

GetCursorPosition retrieves the cursor position

func WithWriter

func WithWriter(w io.Writer) optionFunc

WithWriter sets the output stream for the bar (default os.Stderr)

Types

type ProgressBar

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

func NewProgressBar

func NewProgressBar(max uint64, redrawInterval time.Duration, opts ...optionFunc) *ProgressBar

NewProgressBar creates a new progress bar.

Set max to zero for an unbounded spinner, else it is the value that represents 100% complete.

func (*ProgressBar) Complete

func (p *ProgressBar) Complete()

Complete marks the progress as complete and shows the cursor.

It also cancels the goroutine responsible for redrawing the bar.

func (*ProgressBar) Inc

func (p *ProgressBar) Inc()

Inc increments the progress bar's value by one.

func (*ProgressBar) Pause

func (p *ProgressBar) Pause()

Pause puases redrawing of the bar, e.g. to accept console input

func (*ProgressBar) Resume

func (p *ProgressBar) Resume()

Resume resumes redrawing of the bar

func (*ProgressBar) Set

func (p *ProgressBar) Set(value uint64)

Set sets the progress bar's value to a specific value.

func (*ProgressBar) SetStatus

func (p *ProgressBar) SetStatus(status string)

SetStatus sets the status line for the progress bar.

Use this to indicate to users what is currently happening.

func (*ProgressBar) Start

func (p *ProgressBar) Start()

Start hides the cursor and starts drawing the progress bar.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL