Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions progressbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
23 changes: 23 additions & 0 deletions progressbar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down