goprogressbar

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2020 License: MIT Imports: 9 Imported by: 64

README

goprogressbar

Latest Release Build Status Coverage Status Go ReportCard GoDoc

Golang helper to print one or many progress bars on the console

Installation

Make sure you have a working Go environment (Go 1.9 or higher is required). See the install instructions.

To install goprogressbar, simply run:

go get github.com/muesli/goprogressbar

Example

package main

import (
	"fmt"
	"strconv"
	"time"

	"github.com/muesli/goprogressbar"
)

func main() {
	mpb := goprogressbar.MultiProgressBar{}

	for i := 0; i < 10; i++ {
		pb := &goprogressbar.ProgressBar{
			Text:    "Progress " + strconv.FormatInt(int64(i+1), 10),
			Total:   100,
			Current: 0,
			Width:   60,
		}

		mpb.AddProgressBar(pb)
	}

	pb := &goprogressbar.ProgressBar{
		Text:    "Overall Progress",
		Total:   1000,
		Current: 0,
		Width:   60,
	}
	mpb.AddProgressBar(pb)

	// fill progress bars one after another
	for j := 0; j < 10; j++ {
		for i := 1; i <= 100; i++ {
			p := mpb.ProgressBars[j]
			p.Current = int64(i)
			p.RightAlignedText = fmt.Sprintf("%d of %d", i, p.Total)

			pb.Current++

			mpb.LazyPrint()
			time.Sleep(23 * time.Millisecond)
		}
	}

	fmt.Println()
}

What it looks like

Progress 1                  100 of 100 [#################################################] 100.00%
Progress 2                  100 of 100 [#################################################] 100.00%
Progress 3                   89 of 100 [###########################################>-----]  89.00%
Progress 4                             [#>-----------------------------------------------]   0.00%
Progress 5                             [#>-----------------------------------------------]   0.00%
Progress 6                             [#>-----------------------------------------------]   0.00%
Progress 7                             [#>-----------------------------------------------]   0.00%
Progress 8                             [#>-----------------------------------------------]   0.00%
Progress 9                             [#>-----------------------------------------------]   0.00%
Progress 10                            [#>-----------------------------------------------]   0.00%
Overall Progress                       [#############>-----------------------------------]  28.90%

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Stdout defines where output gets printed to
	Stdout io.Writer = os.Stdout
	// BarFormat defines the bar design
	BarFormat = "[#>-]"
)

Functions

This section is empty.

Types

type MultiProgressBar

type MultiProgressBar struct {
	ProgressBars []*ProgressBar
	// contains filtered or unexported fields
}

MultiProgressBar is a helper for printing multiple progress bars

func (*MultiProgressBar) AddProgressBar

func (mp *MultiProgressBar) AddProgressBar(p *ProgressBar)

AddProgressBar adds another progress bar to the multi struct

func (*MultiProgressBar) LazyPrint

func (mp *MultiProgressBar) LazyPrint()

LazyPrint writes all progress bars to stdout if a significant update occurred

func (*MultiProgressBar) Print

func (mp *MultiProgressBar) Print()

Print writes all progress bars to stdout

type ProgressBar

type ProgressBar struct {
	// Text displayed on the very left
	Text string
	// Text prepending the bar
	PrependText string
	// Max value (100%)
	Total int64
	// Current progress value
	Current int64
	// Desired bar width
	Width uint

	// If a PrependTextFunc is set, the PrependText will be automatically
	// generated on every print
	PrependTextFunc func(p *ProgressBar) string
	// contains filtered or unexported fields
}

ProgressBar is a helper for printing a progress bar

func (*ProgressBar) Clear added in v0.2.0

func (p *ProgressBar) Clear()

Clear deletes everything on the current terminal line, hence removing a printed progressbar

func (*ProgressBar) LazyPrint

func (p *ProgressBar) LazyPrint()

LazyPrint writes the progress bar to stdout if a significant update occurred

func (*ProgressBar) Print

func (p *ProgressBar) Print()

Print writes the progress bar to stdout

func (*ProgressBar) UpdateRequired

func (p *ProgressBar) UpdateRequired() bool

UpdateRequired returns true when this progressbar wants an update regardless of fps limitation

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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