pbars

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2018 License: MIT Imports: 9 Imported by: 1

README

pbars |██████████▏ |

GoDoc Build

// setup the printer
pp := pbars.NewProgressPrinter("My Title", 50, true)

// go!
for i := 0; i < 400; i++ {
    pp.Update(int64(i+1), 400)
    time.Sleep(16 * time.Millisecond)
}

// new line required afterwards :)
pp.Done()

Example

There's an example in the /example directory which does the following:

progress gif

* cursor flickering is just the high (16ms) update rate and the way the gif was rendered

Features

  • UTF8 blocks vs pure ASCII (sometimes a terminal doesn't support utf8 chars)
pbars.NewProgressPrinter("My Title", 50, true)   // utf8 mode
pbars.NewProgressPrinter("My Title", 50, false)   // ascii mode
  • When writing to Non-TTY output streams like files or those that don't support the \r character, you can set ProgressPrinter{}.NonTTY = true. This will disable the progress bar until the Done function is called upon which it will print the final progress bar, rate, and elapsed time as normal.

  • Uses the overall units per second and elapsed time once you call Done

  • Customisable unit formats

By default the progress bar rate is formatted as 'units' per second. But often you'll want a measure of bytes or bits. The ProgressPrinter struct allows you to set the UnitFunc to be whatever you want as long as it looks like func(v float64) string. An example is the pbars.ByteFormatFunc that will convert to B, KB, MB etc.

  • Interruptf method for printing messages while the progress bar continues (see the example)

  • Clear method for clearing and removing the progress bar once you no longer need it

Documentation

Overview

Package pbars provides cli progress bars for long running tasks.

It is quick to get going with and should be customisable to your hearts content.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ByteFormatFunc

func ByteFormatFunc(v float64) string

ByteFormatFunc formats bytes into B, KB, MB, GB..

func DrawBarASCII

func DrawBarASCII(perc float32, width int) string

DrawBarASCII prints a bar that looks like '[===============.....]' it should be compatible with any terminal but can only print whole characters and so has slightly less resolution than others.

func DrawBarUTF8

func DrawBarUTF8(perc float32, width int) string

DrawBarUTF8 prints a bar using the utf8 block characters. Because UTF8 has fractional block characters, this bar has a high resolution and is effective with less width than the ascii counterpart.

func FormatDuration

func FormatDuration(d time.Duration) string

FormatDuration is the same as Duration.String() but with a tweak for the number of decimal places. This implementation allows up to 2 decimals for all fields rather than the 6 or 9 that the standard version prints.

func NoUnitFunc

func NoUnitFunc(v float64) string

NoUnitFunc simply formats the value as a string, no unit conversion is performed

Types

type BarDrawerFunction

type BarDrawerFunction func(progress float32, width int) string

BarDrawerFunction is a function that takes in the progress value (0.0 -> 1.0) and a width and returns a string containing the progress bar. It does not handle any other information.

type NaiveRateWatcher

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

func NewNaiveRateWatcher

func NewNaiveRateWatcher() *NaiveRateWatcher

func (*NaiveRateWatcher) EstimatedRemaining

func (nw *NaiveRateWatcher) EstimatedRemaining() time.Duration

func (*NaiveRateWatcher) EstimatedUnitsPerSecond

func (nw *NaiveRateWatcher) EstimatedUnitsPerSecond() float32

func (*NaiveRateWatcher) HasEstimate

func (nw *NaiveRateWatcher) HasEstimate() bool

func (*NaiveRateWatcher) OverallElapsed

func (nw *NaiveRateWatcher) OverallElapsed() time.Duration

func (*NaiveRateWatcher) OverallUnitsPerSecond

func (nw *NaiveRateWatcher) OverallUnitsPerSecond() float32

func (*NaiveRateWatcher) PercentageComplete

func (nw *NaiveRateWatcher) PercentageComplete() float32

func (*NaiveRateWatcher) String

func (nw *NaiveRateWatcher) String() string

func (*NaiveRateWatcher) Update

func (nw *NaiveRateWatcher) Update(position, length int64)

type ProgressPrinter

type ProgressPrinter struct {

	// Title is the string printed to the left of the progress bar. It may be blank,
	// and it may contain utf8 characters. It is not included in the width.
	Title string

	// Output is the output writer to print the progress bar to. This is usually os.Stdout,
	// but for tests and certain other cases can be overriden or be nil.
	Output io.Writer

	// Width is the width of the actual bar in the progress bar.
	Width int

	// ShowPercentage sets whether the percentage should be visible on the right hand side.
	ShowPercentage bool

	// ShowRate sets whether the rate in units per second should be visible on the right hand side.
	ShowRate bool

	// ShowTimeEstimate sets whether the estimate of the remaining time should be visible on the right hand side.
	ShowTimeEstimate bool

	// UnitFunc is the formatter applied to the rate value in order to format it to subject-specific units.
	UnitFunc UnitFormatFunc

	// RateWatcher is a model that is used to track the rate, percentage, and time estimates. You probably don't
	// want to change this.
	Ratewatcher RateWatcher

	// Bardrawer is the function used to turn the percentage and width into a progress bar. This can be overriden to
	// pick between utf8 or ascii or to implement your own bar style.
	Bardrawer BarDrawerFunction

	// NonTTY can be used to set the progress bar into a nontty mode when writing output to files. The bar will be printed
	// only once when Done is called, and interrupt messages will function as normal.
	NonTTY bool
	// contains filtered or unexported fields
}

ProgressPrinter is the model that represents a single progress bar and is responsible for printing it to the terminal correctly. It ties together all of the formatting, rate calculations, and bar drawing.

func NewProgressPrinter

func NewProgressPrinter(title string, width int, utf8 bool) *ProgressPrinter

NewProgressPrinter constructs a progress bar printer for most use cases

func (*ProgressPrinter) Clear

func (pp *ProgressPrinter) Clear()

Clear just clears the line using a \r character and spaces.

func (*ProgressPrinter) Done

func (pp *ProgressPrinter) Done()

Done reprints the bar using the overall rate and overall elapsed time and terminates the bar with a newline.

func (*ProgressPrinter) Interruptf

func (pp *ProgressPrinter) Interruptf(format string, args ...interface{})

Interruptf interrupts the bar enough to print a message and then reprint the bar on the line below.

func (*ProgressPrinter) Reprint

func (pp *ProgressPrinter) Reprint()

Reprint will reprint the progress bar in place. You probably don't want to call this but its public just in case.

func (*ProgressPrinter) Update

func (pp *ProgressPrinter) Update(position, length int64)

Update updates the progress bar, possibly redrawing it if the position has changed and there is and output stream attached.

type ProgressReceiver

type ProgressReceiver interface {

	// Update performs some work based on the current progres.
	Update(progress, length int64)
}

ProgressReceiver is the interface implemented by many of the objects in this package. It is simply a thing that can receive progress updates.

type RateWatcher

type RateWatcher interface {
	ProgressReceiver
	fmt.Stringer

	// PercentageComplete should return a float between 0.0 and 1.0
	PercentageComplete() float32

	// HasEstimate
	HasEstimate() bool

	// EstimatedUnitsPerSecond should return a value representing the number of units progressing per second
	EstimatedUnitsPerSecond() float32

	// EstimatedRemaining should return an estimated time remaining
	EstimatedRemaining() time.Duration

	// OverallUnitsPerSecond should return the overall rate seen by the watcher. Pretty much length / elapsed.
	OverallUnitsPerSecond() float32

	// OverallElapsed should return the time since the bar was first seen by the user
	OverallElapsed() time.Duration
}

RateWatcher presents an object that can watch the accumulation of progress updates and report the estimated rate of completion.

type UnitFormatFunc

type UnitFormatFunc func(v float64) string

UnitFormatFunc is a function that takes in some decimal number of units and converts it to the appropriate unit.

type UpdateAveragingRateWatcher

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

func NewUpdateAveragingRateWatcher

func NewUpdateAveragingRateWatcher(window int) *UpdateAveragingRateWatcher

func (*UpdateAveragingRateWatcher) EstimatedRemaining

func (nw *UpdateAveragingRateWatcher) EstimatedRemaining() time.Duration

func (*UpdateAveragingRateWatcher) EstimatedUnitsPerSecond

func (nw *UpdateAveragingRateWatcher) EstimatedUnitsPerSecond() float32

func (*UpdateAveragingRateWatcher) HasEstimate

func (nw *UpdateAveragingRateWatcher) HasEstimate() bool

func (*UpdateAveragingRateWatcher) OverallElapsed

func (nw *UpdateAveragingRateWatcher) OverallElapsed() time.Duration

func (*UpdateAveragingRateWatcher) OverallUnitsPerSecond

func (nw *UpdateAveragingRateWatcher) OverallUnitsPerSecond() float32

func (*UpdateAveragingRateWatcher) PercentageComplete

func (nw *UpdateAveragingRateWatcher) PercentageComplete() float32

func (*UpdateAveragingRateWatcher) String

func (nw *UpdateAveragingRateWatcher) String() string

func (*UpdateAveragingRateWatcher) Update

func (nw *UpdateAveragingRateWatcher) Update(position, length int64)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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