~poldi1405/go-indicators

093b67e031c5840f00878563012c1a9585faaf25 — Moritz Poldrack 1 year, 9 months ago 10b8797
combined spinners and progress in the example
obviously the blocks couldn't just work…
2 files changed, 36 insertions(+), 14 deletions(-)

M main.go
M progress/type.go
M main.go => main.go +33 -14
@@ 4,35 4,54 @@ import (
	"fmt"
	"strings"
	"time"
	"unicode/utf8"

	"./progress"
	"./spinner"
)

func main() {
	/*	basicexample()
		fmt.Println(strings.Repeat("=", 20))
		multispinner()
		fmt.Println(strings.Repeat("=", 20))
		repeatedspinner()
		fmt.Println(strings.Repeat("=", 20))
		preparationspinner()
	*/
	basicexample()
	fmt.Println(strings.Repeat("=", 20))
	multispinner()
	fmt.Println(strings.Repeat("=", 20))
	repeatedspinner()
	fmt.Println(strings.Repeat("=", 20))
	preparationspinner()
	fmt.Println(strings.Repeat("=", 20))
	var p progress.Progress
	p.Width = 90
	p.Width = 30
	p.Style = "trapez"

	for i := 0; i < 350; i++ {
		perc, _ := progress.GetPercentage(float64(i), 350)
		fmt.Printf(" [%v] [%3.f%%]\r", p.GetBar(float64(i), 350), perc)
		bar := p.GetBar(float64(i), 350)
		if utf8.RuneCountInString(bar) != p.Width {
			panic(fmt.Sprintf("%v%% (%v/%v) leads to %v characters while %v characters are wanted\n|%v|\n|%v|\n", perc, i, 350, utf8.RuneCountInString(bar), p.Width, strings.Repeat("=", p.Width), bar))
		}
		time.Sleep(5 * time.Millisecond)
	}
	fmt.Println()
	fmt.Println(strings.Repeat("=", 20))
	var s spinner.Spinner
	s.SetStyle("bouncing-bar")
	for i := 0; i < 450; i++ {
		if i < 100 {
			fmt.Printf("\r%v", s.Next())

		} else {
			perc, _ := progress.GetPercentage(float64(i-100), 350)
			fmt.Printf("\r[%3.f%%] %v", perc, p.GetBar(float64(i-100), 350))
		}
		time.Sleep(50 * time.Millisecond)
	}
	fmt.Println()
	fmt.Println(strings.Repeat("=", 20))
	p.Width = 20
	p.Style = "block"
	s.SetStyle("braille")
	for i := 0; i < 350; i++ {
		perc, _ := progress.GetPercentage(float64(i-100), 350)
		fmt.Printf("\r%v %v %3.f%%", s.Next(), p.GetBar(float64(i-100), 350), perc)
		time.Sleep(50 * time.Millisecond)
	}
	fmt.Println()
}

func basicexample() {

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

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


@@ 45,10 46,12 @@ func (p *Progress) GetBar(parts, total float64) (result string) {

	charindex := 0
	dingeling := percperchar / float64(maxIndex+1)
	temp := math.Floor(percent / dingeling)
	for percent > dingeling {
		percent -= dingeling
		charindex++
	}
	charindex = int(temp)

	if charindex > 0 {
		result += ProgressStyles[p.Style][charindex]