Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: exit the bar to keep current state #142

Merged
merged 1 commit into from Nov 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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