Skip to content

Commit

Permalink
feat: exit the bar to keep current state
Browse files Browse the repository at this point in the history
  • Loading branch information
x committed Nov 2, 2022
1 parent 3ad62b6 commit dd105e1
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions progressbar.go
Original file line number Diff line number Diff line change
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 dd105e1

Please sign in to comment.