Codebase list golang-github-vbauerster-mpb / cd0bf0f
refactoring: internal.Percentage minor refactoring to unify param types Vladimir Bauer 1 year, 11 months ago
3 changed file(s) with 11 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
232232 var tip component
233233 var refilling, filling, padding []byte
234234 var fillCount int
235 curWidth := int(internal.PercentageRound(stat.Total, stat.Current, uint(width)))
235 curWidth := int(internal.PercentageRound(stat.Total, stat.Current, int64(width)))
236236
237237 if curWidth != 0 {
238238 if !stat.Completed || s.tipOnComplete {
242242 }
243243 switch refWidth := 0; {
244244 case stat.Refill != 0:
245 refWidth = int(internal.PercentageRound(stat.Total, stat.Refill, uint(width)))
245 refWidth = int(internal.PercentageRound(stat.Total, stat.Refill, int64(width)))
246246 curWidth -= refWidth
247247 refWidth += curWidth
248248 fallthrough
22 import "math"
33
44 // Percentage is a helper function, to calculate percentage.
5 func Percentage(total, current int64, width uint) float64 {
6 if total <= 0 {
5 func Percentage(total, current, width uint) float64 {
6 if total == 0 {
77 return 0
88 }
99 if current >= total {
1010 return float64(width)
1111 }
12 return float64(int64(width)*current) / float64(total)
12 return float64(width*current) / float64(total)
1313 }
1414
1515 // PercentageRound same as Percentage but with math.Round.
16 func PercentageRound(total, current int64, width uint) float64 {
17 return math.Round(Percentage(total, current, width))
16 func PercentageRound(total, current, width int64) float64 {
17 if total < 0 || current < 0 || width < 0 {
18 return 0
19 }
20 return math.Round(Percentage(uint(total), uint(current), uint(width)))
1821 }
33
44 func TestPercentage(t *testing.T) {
55 // key is barWidth
6 testSuite := map[uint][]struct {
6 testSuite := map[int64][]struct {
77 name string
88 total int64
99 current int64