Skip to content

Commit

Permalink
Merge pull request #142 from Mr1X/dev.exit
Browse files Browse the repository at this point in the history
feat: exit the bar to keep current state
  • Loading branch information
schollz committed Nov 2, 2022
2 parents 3ad62b6 + dd105e1 commit 4c315a5
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions progressbar.go
Expand Up @@ -51,6 +51,7 @@ type state struct {
maxLineWidth int
currentBytes float64
finished bool
exit bool // Progress bar exit halfway

rendered string
}
Expand Down Expand Up @@ -467,6 +468,18 @@ func (p *ProgressBar) Finish() error {
return p.Add(0)
}

// Exit will exit the bar to keep current state
func (p *ProgressBar) Exit() error {
p.lock.Lock()
defer p.lock.Unlock()

p.state.exit = true
if p.config.onCompletion != nil {
p.config.onCompletion()
}
return nil
}

// Add will add the specified amount to the progressbar
func (p *ProgressBar) Add(num int) error {
return p.Add64(int64(num))
Expand All @@ -493,6 +506,10 @@ func (p *ProgressBar) Add64(num int64) error {
p.lock.Lock()
defer p.lock.Unlock()

if p.state.exit {
return nil
}

if p.config.max == 0 {
return errors.New("max must be greater than 0")
}
Expand Down

0 comments on commit 4c315a5

Please sign in to comment.