From 920dfd7e6204a64930841e4e09a7208f2ebdc9b1 Mon Sep 17 00:00:00 2001 From: Kevin Hanselman Date: Mon, 2 Dec 2024 23:41:56 -0500 Subject: [PATCH 1/2] add AddMax --- progressbar.go | 26 ++++++++++++++++++++++++++ progressbar_test.go | 9 +++++++++ 2 files changed, 35 insertions(+) diff --git a/progressbar.go b/progressbar.go index b994d6d..f70dd1e 100644 --- a/progressbar.go +++ b/progressbar.go @@ -859,6 +859,32 @@ 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) + } + + 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..e4b4988 100644 --- a/progressbar_test.go +++ b/progressbar_test.go @@ -212,6 +212,15 @@ func ExampleProgressBar_ChangeMax() { // 100% |██████████| } +func ExampleProgressBar_AddMax() { + bar := NewOptions(50, OptionSetWidth(10), OptionSetPredictTime(false)) + bar.Add(25) + bar.AddMax(50) + bar.Add(75) + // Output: + // 100% |██████████| +} + func ExampleOptionShowIts_spinner() { /* Spinner test with iteration count and iteration rate From f5093f1cf077f5f41cb8911abfa4722bf44dc158 Mon Sep 17 00:00:00 2001 From: Kevin Hanselman Date: Sat, 7 Dec 2024 11:50:31 -0500 Subject: [PATCH 2/2] fix test --- progressbar.go | 5 +++++ progressbar_test.go | 3 +-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/progressbar.go b/progressbar.go index f70dd1e..6d73304 100644 --- a/progressbar.go +++ b/progressbar.go @@ -880,6 +880,11 @@ func (p *ProgressBar) AddMax64(added int64) { 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 diff --git a/progressbar_test.go b/progressbar_test.go index e4b4988..90d2f3f 100644 --- a/progressbar_test.go +++ b/progressbar_test.go @@ -214,9 +214,8 @@ func ExampleProgressBar_ChangeMax() { func ExampleProgressBar_AddMax() { bar := NewOptions(50, OptionSetWidth(10), OptionSetPredictTime(false)) - bar.Add(25) bar.AddMax(50) - bar.Add(75) + bar.Add(100) // Output: // 100% |██████████| }