~poldi1405/go-indicators

23e4aa3828b7cd206b366c3fc29df5c4ad85ec82 — Moritz Poldrack 1 year, 9 months ago 922b32f
so the algorithm is junk, not applicable for partial progress
3 files changed, 37 insertions(+), 17 deletions(-)

M main.go
M progress/styles.go
M progress/type.go
M main.go => main.go +5 -1
@@ 18,11 18,15 @@ func main() {
		preparationspinner()
	*/
	var p progress.Progress
	p.Width = 30
	p.Style = "block"

	for i := 0; i < 350; i++ {
		fmt.Printf(" %v\r", p.GetBar(float64(i), 300))
		perc, _ := progress.GetPercentage(float64(i), 350)
		fmt.Printf(" [%v] [%3.f%%]\r", p.GetBar(float64(i), 350), perc)
		time.Sleep(50 * time.Millisecond)
	}
	fmt.Println()
}

func basicexample() {

M progress/styles.go => progress/styles.go +1 -1
@@ 2,7 2,7 @@ package progress

var ProgressStyles = map[string][]string{
	"double":        []string{" ", "="},
	"double-":       []string{" ", "=", "-"},
	"double-":       []string{" ", "-", "="},
	"single":        []string{" ", "-"},
	"trapez":        []string{"▱", "▰"},
	"spaced-blocks": []string{"▯", "▮"},

M progress/type.go => progress/type.go +31 -15
@@ 1,37 1,53 @@
package progress

import (
	"fmt"
	"math"
	"strings"
	"sync"
)

type Progress struct {
	style string
	width int
	Style string
	Width int

	mtx sync.Mutex
}

func (p *Progress) GetBar(parts, total float64) (result string) {
	maxIndex := len(ProgressStyles[p.style]) - 1
	if p.Width <= 0 {
		p.Width = 10
	}
	maxIndex := len(ProgressStyles[p.Style]) - 1
	if total == 0 {
		result += strings.Repeat(ProgressStyles[p.style][maxIndex], p.width)
		result += strings.Repeat(ProgressStyles[p.Style][maxIndex], p.Width)
		return
	}

	// don't question it, I used excel to come up with this
	percent, _ := GetPercentage(parts, total)
	percentPerCharacter := math.Floor(float64(100) / float64(p.width))
	completeSegments := math.Floor(percent / percentPerCharacter)
	percentRemaining := percent - completeSegments*percentPerCharacter
	percentPerSegment := percentPerCharacter / float64(maxIndex+1)
	// TODO: make this not negative multimillion, maybe we should question it...
	character := int(math.Round(percentRemaining / percentPerSegment))
	a2 := p.Width
	b2, _ := GetPercentage(parts, total)
	c2 := float64(len(ProgressStyles[p.Style]))
	d2 := float64(100 / a2)
	completeSegments := math.Floor(b2 / d2)
	e2 := completeSegments
	f2 := b2 - d2*e2
	g2 := d2 / c2
	segmentIndex := int(math.Floor(f2 / g2))

	if b2 >= 100 {
		result += strings.Repeat(ProgressStyles[p.Style][maxIndex], p.Width)
		return
	}

	result += strings.Repeat(ProgressStyles[p.style][maxIndex], int(completeSegments))
	fmt.Println(completeSegments)
	result += strings.Repeat(ProgressStyles[p.Style][maxIndex], int(completeSegments))

	result += ProgressStyles[p.style][character]
	result += strings.Repeat(ProgressStyles[p.style][0], p.width-int(completeSegments)-1)
	if completeSegments < float64(p.Width) {
		result += ProgressStyles[p.Style][segmentIndex]
	}

	if completeSegments+1 < float64(p.Width) {
		result += strings.Repeat(ProgressStyles[p.Style][0], p.Width-int(completeSegments)-1)
	}
	return
}