From e56687fa8f946cb5328c219eee96ad3e265638cc Mon Sep 17 00:00:00 2001 From: Andy Walker Date: Tue, 28 Sep 2021 23:02:24 -0400 Subject: [PATCH] Force redraw on Describe() Fixes #109 --- progressbar.go | 2 +- progressbar_test.go | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/progressbar.go b/progressbar.go index 4de3c50..15ecf35 100644 --- a/progressbar.go +++ b/progressbar.go @@ -515,6 +515,7 @@ func (p *ProgressBar) Clear() error { // can be changed on the fly (as for a slow running process). func (p *ProgressBar) Describe(description string) { p.config.description = description + p.RenderBlank() } // New64 returns a new ProgressBar @@ -679,7 +680,6 @@ func renderProgressBar(c config, s *state) (int, error) { bytesString += fmt.Sprintf("%s/%s%s", currentHumanize, c.maxHumanized, c.maxHumanizedSuffix) } else { bytesString += fmt.Sprintf("%s%s/%s%s", currentHumanize, currentSuffix, c.maxHumanized, c.maxHumanizedSuffix) - } } else { bytesString += fmt.Sprintf("%.0f/%d", s.currentBytes, c.max) diff --git a/progressbar_test.go b/progressbar_test.go index e0de534..77e21c3 100644 --- a/progressbar_test.go +++ b/progressbar_test.go @@ -9,6 +9,7 @@ import ( "io/ioutil" "net/http" "os" + "strconv" "strings" "sync" "testing" @@ -87,6 +88,7 @@ func ExampleOptionClearOnFinish() { // Output: // Finished } + func ExampleProgressBar_Finish() { bar := NewOptions(100, OptionSetWidth(10), OptionSetRenderBlankState(false)) bar.Finish() @@ -502,7 +504,6 @@ func TestConcurrency(t *testing.T) { } func TestIterationNames(t *testing.T) { - b := Default(20) tc := b.config @@ -526,12 +527,13 @@ func md5sum(r io.Reader) (string, error) { return hex.EncodeToString(hash.Sum(nil)), err } -func ExampleProgressBar_Describe() { - bar := NewOptions(100, OptionSetWidth(10), OptionSetRenderBlankState(false)) - bar.Reset() - time.Sleep(1 * time.Second) - bar.Describe("performing axial adjustements") +func TestProgressBar_Describe(t *testing.T) { + buf := strings.Builder{} + bar := NewOptions(100, OptionSetWidth(10), OptionSetRenderBlankState(false), OptionSetWriter(&buf)) + bar.Describe("performing axial adjustments") bar.Add(10) - // Output: - // performing axial adjustements 10% |█ | [1s:9s] + rawBuf := strconv.QuoteToASCII(buf.String()) + if rawBuf != `"\r\r\rperforming axial adjustments 0% | | [0s:0s]\r \r\rperforming axial adjustments 10% |\u2588 | [0s:0s]"` { + t.Errorf("wrong string: %s", rawBuf) + } }