progress

package module
v0.0.0-...-67b5d51 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2021 License: MIT Imports: 4 Imported by: 2

README

import "go.tianon.xyz/progress"

GoDoc

A simple Go progress bar library inspired by the default IncrementalBar from the PyPi progress package.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultPhases = []string{
	" ",
	"▏",
	"▎",
	"▍",
	"▌",
	"▋",
	"▊",
	"▉",
	"█",
}

Default value for "Phases" provided by NewBar. This default allows for sub-character progress precision.

Functions

func TermWidth

func TermWidth(out *os.File) int

TermWidth returns the width of terminal "out" or -1 if it is not a terminal or if the dimensions of it cannot be determined.

Types

type Bar

type Bar struct {
	Val int64
	Min int64
	Max int64

	// Functions invoked by TickString for generating the text before/after a given progress string.
	Prefix func(b *Bar) string
	Suffix func(b *Bar) string

	// The set of characters used for the progress bar display.
	// The first phase is the implied "empty" and the final phase is the implied "full".
	// See DefaultPhases for the default value provided by NewBar.
	// Note: TickString assumes each of these represents a single output character.
	Phases []string

	Out *os.File
}

Bar is the type representing a standard progress bar. It can be used either via TickString (for explicit string generation) or Start/Tick/Finish for direct terminal/os.File output.

Example
package main

import (
	"os"
	"time"

	"go.tianon.xyz/progress"
)

func main() {
	bar := progress.NewBar(os.Stdout)
	bar.Max = 100

	bar.Start()
	for bar.Val = bar.Min; bar.Val <= bar.Max; bar.Val += 25 {
		bar.Tick()
		time.Sleep(time.Second)
	}
	bar.Finish()
}
Output:

Example (TickString)
package main

import (
	"fmt"

	"go.tianon.xyz/progress"
)

func main() {
	bar := progress.NewBar(nil)
	bar.Min = -100
	bar.Max = 100

	bar.Prefix = func(_ *progress.Bar) string {
		return "["
	}
	bar.Suffix = func(b *progress.Bar) string {
		return fmt.Sprintf("]%3.0f%%", b.Progress()*100)
	}

	bar.Phases = []string{
		" ",
		"-",
		"=",
	}

	for bar.Val = bar.Min; bar.Val <= bar.Max; bar.Val += 25 {
		fmt.Println(bar.TickString(8))
	}

}
Output:

[  ]  0%
[  ] 12%
[- ] 25%
[- ] 38%
[= ] 50%
[= ] 62%
[=-] 75%
[=-] 88%
[==]100%
Example (TickStringManyPhases)
package main

import (
	"fmt"

	"go.tianon.xyz/progress"
)

func main() {
	bar := progress.NewBar(nil)
	bar.Min = -100
	bar.Max = 100

	bar.Prefix = func(_ *progress.Bar) string {
		return "["
	}
	bar.Suffix = func(b *progress.Bar) string {
		return fmt.Sprintf("]%3.0f%%", b.Progress()*100)
	}

	bar.Phases = []string{
		"0",
		"1",
		"2",
		"3",
		"4",
		"5",
		"6",
		"7",
		"8",
		"9",
	}

	for bar.Val = bar.Min; bar.Val <= bar.Max; bar.Val += 5 {
		fmt.Println(bar.TickString(10))
	}

}
Output:

[0000]  0%
[0000]  2%
[1000]  5%
[2000]  8%
[3000] 10%
[4000] 12%
[5000] 15%
[6000] 18%
[7000] 20%
[8000] 22%
[9000] 25%
[9000] 28%
[9100] 30%
[9200] 32%
[9300] 35%
[9400] 38%
[9500] 40%
[9600] 42%
[9700] 45%
[9800] 48%
[9900] 50%
[9900] 52%
[9910] 55%
[9920] 57%
[9930] 60%
[9940] 62%
[9950] 65%
[9960] 68%
[9970] 70%
[9980] 72%
[9990] 75%
[9990] 78%
[9991] 80%
[9992] 82%
[9993] 85%
[9994] 88%
[9995] 90%
[9996] 92%
[9997] 95%
[9998] 98%
[9999]100%

func NewBar

func NewBar(out *os.File) *Bar

NewBar creates a new progress bar targeted at "out" (can be "nil" if "TickString" use is intended instead of Start/Tick/Finish).

func (*Bar) Finish

func (b *Bar) Finish()

Finish completes progress bar output (invokes Tick and writes "\n").

func (*Bar) Progress

func (b *Bar) Progress() float64

Progress returns the current percentage (Val along the line Min <-> Max), normalized to 0-100% as a 0.0-1.0 float64.

The following special cases apply:

Min >= Max: 100%
Val <  Min:   0%
Val >  Max: 100%

func (*Bar) Start

func (b *Bar) Start()

Start begins progress bar output (invokes Tick).

func (*Bar) Tick

func (b *Bar) Tick()

Tick updates progress bar output.

func (*Bar) TickString

func (b *Bar) TickString(width int) string

TickString returns a current progress bar string of "width" (possibly more depending on whether "Prefix" and "Suffix" take all available space).

func (*Bar) TickWidth

func (b *Bar) TickWidth() int

TickWidth returns the available width for the entire progress bar (or 80 if the available width cannot be determined).

Jump to

Keyboard shortcuts

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