Skip to content

Commit

Permalink
Merge pull request #30 from schollz/finish
Browse files Browse the repository at this point in the history
add option to clear on finish
  • Loading branch information
schollz authored Apr 1, 2019
2 parents c68be36 + fb29512 commit a77c958
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
5 changes: 5 additions & 0 deletions examples/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"os"
"time"

Expand All @@ -17,6 +18,7 @@ func main() {
bar.Add(1)
time.Sleep(2 * time.Millisecond)
}
fmt.Println("finished1")

// bar with options
bar = progressbar.NewOptions(1000,
Expand All @@ -28,6 +30,7 @@ func main() {
bar.Add(1)
time.Sleep(2 * time.Millisecond)
}
fmt.Println("finished2")

bar = progressbar.NewOptions(100,
progressbar.OptionSetWriter(ansi.NewAnsiStdout()),
Expand All @@ -47,4 +50,6 @@ func main() {
time.Sleep(10 * time.Millisecond)
}

fmt.Println("finished3")

}
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module github.com/schollz/progressbar/v2

require github.com/mitchellh/colorstring v0.0.0-20150917214807-8631ce90f286
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db
github.com/stretchr/objx v0.1.1 // indirect
github.com/stretchr/testify v1.3.0
)
21 changes: 21 additions & 0 deletions progressbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type state struct {

maxLineWidth int
currentBytes float64

finished bool
}

type config struct {
Expand All @@ -61,6 +63,9 @@ type config struct {

// minimum time to wait in between updates
throttleDuration time.Duration

// clear bar once finished
clearOnFinish bool
}

// Theme defines the elements of the bar
Expand Down Expand Up @@ -152,6 +157,13 @@ func OptionThrottle(duration time.Duration) Option {
}
}

// OptionClearOnFinish will clear the bar once its finished
func OptionClearOnFinish() Option {
return func(p *ProgressBar) {
p.config.clearOnFinish = true
}
}

var defaultTheme = Theme{Saucer: "█", SaucerPadding: " ", BarStart: "|", BarEnd: "|"}

// NewOptions constructs a new instance of ProgressBar, with any options you specify
Expand Down Expand Up @@ -288,6 +300,15 @@ func (p *ProgressBar) render() error {
return err
}

// check if the progress bar is finished
if p.state.finished || p.state.currentNum >= p.config.max {
p.state.finished = true
if p.config.clearOnFinish {
// if the progressbar is finished, return
return nil
}
}

// then, re-render the current progress bar
w, err := renderProgressBar(p.config, p.state)
if err != nil {
Expand Down
12 changes: 11 additions & 1 deletion progressbar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -42,11 +43,20 @@ func ExampleThrottle() {
// 10% |█ | [0s:1s]
}
func ExampleFinish() {
bar := NewOptions(100, OptionSetWidth(10), OptionSetRenderBlankState(false), OptionClearOnFinish())
bar.Reset()
bar.Finish()
fmt.Println("Finished")
// Output:
// Finished
}
func ExampleFinish2() {
bar := NewOptions(100, OptionSetWidth(10), OptionSetRenderBlankState(false))
bar.Reset()
bar.Finish()
fmt.Println("Finished")
// Output:
// 100% |██████████| [0s:0s]
// 100% |██████████| [0s:0s]Finished
}

func ExampleSetBytes() {
Expand Down

0 comments on commit a77c958

Please sign in to comment.