Skip to content

Commit

Permalink
Added IsFinished method to progress bar. (#77)
Browse files Browse the repository at this point in the history
Add IsFinished Fixes #77
  • Loading branch information
haseth committed Oct 2, 2020
1 parent 984611c commit 2ec720b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
24 changes: 19 additions & 5 deletions examples/customization/main.go
@@ -1,13 +1,16 @@
package main

import (
"fmt"
"time"

"github.com/k0kubun/go-ansi"
"github.com/schollz/progressbar/v3"
)

func main() {
doneCh := make(chan struct{})

bar := progressbar.NewOptions(1000,
progressbar.OptionSetWriter(ansi.NewAnsiStdout()),
progressbar.OptionEnableColorCodes(true),
Expand All @@ -20,9 +23,20 @@ func main() {
SaucerPadding: " ",
BarStart: "[",
BarEnd: "]",
}))
for i := 0; i < 1000; i++ {
bar.Add(1)
time.Sleep(5 * time.Millisecond)
}
}),
progressbar.OptionOnCompletion(func() {
doneCh <- struct{}{}
}),
)

go func() {
for i := 0; i < 1000; i++ {
bar.Add(1)
time.Sleep(5 * time.Millisecond)
}
}()

// got notified that progress bar is complete.
<-doneCh
fmt.Println("\n ======= progress bar completed ==========\n")
}
5 changes: 5 additions & 0 deletions progressbar.go
Expand Up @@ -477,6 +477,11 @@ func (p *ProgressBar) ChangeMax64(newMax int64) {
p.Add(0) // re-render
}

// IsFinished returns true if progreess bar is completed
func (p *ProgressBar) IsFinished() bool {
return p.state.finished
}

// render renders the progress bar, updating the maximum
// rendered line width. this function is not thread-safe,
// so it must be called with an acquired lock.
Expand Down
27 changes: 27 additions & 0 deletions progressbar_test.go
Expand Up @@ -197,6 +197,33 @@ func TestSpinnerType(t *testing.T) {
}
}

func Test_IsFinished(t *testing.T) {
isCalled := false
bar := NewOptions(72, OptionOnCompletion(func() {
isCalled = true
}))

// Test1: If bar is not fully completed.
bar.Add(5)
if bar.IsFinished() || isCalled {
t.Errorf("Successfully tested bar is not yet finished.")
}

// Test2: Bar fully completed.
bar.Add(67)
if !bar.IsFinished() || !isCalled {
t.Errorf("Successfully tested bar is finished.")
}

// Test3: If increases maximum bytes error should be thrown and
// bar finished will remain false.
bar.Reset()
err := bar.Add(73)
if err == nil || bar.IsFinished() {
t.Errorf("Successfully got error when bytes increases max bytes, bar finished: %v", bar.IsFinished())
}
}

func ExampleIgnoreLength_WithSpeed() {
/*
IgnoreLength test with iterations and count
Expand Down

0 comments on commit 2ec720b

Please sign in to comment.