Skip to content

Commit

Permalink
Explicit newline if string is not cleared after finish
Browse files Browse the repository at this point in the history
  • Loading branch information
flowchartsman committed Sep 29, 2021
1 parent fa76ccc commit a7b2337
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
8 changes: 7 additions & 1 deletion progressbar.go
Expand Up @@ -585,6 +585,13 @@ func (p *ProgressBar) render() error {
}
}
if p.state.finished {
// append a newline if we're leaving the progressbar around
if !p.config.clearOnFinish {
if _, err := p.config.writer.Write([]byte{'\n'}); err != nil {
return err
}
}

// when using ANSI codes we don't pre-clean the current line
if p.config.useANSICodes {
err := clearProgressBar(p.config, p.state)
Expand Down Expand Up @@ -679,7 +686,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)
Expand Down
52 changes: 51 additions & 1 deletion progressbar_test.go
Expand Up @@ -17,6 +17,34 @@ import (
"github.com/stretchr/testify/assert"
)

// clearBuffer simulates lines printed to the screen. Any carriage returns will
// reset the beginning of the accumulated bytes, while any newline will write
// accumulated bytes. Useful for testing against how the screen would appear
type clearBuffer struct {
b []byte
}

func (c *clearBuffer) Write(p []byte) (int, error) {
start := 0
for i, b := range p {
switch b {
case '\r':
start = i + 1
case '\n':
c.b = append(c.b, p[start:i+1]...)
start = i + 1
}
}
if start < len(p) {
c.b = append(c.b, p[start:]...)
}
return len(p), nil
}

func (c *clearBuffer) String() string {
return string(c.b)
}

func BenchmarkRender(b *testing.B) {
bar := NewOptions64(100000000,
OptionSetWriter(os.Stderr),
Expand Down Expand Up @@ -87,6 +115,7 @@ func ExampleOptionClearOnFinish() {
// Output:
// Finished
}

func ExampleProgressBar_Finish() {
bar := NewOptions(100, OptionSetWidth(10), OptionSetRenderBlankState(false))
bar.Finish()
Expand Down Expand Up @@ -502,7 +531,6 @@ func TestConcurrency(t *testing.T) {
}

func TestIterationNames(t *testing.T) {

b := Default(20)
tc := b.config

Expand All @@ -520,6 +548,28 @@ func TestIterationNames(t *testing.T) {
}
}

func TestNoClear(t *testing.T) {
buf := &clearBuffer{}
bar := NewOptions(100, OptionSetWidth(10), OptionSetWriter(buf))
bar.Finish()
bar = NewOptions(100, OptionSetWidth(10), OptionSetWriter(buf))
bar.Finish()
want := " 100% |██████████| \n 100% |██████████| \n"
if buf.String() != want {
t.Errorf("Expected to see two lines:\n%s instead I got:\n%s\n", want, buf.String())
}

buf = &clearBuffer{}
bar = NewOptions(100, OptionSetWidth(10), OptionClearOnFinish(), OptionSetWriter(buf))
bar.Finish()
bar = NewOptions(100, OptionSetWidth(10), OptionClearOnFinish(), OptionSetWriter(buf))
bar.Finish()
want = ""
if buf.String() != want {
t.Errorf("Expected to see no lines, instead I got:\n%s\n", buf.String())
}
}

func md5sum(r io.Reader) (string, error) {
hash := md5.New()
_, err := io.Copy(hash, r)
Expand Down

0 comments on commit a7b2337

Please sign in to comment.