Skip to content

Commit

Permalink
add visibility flag to quickly turn off
Browse files Browse the repository at this point in the history
  • Loading branch information
schollz committed Sep 3, 2020
1 parent 385b256 commit b72da62
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
17 changes: 17 additions & 0 deletions progressbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ type config struct {
// fullWidth specifies whether to measure and set the bar to a specific width
fullWidth bool

// invisible doesn't render the bar at all, useful for debugging
invisible bool

onCompletion func()
}

Expand Down Expand Up @@ -122,6 +125,13 @@ func OptionSetTheme(t Theme) Option {
}
}

// OptionSetVisibility sets the visibility
func OptionSetVisibility(visibility bool) Option {
return func(p *ProgressBar) {
p.config.invisible = !visibility
}
}

// OptionFullWidth sets the bar to be full width
func OptionFullWidth() Option {
return func(p *ProgressBar) {
Expand Down Expand Up @@ -236,6 +246,7 @@ func NewOptions64(max int64, options ...Option) *ProgressBar {
throttleDuration: 0 * time.Nanosecond,
predictTime: true,
spinnerType: 9,
invisible: false,
},
}

Expand Down Expand Up @@ -329,6 +340,9 @@ func Default(max int64, description ...string) *ProgressBar {

// RenderBlank renders the current bar state, you can use this to render a 0% state
func (p *ProgressBar) RenderBlank() error {
if p.config.invisible {
return nil
}
return p.render()
}

Expand Down Expand Up @@ -369,6 +383,9 @@ func (p *ProgressBar) Set64(num int64) error {

// Add64 will add the specified amount to the progressbar
func (p *ProgressBar) Add64(num int64) error {
if p.config.invisible {
return nil
}
p.lock.Lock()
defer p.lock.Unlock()

Expand Down
11 changes: 11 additions & 0 deletions progressbar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ func ExampleProgressBar_basic() {
// 10% |█ | [1s:9s]
}

func ExampleProgressBar_invisible() {
bar := NewOptions(100, OptionSetWidth(10), OptionSetRenderBlankState(false), OptionSetVisibility(false))
bar.Reset()
bar.RenderBlank()
fmt.Println("hello, world")
time.Sleep(1 * time.Second)
bar.Add(10)
// Output:
// hello, world
}

func ExampleOptionThrottle() {
bar := NewOptions(100, OptionSetWidth(10), OptionSetRenderBlankState(false), OptionThrottle(100*time.Millisecond))
bar.Reset()
Expand Down

0 comments on commit b72da62

Please sign in to comment.