From 77237be0e23dfaba4d4d97a525c3ecc7fb819042 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Mon, 28 Jul 2014 12:01:43 -0400 Subject: [PATCH 1/2] progress bar: don't strings.Repeat a negative number Docker-DCO-1.1-Signed-off-by: Vincent Batts (github: vbatts) --- utils/jsonmessage.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/utils/jsonmessage.go b/utils/jsonmessage.go index 7ad4548c38e77..e22d06eccd719 100644 --- a/utils/jsonmessage.go +++ b/utils/jsonmessage.go @@ -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) From 704b97d1c4cc6eaa6ecf9630eed2dffd5abaffb9 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Mon, 28 Jul 2014 14:43:28 -0400 Subject: [PATCH 2/2] jsonmessage: added test and cleaned up others Docker-DCO-1.1-Signed-off-by: Vincent Batts (github: vbatts) --- utils/jsonmessage_test.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/utils/jsonmessage_test.go b/utils/jsonmessage_test.go index ecf1896762024..0ce9492c9818a 100644 --- a/utils/jsonmessage_test.go +++ b/utils/jsonmessage_test.go @@ -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()) } }