bar

package module
v0.0.0-...-bbc214b Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2020 License: ISC Imports: 9 Imported by: 2

README

bar

Travis (.org)

This package is a port of the progress package, written in Golang.

terminal demo

Installation

go get github.com/superhawk610/bar github.com/superhawk610/terminal

Getting Started

package main

import (
	"time"

	"github.com/superhawk610/bar"
)

func main() {
	n := 20
	b := bar.New(30)

	for i := 0; i < n; i++ {
		b.Tick()
		time.Sleep(500 * time.Millisecond)
	}

	b.Done()
}

There are additional examples of more advanced usage in the examples directory.

Colored Output

This package works well with color libraries like ttacon/chalk. In order to get the output displayed in the GIF above, you'd use it like so:

package main

import (
	"time"

  "github.com/superhawk610/bar"
  "github.com/ttacon/chalk"
)

func main() {
	n := 20
	b := bar.New(
    bar.WithDimensions(20, 20),
    bar.WithFormat(
      fmt.Sprintf(
        " %sloading...%s :percent :bar %s:rate ops/s%s ",
        chalk.Blue,
        chalk.Reset,
        chalk.Green,
        chalk.Reset,
      )
    )
  )

	for i := 0; i < n; i++ {
		b.Tick()
		time.Sleep(500 * time.Millisecond)
	}

	b.Done()
}

Configuration

This package uses the functional options pattern to support incremental configuration. To create a new instance of bar with options, use the bar.NewWithOpts function and provide any number of configuration augments (listed below).

WithCallback(cb func())

Provide a callback function to be executed when the bar is completed via b.Done().

WithDisplay(start, complete, head, incomplete, end string)

Provide display characters to be used when outputting the bar to the terminal.

[ xxx >    ]
| |   | |  |- end
| |   | |- incomplete
| |   |- head
| |- complete
|- start
WithDimensions(total, width int)

Provide dimensions for the total value of the progress bar and its output width.

WithOutput(out Output)

Provide an output stream for displaying the progress bar. Output is essentially an io.Writer, but it also exposes a ClearLine() function to clear the current line of output and return the cursor to the first index. By default, this uses os.Stdout.

WithContext(ctx Context)

Provide an initial value for the bar's context (read more about how to use context with custom verbs below).

WithFormat(f string)

Provide an ordering of verbs to be used when outputting the progress bar. You can choose from the standard included verbs :bar, :progress, :rate, and :eta, or you can provide your own verbs using the Ctx helper. Verbs must always be prefixed with :.

Standard Verbs

The following verbs are included:

:bar

Output the progress bar visual.

[------>           ]
:percent

Output the total progress percentage.

38.4%
:rate

Output the total progress rate (in completed ticks per second).

2.1 ops/s

NOTE: This verb does not display a unit by default, so you'll need to provide your own units (eg - ops/s).

:eta

Output the estimated time remaining before completion (formatted by time.Duration.String()).

16m28s

Until the bar has established a rate of progress, this verb won't display anything.

Custom Verbs

You can provide your own verbs when defining a format. Custom verbs must be prefixed with a colon :. You may not use any of the standard verbs as custom verbs.

First, include the verb when defining your format:

b := bar.NewWithOpts(
	bar.WithDimensions(n, 30),
	bar.WithFormat(" :bar :hello :world "),
)

You'll also probably want to include a default value for each custom verb using the WithContext helper mentioned above.

Then, whenever ticking or updating your progress bar, provide a Context slice with the value(s) you'd like to have displayed in their place. Use the Ctx helper to clean up the syntax:

b.TickAndUpdate(bar.Context{
	bar.Ctx("hello", "Hello,"),
	bar.Ctx("world", "world!"),
})

This will produce the following output:

[----->            ] Hello, world!

WithDebug()

Debugging crowded layouts can be difficult, so this helper swaps each bar component's print() method for its debug() method, displaying its internal state and type.

 <barToken p={4} t={10}> <percentToken "40.0%"> <customVerbToken verb="hello" value="Hello!">

License

Copyright © 2019 Aaron Ross, all rights reserved. View the ISC license here.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClearLine

func ClearLine()

ClearLine clears the current line in the terminal

func InitTerminal

func InitTerminal()

func Overwritef

func Overwritef(format string, s ...interface{})

Overwritef clears the current line in the terminal and then performs a fmt.Printf() with the provided args

func WithCallback

func WithCallback(cb func()) augment

WithCallback augments an options constructor by setting a callback

func WithContext

func WithContext(ctx Context) augment

WithContext augments an options constructor by setting the initial values for the bar's context

func WithDebug

func WithDebug() augment

WithDebug augments an options constructor by setting the internal debug flag to true; this will display the list of internal tokens recognized on each Tick/Update in place of the standard output

func WithDimensions

func WithDimensions(total, width int) augment

WithDimensions augments an options constructor by customizing the bar's width and total

func WithDisplay

func WithDisplay(start, complete, head, incomplete, end string) augment

WithDisplay augments an options constructor by customizing terminal output characters

[ xxx > ] | | | | |- end | | | |- incomplete | | |- head | |- complete |- start

func WithFormat

func WithFormat(f string) augment

WithFormat augments an options constructor by customizing the bar's output format

func WithLines

func WithLines(lines int) augment

WithLines augments an options constructor by setting a lines

func WithOutput

func WithOutput(out Output) augment

WithOutput augments an options constructor by setting the output stream

Types

type Bar

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

Bar is a progress bar to be used for displaying task progress via terminal output

func New

func New(t int) *Bar

New creates a new instance of bar.Bar with the given total and returns a reference to it

func NewWithFormat

func NewWithFormat(t int, f string) *Bar

NewWithFormat creates a new instance of bar.Bar with the given total and format and returns a reference to it

func NewWithOpts

func NewWithOpts(opts ...func(o *barOpts)) *Bar

NewWithOpts creates a new instance of bar.Bar with the provided options and returns a reference to it

func (*Bar) Done

func (b *Bar) Done()

Done finalizes the bar and prints it followed by a new line

func (*Bar) GetLines

func (b *Bar) GetLines() int

func (*Bar) Interrupt

func (b *Bar) Interrupt(s string)

Interrupt prints s above the bar

func (*Bar) Interruptf

func (b *Bar) Interruptf(format string, s ...interface{})

Interruptf passes the given input to fmt.Sprintf and prints it above the bar

func (*Bar) SetFormat

func (b *Bar) SetFormat(f string)

func (*Bar) SetLines

func (b *Bar) SetLines(lines int)

SetLines

func (*Bar) String

func (b *Bar) String() string

func (*Bar) Tick

func (b *Bar) Tick()

Tick increments the bar's progress by 1

func (*Bar) TickAndUpdate

func (b *Bar) TickAndUpdate(ctx Context)

TickAndUpdate is a helper function for calling Tick followed by Update

func (*Bar) Update

func (b *Bar) Update(progress int, ctx Context)

Update sets the bar's progress to an arbitrary value and optionally updates the bar's context

type Context

type Context []*ContextValue

Context is a wrapper type for a slice of ContextValues

type ContextValue

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

ContextValue is a tuple that defines a substitution for a custom verb

func Ctx

func Ctx(verb string, value interface{}) *ContextValue

Ctx is a helper for creating a ContextValue tuple

type Output

type Output interface {
	ClearLine()
	Printf(format string, vals ...interface{})
}

Output is a stand-in for a basic io.Writer that also exposes a ClearLine() function to clear the current line and return the cursor to the first index

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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