~poldi1405/go-indicators

ee3f4765fcb88a2159dadd62f1cf62107c5b9b4f — Moritz Poldrack 1 year, 9 months ago a6bc81f
added progress documentation
fixed the name of the parallelogram progress-style
fixed default style not being loaded
set default as fallback in case of invalid style
5 files changed, 40 insertions(+), 10 deletions(-)

M progress/doc.go
A progress/doc_test.go
M progress/styles.go
M progress/type.go
M spinner/type.go
M progress/doc.go => progress/doc.go +2 -2
@@ 1,3 1,3 @@
// Package progress allows easily creating progressbars with multiple
// intermediary stages, like UTF-8 blocks.
// Package progress allows to easily create a progressbar by simply providing
// the total and the completed parts.
package progress

A progress/doc_test.go => progress/doc_test.go +20 -0
@@ 0,0 1,20 @@
package progress

// The easiest way to get a progressbar is as follows
func Example() {
	var p Progress
	p.GetBar(275, 346)
}

// The easiest way to get a progressbar is as follows
func Example_styled() {
	var p Progress
	p.SetStyle("")
	p.GetBar(275, 346)
}

// The easiest way to get a progressbar is as follows
func Example() {
	var p Progress
	p.GetBar(275, 346)
}

M progress/styles.go => progress/styles.go +1 -1
@@ 8,7 8,7 @@ var ProgressStyles = map[string][]string{
	"double":        []string{" ", "="},
	"double-":       []string{" ", "-", "="},
	"single":        []string{" ", "-"},
	"trapez":        []string{"▱", "▰"},
	"parallelogram": []string{"▱", "▰"},
	"spaced-blocks": []string{"▯", "▮"},
	"block":         []string{" ", "▏", "▎", "▍", "▌", "▋", "▊", "▉", "█"},
	"":              []string{" ", "="},

M progress/type.go => progress/type.go +16 -6
@@ 1,7 1,6 @@
package progress

import (
	"errors"
	"math"
	"strings"
	"sync"


@@ 17,28 16,28 @@ type Progress struct {
}

// SetStyle sets the style that should be used with the progressbar
func (p *Progress) SetStyle(style string) error {
	if style == p.style {
		return nil
func (p *Progress) SetStyle(style string) {
	if style == p.style && style != "" {
		return
	}
	progressStyleMtx.RLock()
	defer progressStyleMtx.RUnlock()

	prgc, exists := ProgressStyles[style]
	if !exists {
		return errors.New("undefined style")
		prgc = ProgressStyles[""]
	}

	p.prgCharsMtx.Lock()
	p.prgChars = prgc
	p.prgCharsMtx.Unlock()
	p.style = style
	return nil
}

// GetBar returns the string that represents the progressbar in the set style
// for the given progress
func (p *Progress) GetBar(parts, total float64) (result string) {
	p.loadStyleIfEmpty()
	if p.Width <= 0 {
		p.Width = 10
	}


@@ 94,3 93,14 @@ func (p *Progress) GetBar(parts, total float64) (result string) {
	}
	return
}

func (p *Progress) loadStyleIfEmpty() {
	p.prgCharsMtx.RLock()
	if len(p.prgChars) > 0 {
		p.prgCharsMtx.RUnlock()
		return
	}
	p.prgCharsMtx.RUnlock()

	p.SetStyle(p.style)
}

M spinner/type.go => spinner/type.go +1 -1
@@ 42,7 42,7 @@ func (s *Spinner) Current() string {

// SetStyle loads a style into the spinner
func (s *Spinner) SetStyle(style string) {
	if style == s.style {
	if style == s.style && style != "" {
		return
	}
	s.spinCharsMtx.Lock()