From b8eacc7af4f6703fd93165e8f6395464059d96e7 Mon Sep 17 00:00:00 2001 From: Nathan Baulch Date: Mon, 25 Jul 2022 15:03:09 +1000 Subject: [PATCH] Fix RenderBlank when throttled --- progressbar.go | 34 +++++++++++++++++++--------------- progressbar_test.go | 23 ++++++++++++++++------- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/progressbar.go b/progressbar.go index b67bb5b..15735e4 100644 --- a/progressbar.go +++ b/progressbar.go @@ -317,7 +317,7 @@ func DefaultBytes(maxBytes int64, description ...string) *ProgressBar { if len(description) > 0 { desc = description[0] } - bar := NewOptions64( + return NewOptions64( maxBytes, OptionSetDescription(desc), OptionSetWriter(os.Stderr), @@ -330,9 +330,8 @@ func DefaultBytes(maxBytes int64, description ...string) *ProgressBar { }), OptionSpinnerType(14), OptionFullWidth(), + OptionSetRenderBlankState(true), ) - bar.RenderBlank() - return bar } // DefaultBytesSilent is the same as DefaultBytes, but does not output anywhere. @@ -344,7 +343,7 @@ func DefaultBytesSilent(maxBytes int64, description ...string) *ProgressBar { if len(description) > 0 { desc = description[0] } - bar := NewOptions64( + return NewOptions64( maxBytes, OptionSetDescription(desc), OptionSetWriter(ioutil.Discard), @@ -355,8 +354,6 @@ func DefaultBytesSilent(maxBytes int64, description ...string) *ProgressBar { OptionSpinnerType(14), OptionFullWidth(), ) - bar.RenderBlank() - return bar } // Default provides a progressbar with recommended defaults. @@ -366,7 +363,7 @@ func Default(max int64, description ...string) *ProgressBar { if len(description) > 0 { desc = description[0] } - bar := NewOptions64( + return NewOptions64( max, OptionSetDescription(desc), OptionSetWriter(os.Stderr), @@ -379,9 +376,8 @@ func Default(max int64, description ...string) *ProgressBar { }), OptionSpinnerType(14), OptionFullWidth(), + OptionSetRenderBlankState(true), ) - bar.RenderBlank() - return bar } // DefaultSilent is the same as Default, but does not output anywhere. @@ -393,7 +389,7 @@ func DefaultSilent(max int64, description ...string) *ProgressBar { if len(description) > 0 { desc = description[0] } - bar := NewOptions64( + return NewOptions64( max, OptionSetDescription(desc), OptionSetWriter(ioutil.Discard), @@ -404,8 +400,6 @@ func DefaultSilent(max int64, description ...string) *ProgressBar { OptionSpinnerType(14), OptionFullWidth(), ) - bar.RenderBlank() - return bar } // String returns the current rendered version of the progress bar. @@ -419,6 +413,9 @@ func (p *ProgressBar) RenderBlank() error { if p.config.invisible { return nil } + if p.state.currentNum == 0 { + p.state.lastShown = time.Time{} + } return p.render() } @@ -517,7 +514,10 @@ 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() + if p.config.invisible { + return + } + p.render() } // New64 returns a new ProgressBar @@ -557,7 +557,7 @@ func (p *ProgressBar) ChangeMax64(newMax int64) { p.Add(0) // re-render } -// IsFinished returns true if progreess bar is completed +// IsFinished returns true if progress bar is completed func (p *ProgressBar) IsFinished() bool { return p.state.finished } @@ -671,7 +671,11 @@ func renderProgressBar(c config, s *state) (int, error) { if len(s.counterLastTenRates) == 0 || s.finished { // if no average samples, or if finished, // then average rate should be the total rate - averageRate = s.currentBytes / time.Since(s.startTime).Seconds() + if t := time.Since(s.startTime).Seconds(); t > 0 { + averageRate = s.currentBytes / t + } else { + averageRate = 0 + } } // show iteration count in "current/total" iterations format diff --git a/progressbar_test.go b/progressbar_test.go index af5b315..7754b30 100644 --- a/progressbar_test.go +++ b/progressbar_test.go @@ -50,7 +50,7 @@ func ExampleProgressBar_Set64() { } func ExampleProgressBar_basic() { - bar := NewOptions(100, OptionSetWidth(10), OptionSetRenderBlankState(false)) + bar := NewOptions(100, OptionSetWidth(10)) bar.Reset() time.Sleep(1 * time.Second) bar.Add(10) @@ -59,9 +59,8 @@ func ExampleProgressBar_basic() { } func ExampleProgressBar_invisible() { - bar := NewOptions(100, OptionSetWidth(10), OptionSetRenderBlankState(false), OptionSetVisibility(false)) + bar := NewOptions(100, OptionSetWidth(10), OptionSetRenderBlankState(true), OptionSetVisibility(false)) bar.Reset() - bar.RenderBlank() fmt.Println("hello, world") time.Sleep(1 * time.Second) bar.Add(10) @@ -70,7 +69,7 @@ func ExampleProgressBar_invisible() { } func ExampleOptionThrottle() { - bar := NewOptions(100, OptionSetWidth(10), OptionSetRenderBlankState(false), OptionThrottle(100*time.Millisecond)) + bar := NewOptions(100, OptionSetWidth(10), OptionThrottle(100*time.Millisecond)) bar.Reset() bar.Add(5) time.Sleep(150 * time.Millisecond) @@ -81,7 +80,7 @@ func ExampleOptionThrottle() { } func ExampleOptionClearOnFinish() { - bar := NewOptions(100, OptionSetWidth(10), OptionSetRenderBlankState(false), OptionClearOnFinish()) + bar := NewOptions(100, OptionSetWidth(10), OptionClearOnFinish()) bar.Reset() bar.Finish() fmt.Println("Finished") @@ -90,7 +89,7 @@ func ExampleOptionClearOnFinish() { } func ExampleProgressBar_Finish() { - bar := NewOptions(100, OptionSetWidth(10), OptionSetRenderBlankState(false)) + bar := NewOptions(100, OptionSetWidth(10)) bar.Finish() // Output: // 100% |██████████| @@ -575,7 +574,7 @@ func md5sum(r io.Reader) (string, error) { func TestProgressBar_Describe(t *testing.T) { buf := strings.Builder{} - bar := NewOptions(100, OptionSetWidth(10), OptionSetRenderBlankState(false), OptionSetWriter(&buf)) + bar := NewOptions(100, OptionSetWidth(10), OptionSetWriter(&buf)) bar.Describe("performing axial adjustments") bar.Add(10) rawBuf := strconv.QuoteToASCII(buf.String()) @@ -583,3 +582,13 @@ func TestProgressBar_Describe(t *testing.T) { t.Errorf("wrong string: %s", rawBuf) } } + +func TestRenderBlankStateWithThrottle(t *testing.T) { + buf := strings.Builder{} + bar := NewOptions(100, OptionSetWidth(10), OptionSetRenderBlankState(true), OptionThrottle(time.Millisecond), OptionSetWriter(&buf)) + result := strings.TrimSpace(buf.String()) + expect := "0% | | [0s:0s]" + if result != expect { + t.Errorf("Render miss-match\nResult: '%s'\nExpect: '%s'\n%+v", result, expect, bar) + } +}