gogress

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2023 License: GPL-3.0 Imports: 16 Imported by: 0

README

gogress

Simple terminal progress bar with Go.

Features:

  • Customizable progress bar
  • Multi-progress bar
  • Multi-threaded multi-progress bar
  • IO wrapper

Based on cheggaaa/pb

Sample usage - Simple progress bar

package main

import (
	"time"

	"github.com/snakeice/gogress"
)

const TOTAL = 500

func main() {
	bar := gogress.New(TOTAL)
	bar.Start()
	bar.Prefix("Downloading life")
	for i := 1; i <= TOTAL; i++ {
		bar.Inc()
		time.Sleep(time.Second / 120)
	}
	bar.FinishPrint("All Solved")
}

asciicast

Sample decorator

package main

import (
	"time"

	"github.com/snakeice/gogress"
	"github.com/snakeice/gogress/format"
)

const TOTAL = 500

func main() {
	barFormat := format.ProgressFormat{
		BoxStart:   "|",
		BoxEnd:     "|",
		Empty:      "_",
		Current:    ">",
		Completed:  "-",
		SpinString: "\\|/-",
	}

	template := `{{prefix . 2 | green }} {{spin . 1 | rndcolor }}  {{percent . 1 | cyan }} {{counter . 1 | red }} {{speed . 1 | blue}}`

	bar := gogress.New(TOTAL)

	if err := bar.SetTemplate(template); err != nil {
		panic(err)
	}

	bar.Format = barFormat
	bar.Prefix("Processing")

	bar.Start()
	bar.Prefix("Downloading life")
	for i := 1; i <= TOTAL; i++ {
		bar.Inc()
		time.Sleep(time.Second / 120)
	}
	bar.FinishPrint("All Solved")
}

asciicast

Sample usage - Multi-progress bar

package main

import (
	"math/rand"
	"strconv"
	"time"

	"github.com/snakeice/gogress"
)

const (
	TOTAL = 100
	BARS  = 3
)

func processBar(bar *gogress.Progress) {
	for bar.GetMax() > bar.GetCurrent() {
		bar.Inc()
		time.Sleep(time.Millisecond * time.Duration(rand.Intn(100)))
	}

	bar.Finish()
}

func main() {
	pool := gogress.NewPool()
	pool.Start()
	for i := 0; i < BARS; i++ {
		bar := pool.NewBar(TOTAL)
		bar.Prefix("Processing " + strconv.Itoa(i))
		go processBar(bar)
	}

	bar := pool.NewBar(TOTAL)
	bar.Add(50).Prefix("Other processing")
	processBar(bar)
	pool.RemoveBar(bar)

	pool.Wait()

}

asciicast

Documentation

Index

Constants

View Source
const DefaultTemplate = `{{prefix . 2}} {{bar . 5}} {{percent . 1}} {{counter . 1}} {{speed . 1}} {{timeSpent . 1}} {{timeLeft . 1}}`

Variables

View Source
var Colors = template.FuncMap{
	"black":      color.New(color.FgBlack).SprintFunc(),
	"red":        color.New(color.FgRed).SprintFunc(),
	"green":      color.New(color.FgGreen).SprintFunc(),
	"yellow":     color.New(color.FgYellow).SprintFunc(),
	"blue":       color.New(color.FgBlue).SprintFunc(),
	"magenta":    color.New(color.FgMagenta).SprintFunc(),
	"cyan":       color.New(color.FgCyan).SprintFunc(),
	"white":      color.New(color.FgWhite).SprintFunc(),
	"resetcolor": color.New(color.Reset).SprintFunc(),
	"rndcolor":   rndcolor,
}
View Source
var Decorators = template.FuncMap{
	"bar":       adaptativeDecorator(bar),
	"prefix":    wrapDecorator(prefix),
	"counter":   wrapDecorator(counter),
	"timeSpent": wrapDecorator(timeSpent),
	"speed":     wrapDecorator(speed),
	"percent":   wrapDecorator(percent),
	"timeLeft":  wrapDecorator(timeLeft),
	"spin":      wrapDecorator(spin),
	"frameNo":   wrapDecorator(frameNo),
}

Functions

func AddDecorator

func AddDecorator(name string, decorator Decorator)

func RemoveDecorator

func RemoveDecorator(name string)

Types

type Decorator

type Decorator func(frame *FrameContext, cols int) string

type FrameContext

type FrameContext struct {
	Current int64
	Max     int64
	Width   int

	MessagePrefix string
	IsFinish      bool
	// contains filtered or unexported fields
}

func NewFrame

func NewFrame(bar *Progress, current, max int64, width int, frameNo int64) *FrameContext

func (*FrameContext) Copy

func (f *FrameContext) Copy() *FrameContext

func (*FrameContext) Format

func (f *FrameContext) Format() *format.ProgressFormat

func (*FrameContext) FrameNo

func (f *FrameContext) FrameNo() int64

type Pool

type Pool struct {
	RefreshRate time.Duration
	// contains filtered or unexported fields
}

func NewPool

func NewPool() *Pool

func (*Pool) AddBar

func (p *Pool) AddBar(bar *Progress) int

func (*Pool) FinishAll

func (p *Pool) FinishAll()

func (*Pool) IsFinished

func (p *Pool) IsFinished() bool

func (*Pool) NewBar

func (p *Pool) NewBar(max int) *Progress

func (*Pool) NewBar64

func (p *Pool) NewBar64(max int64) *Progress

func (*Pool) NewBarDef

func (p *Pool) NewBarDef() *Progress

func (*Pool) RemoveBar

func (p *Pool) RemoveBar(bar *Progress) int

func (*Pool) Start

func (p *Pool) Start() *Pool

func (*Pool) Update

func (p *Pool) Update()

func (*Pool) Wait added in v1.0.2

func (p *Pool) Wait()

type Progress

type Progress struct {
	ID int

	Previous int64

	RefreshRate time.Duration

	UnitsWidth int

	Output io.Writer

	Width      int
	ForceWidth bool
	Format     format.ProgressFormat
	Units      format.Units
	// contains filtered or unexported fields
}

func New

func New(max int) *Progress

func New64

func New64(max int64) *Progress

func NewDef

func NewDef() *Progress

func (*Progress) Add

func (p *Progress) Add(incSize int) *Progress

func (*Progress) Add64

func (p *Progress) Add64(incSize int64) *Progress

func (*Progress) Finish

func (p *Progress) Finish()

func (*Progress) FinishPrint

func (p *Progress) FinishPrint(str string)

func (*Progress) GetCurrent

func (p *Progress) GetCurrent() int64

func (*Progress) GetMax

func (p *Progress) GetMax() int64

func (*Progress) GetWidth

func (p *Progress) GetWidth() int

func (*Progress) Inc

func (p *Progress) Inc() *Progress

func (*Progress) IsFinished

func (p *Progress) IsFinished() bool

func (*Progress) NewProxyReader

func (pb *Progress) NewProxyReader(r io.Reader) *Reader

func (*Progress) NewProxyWriter

func (pb *Progress) NewProxyWriter(r io.Writer) *Writer

func (*Progress) Prefix

func (p *Progress) Prefix(prefix string) *Progress

func (*Progress) Reset

func (p *Progress) Reset(max int) *Progress

func (*Progress) Reset64

func (p *Progress) Reset64(max int64) *Progress

func (*Progress) ResetTemplate added in v1.0.0

func (pb *Progress) ResetTemplate() error

func (*Progress) Set

func (p *Progress) Set(newValue int) *Progress

func (*Progress) Set64

func (p *Progress) Set64(newValue int64) *Progress

func (*Progress) SetMax

func (p *Progress) SetMax(max int) *Progress

func (*Progress) SetMax64

func (p *Progress) SetMax64(max int64) *Progress

func (*Progress) SetMaxWidth

func (p *Progress) SetMaxWidth(maxWidth int) *Progress

func (*Progress) SetTemplate added in v1.0.0

func (pb *Progress) SetTemplate(template string) error

func (*Progress) SetWidth

func (p *Progress) SetWidth(width int) *Progress

func (*Progress) Start

func (p *Progress) Start() *Progress

func (*Progress) String

func (p *Progress) String() string

func (*Progress) Update

func (p *Progress) Update()

type Reader

type Reader struct {
	io.Reader
	// contains filtered or unexported fields
}

func (*Reader) Close

func (r *Reader) Close() (err error)

func (*Reader) Read

func (r *Reader) Read(p []byte) (n int, err error)

type TemplateParser

type TemplateParser struct {
	// contains filtered or unexported fields
}
 +-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----+
 | C1  | C2  | C3  | C4  | C5  | C6  | C7  | C8  | C9  | C10 | C11 | C12 |
 +-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----+

	Like bootstrap grid system

func NewTemplateParser

func NewTemplateParser(templateString string) (*TemplateParser, error)

func NewTemplateParserEmpty

func NewTemplateParserEmpty() *TemplateParser

func (*TemplateParser) Last

func (tp *TemplateParser) Last() string

func (*TemplateParser) UpdateFrame

func (tp *TemplateParser) UpdateFrame(frame *FrameContext)

func (*TemplateParser) UpdateTemplate

func (tp *TemplateParser) UpdateTemplate(templateString string) error

type Writer

type Writer struct {
	io.Writer
	// contains filtered or unexported fields
}

func (*Writer) Close

func (r *Writer) Close() (err error)

func (*Writer) Write

func (r *Writer) Write(p []byte) (n int, err error)

Directories

Path Synopsis
sample

Jump to

Keyboard shortcuts

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