Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: divided by 0 #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions bar.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ type DecoratorFunc func(b *Bar) string

// NewBar returns a new progress bar
func NewBar(total int) *Bar {
if total <= 0 {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if total < 0, maybe panic or not modify to 0

total = 0
}
return &Bar{
Total: total,
Width: Width,
Expand Down Expand Up @@ -216,6 +219,9 @@ func (b *Bar) String() string {

// CompletedPercent return the percent completed
func (b *Bar) CompletedPercent() float64 {
if b.Total <= 0 {
return 100.00
}
return (float64(b.Current()) / float64(b.Total)) * 100.00
}

Expand Down
8 changes: 8 additions & 0 deletions bar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import (
"time"
)

func TestBarZero(t *testing.T) {
b := NewBar(0)
b.PrependCompleted()
if !strings.Contains(b.String(), "100") {
t.Fatal("want", "100%", "in", b.String())
}
}

func TestBarPrepend(t *testing.T) {
b := NewBar(100)
b.PrependCompleted()
Expand Down