From ac7ed2c6a1c121f8fbae0f25c973afc4037036cf Mon Sep 17 00:00:00 2001 From: abo3losh1 Date: Thu, 15 Oct 2026 00:02:20 +0200 Subject: [PATCH 1/2] Return -1 from GetMax for a bar of unknown length For an unknown length, lengthUnknown stores the bar width in config.max, so GetMax and GetMax64 returned the width (40 by default) while State() reported Max as -1. Return -1 from both getters in that case. --- progressbar.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/progressbar.go b/progressbar.go index 8c908bb..22774dc 100644 --- a/progressbar.go +++ b/progressbar.go @@ -876,19 +876,20 @@ func New64(max int64) *ProgressBar { return NewOptions64(max) } -// GetMax returns the max of a bar +// GetMax returns the max of a bar, or -1 if the length is unknown func (p *ProgressBar) GetMax() int { - p.lock.Lock() - defer p.lock.Unlock() - - return int(p.config.max) + return int(p.GetMax64()) } -// GetMax64 returns the current max +// GetMax64 returns the current max, or -1 if the length is unknown func (p *ProgressBar) GetMax64() int64 { p.lock.Lock() defer p.lock.Unlock() + // For an unknown length, config.max holds the spinner width, not a max. + if p.config.ignoreLength { + return -1 + } return p.config.max } From 9dabe6a8be4c1494ee9ecc3b52a38be4c7ffb63c Mon Sep 17 00:00:00 2001 From: abo3losh1 Date: Thu, 15 Oct 2026 00:02:20 +0200 Subject: [PATCH 2/2] Test GetMax for a bar of unknown length --- progressbar_test.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/progressbar_test.go b/progressbar_test.go index 8238e6d..e0bd0b1 100644 --- a/progressbar_test.go +++ b/progressbar_test.go @@ -431,6 +431,29 @@ func TestState(t *testing.T) { } } +func TestGetMaxUnknownLength(t *testing.T) { + bar := NewOptions(-1, OptionSetWriter(io.Discard), OptionSetSpinnerChangeInterval(0)) + if got := bar.GetMax(); got != -1 { + t.Errorf("GetMax() = %d, want -1", got) + } + if got := bar.GetMax64(); got != -1 { + t.Errorf("GetMax64() = %d, want -1", got) + } + if got := bar.State().Max; got != -1 { + t.Errorf("State().Max = %d, want -1", got) + } + + bar = NewOptions(100, OptionSetWriter(io.Discard), OptionSetSpinnerChangeInterval(0)) + bar.ChangeMax(-1) + if got := bar.GetMax64(); got != -1 { + t.Errorf("after ChangeMax(-1), GetMax64() = %d, want -1", got) + } + bar.ChangeMax(50) + if got := bar.GetMax64(); got != 50 { + t.Errorf("after ChangeMax(50), GetMax64() = %d, want 50", got) + } +} + func TestStateBeforeStart(t *testing.T) { for _, tc := range []struct { name string