Skip to content

Commit

Permalink
Merge pull request #209 from kevin-hanselman/add_max
Browse files Browse the repository at this point in the history
add AddMax
  • Loading branch information
schollz authored Dec 7, 2024
2 parents 23e1221 + f5093f1 commit 77ce0b9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
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

0 comments on commit 77ce0b9

Please sign in to comment.