Skip to content

Commit

Permalink
Merge pull request #126 from NathanBaulch/throttled-renderblank
Browse files Browse the repository at this point in the history
Fix RenderBlank when throttled
  • Loading branch information
schollz committed Jul 31, 2022
2 parents 134ade7 + b8eacc7 commit 0d4fc1b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
34 changes: 19 additions & 15 deletions progressbar.go
Expand Up @@ -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),
Expand All @@ -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.
Expand All @@ -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),
Expand All @@ -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.
Expand All @@ -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),
Expand All @@ -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.
Expand All @@ -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),
Expand All @@ -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.
Expand All @@ -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()
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
23 changes: 16 additions & 7 deletions progressbar_test.go
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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")
Expand All @@ -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% |██████████|
Expand Down Expand Up @@ -575,11 +574,21 @@ 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())
if rawBuf != `"\rperforming axial adjustments 0% | | [0s:0s]\r \rperforming axial adjustments 10% |\u2588 | [0s:0s]"` {
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)
}
}

0 comments on commit 0d4fc1b

Please sign in to comment.