Skip to content

Commit

Permalink
Merge pull request moby#7274 from vbatts/vbatts-gh7136
Browse files Browse the repository at this point in the history
progress bar: don't strings.Repeat a negative number
  • Loading branch information
vieux committed Jul 28, 2014
2 parents f906f31 + 704b97d commit a0dad3d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
7 changes: 6 additions & 1 deletion utils/jsonmessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ func (p *JSONProgress) String() string {
total := units.HumanSize(int64(p.Total))
percentage := int(float64(p.Current)/float64(p.Total)*100) / 2
if width > 110 {
pbBox = fmt.Sprintf("[%s>%s] ", strings.Repeat("=", percentage), strings.Repeat(" ", 50-percentage))
// this number can't be negetive gh#7136
numSpaces := 0
if 50-percentage > 0 {
numSpaces = 50 - percentage
}
pbBox = fmt.Sprintf("[%s>%s] ", strings.Repeat("=", percentage), strings.Repeat(" ", numSpaces))
}
numbersBox = fmt.Sprintf("%8v/%v", current, total)

Expand Down
17 changes: 13 additions & 4 deletions utils/jsonmessage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,22 @@ func TestProgress(t *testing.T) {
t.Fatalf("Expected empty string, got '%s'", jp.String())
}

expected := " 1 B"
jp2 := JSONProgress{Current: 1}
if jp2.String() != " 1 B" {
t.Fatalf("Expected ' 1 B', got '%s'", jp2.String())
if jp2.String() != expected {
t.Fatalf("Expected %q, got %q", expected, jp2.String())
}

expected = "[=========================> ] 50 B/100 B"
jp3 := JSONProgress{Current: 50, Total: 100}
if jp3.String() != "[=========================> ] 50 B/100 B" {
t.Fatalf("Expected '[=========================> ] 50 B/100 B', got '%s'", jp3.String())
if jp3.String() != expected {
t.Fatalf("Expected %q, got %q", expected, jp3.String())
}

// this number can't be negetive gh#7136
expected = "[==============================================================>] 50 B/40 B"
jp4 := JSONProgress{Current: 50, Total: 40}
if jp4.String() != expected {
t.Fatalf("Expected %q, got %q", expected, jp4.String())
}
}

0 comments on commit a0dad3d

Please sign in to comment.