From dd105e186a2e575dedfc50e9bbc8b345b47a7b47 Mon Sep 17 00:00:00 2001 From: x Date: Wed, 2 Nov 2022 19:29:19 +0800 Subject: [PATCH] feat: exit the bar to keep current state --- progressbar.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/progressbar.go b/progressbar.go index 4c268be..e73f58b 100644 --- a/progressbar.go +++ b/progressbar.go @@ -51,6 +51,7 @@ type state struct { maxLineWidth int currentBytes float64 finished bool + exit bool // Progress bar exit halfway rendered string } @@ -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)) @@ -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") }