Skip to content

Commit

Permalink
remove timer when 100% is hit
Browse files Browse the repository at this point in the history
  • Loading branch information
schollz committed Oct 23, 2020
1 parent c121f44 commit 953dc78
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
34 changes: 23 additions & 11 deletions progressbar.go
Expand Up @@ -680,17 +680,29 @@ func renderProgressBar(c config, s state) (int, error) {
bytesString,
)
} else {
str = fmt.Sprintf("\r%s%4d%% %s%s%s%s %s [%s:%s]",
c.description,
s.currentPercent,
c.theme.BarStart,
saucer,
strings.Repeat(c.theme.SaucerPadding, repeatAmount),
c.theme.BarEnd,
bytesString,
leftBrac,
rightBrac,
)
if s.currentPercent == 100 {
str = fmt.Sprintf("\r%s%4d%% %s%s%s%s %s",
c.description,
s.currentPercent,
c.theme.BarStart,
saucer,
strings.Repeat(c.theme.SaucerPadding, repeatAmount),
c.theme.BarEnd,
bytesString,
)
} else {
str = fmt.Sprintf("\r%s%4d%% %s%s%s%s %s [%s:%s]",
c.description,
s.currentPercent,
c.theme.BarStart,
saucer,
strings.Repeat(c.theme.SaucerPadding, repeatAmount),
c.theme.BarEnd,
bytesString,
leftBrac,
rightBrac,
)
}
}

if c.colorCodes {
Expand Down
30 changes: 20 additions & 10 deletions progressbar_test.go
Expand Up @@ -91,7 +91,7 @@ func ExampleProgressBar_Finish() {
bar := NewOptions(100, OptionSetWidth(10), OptionSetRenderBlankState(false))
bar.Finish()
// Output:
// 100% |██████████| [0s:0s]
// 100% |██████████|
}

func Example_xOutOfY() {
Expand Down Expand Up @@ -128,15 +128,6 @@ func ExampleOptionShowCountBigNumber() {
// 0% | | (1/10000)
}

func ExampleOptionShowItsSlow() {
bar := NewOptions(100, OptionSetWidth(10), OptionShowIts())
bar.Reset()
time.Sleep(4 * time.Second)
bar.Add(1)
// Output:
// 1% | | (15 it/min) [4s:6m36s]
}

func ExampleOptionSetPredictTime() {
bar := NewOptions(100, OptionSetWidth(10), OptionSetPredictTime(false))
_ = bar.Add(10)
Expand Down Expand Up @@ -243,6 +234,25 @@ func ExampleIgnoreLength_WithSpeed() {
// | (0.011 kB/s)
}

func TestBarSlowAdd(t *testing.T) {
buf := strings.Builder{}
bar := NewOptions(100, OptionSetWidth(10), OptionShowIts(), OptionSetWriter(&buf))
bar.Reset()
time.Sleep(3 * time.Second)
bar.Add(1)
if !strings.Contains(buf.String(), "1%") {
t.Errorf("wrong string: %s", buf.String())
}
if !strings.Contains(buf.String(), "20 it/min") {
t.Errorf("wrong string: %s", buf.String())
}
if !strings.Contains(buf.String(), "[3s:") {
t.Errorf("wrong string: %s", buf.String())
}
// Output:
// 1% | | (20 it/min) [3s:4m57s]
}

func TestBarSmallBytes(t *testing.T) {
buf := strings.Builder{}
bar := NewOptions64(100000000, OptionShowBytes(true), OptionShowCount(), OptionSetWidth(10), OptionSetWriter(&buf))
Expand Down

1 comment on commit 953dc78

@NathanBaulch
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kinda liked having the elapsed time stick around for informational purposes. Perhaps an option to leave it?

Please sign in to comment.