From 4d677ee3894c28190c424c9359aae3c6aedae13d Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Wed, 15 Nov 2023 14:52:21 -0800 Subject: [PATCH] no divide by zero --- progress/bar.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/progress/bar.go b/progress/bar.go index 9a81fd0f..f69fb87b 100644 --- a/progress/bar.go +++ b/progress/bar.go @@ -105,7 +105,12 @@ func (b *Bar) percent() float64 { } func (b *Bar) rate() float64 { - return (float64(b.currentValue) - float64(b.initialValue)) / b.elapsed().Seconds() + elapsed := b.elapsed() + if elapsed.Seconds() > 0 { + return (float64(b.currentValue) - float64(b.initialValue)) / elapsed.Seconds() + } + + return 0 } func (b *Bar) elapsed() time.Duration {