cmpb

package module
v0.0.0-...-0306ae9 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2018 License: MIT Imports: 8 Imported by: 0

README

cmpb

Build Status Build status Coverage Status Maintainability Go Report Card GoDoc

A color multi-progress bar for Go terminal applications. Works on ANSI compatible terminals... and also on Windows!

Demo

color demo

Demo Source

package main

import (
	"math/rand"
	"time"

	"github.com/fatih/color"
	"github.com/nu11ptr/cmpb"
)

const total = 100

var (
	keys    = []string{"server1000", "server1001", "server1002"}
	actions = []string{"downloading...", "compiling source...", "fetching...", "committing work..."}
)

func main() {
	p := cmpb.New()

	colors := new(cmpb.BarColors)
	colors.Post, colors.KeyDiv, colors.LBracket, colors.RBracket =
		color.HiCyanString, color.HiCyanString, color.HiCyanString, color.HiCyanString
	colors.Key = color.HiBlueString
	colors.Msg, colors.Empty = color.HiYellowString, color.HiYellowString
	colors.Full = color.HiGreenString
	colors.Curr = color.GreenString
	colors.PreBar, colors.PostBar = color.HiMagentaString, color.HiMagentaString

	for _, key := range keys {
		b := p.NewBar(key, total)
		go func() {
			for i := 0; i < total; i++ {
				time.Sleep(time.Duration(rand.Intn(250)) * time.Millisecond)
				action := actions[rand.Intn(len(actions))]
				b.SetMessage(action)
				b.Increment()
			}
		}()
	}

	p.SetColors(colors)
	p.Start()
	p.Wait()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnsiScrollUp

func AnsiScrollUp(rows int, out io.Writer)

AnsiScrollUp uses ANSI escape codes to do the scroll up action

func CalcDur

func CalcDur() func(int, int, time.Time, bool) string

CalcDur calculates the duration since start time and returns a string

func CalcPct

func CalcPct(curr, total int, start time.Time, stopped bool) string

CalcPct calculates the percentage of work complete and returns as a string

func CalcSteps

func CalcSteps(curr, total int, start time.Time, stopped bool) string

CalcSteps calculates the steps completed so far and returns a string

Types

type Bar

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

Bar represents a single progress bar

func (*Bar) GetColors

func (b *Bar) GetColors() BarColors

GetColors returns a copy of the colors used by this bar

func (*Bar) Increment

func (b *Bar) Increment()

Increment updates the current status of the bar by 1

func (*Bar) SetColors

func (b *Bar) SetColors(colors *BarColors)

SetColors sets the colors used to render the bar

func (*Bar) SetMessage

func (b *Bar) SetMessage(msg string)

SetMessage sets the displayed current message

func (*Bar) SetPostBar

func (b *Bar) SetPostBar(f func(int, int, time.Time, bool) string)

SetPostBar sets the postbar function decorator

func (*Bar) SetPreBar

func (b *Bar) SetPreBar(f func(int, int, time.Time, bool) string)

SetPreBar sets the prebar function decorator

func (*Bar) Stop

func (b *Bar) Stop(msg, extMsg string)

Stop stops the updating of the bar and sets a final msg (if not ab empty string)

func (*Bar) String

func (b *Bar) String() string

func (*Bar) Update

func (b *Bar) Update(curr int)

Update updates the current status of the bar

type BarColors

type BarColors struct {
	Post, Key, KeyDiv, Msg, PreBar, LBracket, Empty, Full, Curr, RBracket,
	PostBar, StopMsg, StopExtMsg func(string, ...interface{}) string
}

BarColors represents a structure holding all bar drawing colors

func DefaultColors

func DefaultColors() *BarColors

DefaultColors returns a set of default colors for rendering the bar

func (*BarColors) SetAll

func (b *BarColors) SetAll(f func(string, ...interface{}) string)

SetAll sets all colors to the same color

type Param

type Param struct {
	Interval     time.Duration
	Out          io.Writer
	ScrollUp     func(int, io.Writer)
	InlineExtMsg bool

	PrePad, KeyWidth, MsgWidth, PreBarWidth, BarWidth, PostBarWidth int

	Post                                          string
	KeyDiv, LBracket, RBracket, Empty, Full, Curr rune
}

Param represents the parameters for a Progress

func DefaultParam

func DefaultParam() *Param

DefaultParam builds a Param struct with default values

type Progress

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

Progress represents a collection of progress bars

func New

func New() *Progress

New creates a new progress bar collection with default params

func NewWithParam

func NewWithParam(param *Param) *Progress

NewWithParam creates a new progress bar collection with specified params

func (*Progress) Bar

func (p *Progress) Bar(key string) *Bar

Bar returns the bar stored the given key. The value is nil if it can't be found

func (*Progress) NewBar

func (p *Progress) NewBar(key string, total int) *Bar

NewBar creates a new progress bar and adds it to the progress bar collection

func (*Progress) SetColors

func (p *Progress) SetColors(colors *BarColors)

SetColors sets the colors used to render all the bars part of this progress

func (*Progress) SetPostBar

func (p *Progress) SetPostBar(f func(int, int, time.Time, bool) string)

SetPostBar sets the postbar function decorator

func (*Progress) SetPreBar

func (p *Progress) SetPreBar(f func(int, int, time.Time, bool) string)

SetPreBar sets the prebar function decorator

func (*Progress) Start

func (p *Progress) Start()

Start begins rendering of the progress bars

func (*Progress) Stop

func (p *Progress) Stop(msg, extMsg string)

Stop stops the render of the progress bars assigning a msg (if not any empty string)

func (*Progress) Wait

func (p *Progress) Wait()

Wait waits for progress to be finished or cancelled. It can only be called once

Directories

Path Synopsis
_example
ryg

Jump to

Keyboard shortcuts

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