diff --git a/bar.go b/bar.go index 1a4c66f..13bda22 100644 --- a/bar.go +++ b/bar.go @@ -69,6 +69,7 @@ trimSpace bool toComplete bool completeFlushed bool + ignoreComplete bool noPop bool aDecorators []decor.Decorator pDecorators []decor.Decorator @@ -170,17 +171,18 @@ } // SetTotal sets total dynamically. -// If total is less or equal to zero it takes progress' current value. -// If complete is true, complete event will be triggered. +// If total is less than or equal to zero it takes progress' current value. +// A complete flag enables or disables complete event on `current >= total`. func (b *Bar) SetTotal(total int64, complete bool) { select { case b.operateState <- func(s *bState) { + s.ignoreComplete = !complete if total <= 0 { s.total = s.current } else { s.total = total } - if complete && !s.toComplete { + if !s.ignoreComplete && !s.toComplete { s.current = s.total s.toComplete = true go b.refreshTillShutdown() @@ -197,7 +199,7 @@ s.iterated = true s.lastN = current - s.current s.current = current - if s.total > 0 && s.current >= s.total { + if !s.ignoreComplete && s.current >= s.total { s.current = s.total s.toComplete = true go b.refreshTillShutdown() @@ -224,7 +226,7 @@ s.iterated = true s.lastN = n s.current += n - if s.total > 0 && s.current >= s.total { + if !s.ignoreComplete && s.current >= s.total { s.current = s.total s.toComplete = true go b.refreshTillShutdown() diff --git a/internal/percentage.go b/internal/percentage.go index 7e261cb..e321e0a 100644 --- a/internal/percentage.go +++ b/internal/percentage.go @@ -7,6 +7,9 @@ if total <= 0 { return 0 } + if current >= total { + return float64(width) + } return float64(int64(width)*current) / float64(total) } diff --git a/internal/percentage_test.go b/internal/percentage_test.go index a2fa3f7..6d5410d 100644 --- a/internal/percentage_test.go +++ b/internal/percentage_test.go @@ -21,8 +21,7 @@ {"t,c,e{100,50,50}", 100, 50, 50}, {"t,c,e{100,99,99}", 100, 99, 99}, {"t,c,e{100,100,100}", 100, 100, 100}, - {"t,c,e{100,101,101}", 100, 101, 101}, - {"t,c,e{100,102,101}", 100, 102, 102}, + {"t,c,e{100,101,101}", 100, 101, 100}, {"t,c,e{120,0,0}", 120, 0, 0}, {"t,c,e{120,10,8}", 120, 10, 8}, {"t,c,e{120,15,13}", 120, 15, 13}, @@ -33,8 +32,7 @@ {"t,c,e{120,118,98}", 120, 118, 98}, {"t,c,e{120,119,99}", 120, 119, 99}, {"t,c,e{120,120,100}", 120, 120, 100}, - {"t,c,e{120,121,101}", 120, 121, 101}, - {"t,c,e{120,122,101}", 120, 122, 102}, + {"t,c,e{120,121,101}", 120, 121, 100}, }, 80: { {"t,c,e{-1,-1,0}", -1, -1, 0}, @@ -47,8 +45,7 @@ {"t,c,e{100,50,40}", 100, 50, 40}, {"t,c,e{100,99,79}", 100, 99, 79}, {"t,c,e{100,100,80}", 100, 100, 80}, - {"t,c,e{100,101,81}", 100, 101, 81}, - {"t,c,e{100,102,82}", 100, 102, 82}, + {"t,c,e{100,101,81}", 100, 101, 80}, {"t,c,e{120,0,0}", 120, 0, 0}, {"t,c,e{120,10,7}", 120, 10, 7}, {"t,c,e{120,15,10}", 120, 15, 10}, @@ -59,8 +56,7 @@ {"t,c,e{120,118,79}", 120, 118, 79}, {"t,c,e{120,119,79}", 120, 119, 79}, {"t,c,e{120,120,80}", 120, 120, 80}, - {"t,c,e{120,121,81}", 120, 121, 81}, - {"t,c,e{120,122,81}", 120, 122, 81}, + {"t,c,e{120,121,81}", 120, 121, 80}, }, }