~poldi1405/go-indicators

f18acc771195b3232d286362fb37b5cf038a00b0 — Moritz Poldrack 5 months ago 6d07ab3 v1.1.0
allowed all numeric types in progress bars
3 files changed, 66 insertions(+), 9 deletions(-)

M progress/calculate.go
M progress/type.go
M spinner/type.go
M progress/calculate.go => progress/calculate.go +58 -3
@@ 1,12 1,67 @@
package progress

import "errors"
import (
	"errors"
	"fmt"
)

// GetPercentage returns the percentage of a part from a total.
func GetPercentage(parts, total float64) (float64, error) {
func GetPercentage(parts, total interface{}) (float64, error) {
	var fparts, ftotal float64

	switch parts.(type) { // {{{ type switch for parts
	case uint8:
		fparts = float64(parts.(uint8))
	case int8:
		fparts = float64(parts.(int8))
	case uint16:
		fparts = float64(parts.(uint16))
	case int16:
		fparts = float64(parts.(int16))
	case uint32:
		fparts = float64(parts.(uint32))
	case int32:
		fparts = float64(parts.(int32))
	case uint64:
		fparts = float64(parts.(uint64))
	case int64:
		fparts = float64(parts.(int64))
	case float32:
		fparts = float64(parts.(float32))
	case float64:
		fparts = parts.(float64)
	default:
		return 0, fmt.Errorf("type %T is not supported", parts)
	} // }}}

	switch total.(type) { // {{{ type switch for total
	case uint8:
		ftotal = float64(total.(uint8))
	case int8:
		ftotal = float64(total.(int8))
	case uint16:
		ftotal = float64(total.(uint16))
	case int16:
		ftotal = float64(total.(int16))
	case uint32:
		ftotal = float64(total.(uint32))
	case int32:
		ftotal = float64(total.(int32))
	case uint64:
		ftotal = float64(total.(uint64))
	case int64:
		ftotal = float64(total.(int64))
	case float32:
		ftotal = float64(total.(float32))
	case float64:
		ftotal = total.(float64)
	default:
		return 0, fmt.Errorf("type %T is not supported", total)
	} // }}}

	if total == 0 {
		return 0, errors.New("division by zero")
	}

	return parts / total * 100, nil
	return fparts / ftotal * 100, nil
}

M progress/type.go => progress/type.go +3 -2
@@ 35,8 35,9 @@ func (p *Progress) SetStyle(style string) {
}

// 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) {
// for the given progress. If the parameters are not numeric, the percentage
// will be 0
func (p *Progress) GetBar(parts, total interface{}) (result string) {
	p.loadStyleIfEmpty()
	if p.Width <= 0 {
		p.Width = 10

M spinner/type.go => spinner/type.go +5 -4
@@ 50,8 50,9 @@ func (s *Spinner) SetStyle(style string) {

	s.style = style
	spinnerStyleMtx.RLock()
	defer spinnerStyleMtx.RUnlock()

	s.spinChars = SpinnerStyles[s.style]
	spinnerStyleMtx.RUnlock()
}

// Clear returns the amount of characters for the first spinner-state in spaces


@@ 63,13 64,13 @@ func (s *Spinner) Clear() string {
// loadIfUnloaded makes sure a style is always loaded.
func (s *Spinner) loadIfUnloaded() {
	s.spinCharsMtx.Lock()
	defer s.spinCharsMtx.Unlock()
	if len(s.spinChars) > 0 {
		s.spinCharsMtx.Unlock()
		return
	}

	spinnerStyleMtx.RLock()
	defer spinnerStyleMtx.RUnlock()

	s.spinChars = SpinnerStyles[""]
	s.spinCharsMtx.Unlock()
	spinnerStyleMtx.RUnlock()
}