diff --git a/bar.go b/bar.go index 567dd69..8ebe793 100644 --- a/bar.go +++ b/bar.go @@ -3,7 +3,6 @@ import ( "fmt" "io" - "math" "sync" "time" "unicode/utf8" @@ -379,13 +378,13 @@ // bar width without leftEnd and rightEnd runes barWidth := width - 2 - completedWidth := percentage(total, current, barWidth) + completedWidth := decor.CalcPercentage(total, current, barWidth) buf := make([]byte, 0, width) buf = append(buf, fmtBytes[rLeft]...) if rf != nil { - till := percentage(total, rf.till, barWidth) + till := decor.CalcPercentage(total, rf.till, barWidth) rbytes := make([]byte, utf8.RuneLen(rf.char)) utf8.EncodeRune(rbytes, rf.char) // append refill rune @@ -439,21 +438,6 @@ return fmtBytes } -func percentage(total, current, ratio int) int { - if total == 0 || current > total { - return 0 - } - num := float64(ratio) * float64(current) / float64(total) - ceil := math.Ceil(num) - diff := ceil - num - // num = 2.34 will return 2 - // num = 2.44 will return 3 - if math.Max(diff, 0.6) == diff { - return int(num) - } - return int(ceil) -} - func getSpinner() func() byte { chars := []byte(`-\|/`) repeat := len(chars) - 1 diff --git a/decor/decorators.go b/decor/decorators.go index 6356da3..54a2294 100644 --- a/decor/decorators.go +++ b/decor/decorators.go @@ -131,7 +131,7 @@ } format += "%ds" return func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string { - str := fmt.Sprintf("%d %%", percentage(s.Total, s.Current, 100)) + str := fmt.Sprintf("%d %%", CalcPercentage(s.Total, s.Current, 100)) if (conf & DwidthSync) != 0 { myWidth <- utf8.RuneCountInString(str) max := <-maxWidth @@ -144,11 +144,11 @@ } } -func percentage(total, current, ratio int) int { +func CalcPercentage(total, current, width int) int { if total == 0 || current > total { return 0 } - num := float64(ratio) * float64(current) / float64(total) + num := float64(width) * float64(current) / float64(total) ceil := math.Ceil(num) diff := ceil - num // num = 2.34 will return 2