diff --git a/progressbar.go b/progressbar.go index b994d6d..6d73304 100644 --- a/progressbar.go +++ b/progressbar.go @@ -859,6 +859,37 @@ func (p *ProgressBar) ChangeMax64(newMax int64) { p.Add(0) // re-render } +// AddMax takes in a int +// and adds it to the max +// value of the progress bar +func (p *ProgressBar) AddMax(added int) { + p.AddMax64(int64(added)) +} + +// AddMax64 is basically +// the same as AddMax, +// but takes in a int64 +// to avoid casting +func (p *ProgressBar) AddMax64(added int64) { + p.lock.Lock() + + p.config.max += added + + if p.config.showBytes { + p.config.maxHumanized, p.config.maxHumanizedSuffix = humanizeBytes(float64(p.config.max), + p.config.useIECUnits) + } + + if p.config.max == -1 { + p.lengthUnknown() + } else { + p.lengthKnown(p.config.max) + } + p.lock.Unlock() // so p.Add can lock + + p.Add(0) // re-render +} + // IsFinished returns true if progress bar is completed func (p *ProgressBar) IsFinished() bool { p.lock.Lock() diff --git a/progressbar_test.go b/progressbar_test.go index d468f61..90d2f3f 100644 --- a/progressbar_test.go +++ b/progressbar_test.go @@ -212,6 +212,14 @@ func ExampleProgressBar_ChangeMax() { // 100% |██████████| } +func ExampleProgressBar_AddMax() { + bar := NewOptions(50, OptionSetWidth(10), OptionSetPredictTime(false)) + bar.AddMax(50) + bar.Add(100) + // Output: + // 100% |██████████| +} + func ExampleOptionShowIts_spinner() { /* Spinner test with iteration count and iteration rate