Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add AddMax #209

Merged
merged 2 commits into from
Dec 7, 2024
Merged
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
31 changes: 31 additions & 0 deletions progressbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions progressbar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading