Skip to content

Commit

Permalink
add ChangeMax
Browse files Browse the repository at this point in the history
  • Loading branch information
schollz committed Apr 11, 2020
1 parent 77e36af commit d4d0bbc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
21 changes: 17 additions & 4 deletions progressbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ type ProgressBar struct {
type State struct {
CurrentPercent float64
CurrentBytes float64
MaxBytes int64
SecondsSince float64
SecondsLeft float64
KBsPerSecond float64
Expand Down Expand Up @@ -61,7 +60,6 @@ type config struct {

// whether the output is expected to contain color codes
colorCodes bool
maxBytes int64

// show rate of change in kB/sec or MB/sec
showBytes bool
Expand Down Expand Up @@ -322,7 +320,7 @@ func (p *ProgressBar) Add64(num int64) error {
}

// always update if show bytes/second or its/second
if updateBar || p.config.showIterationsPerSecond || p.config.maxBytes > 0 {
if updateBar || p.config.showIterationsPerSecond {
return p.render()
}

Expand Down Expand Up @@ -356,6 +354,22 @@ func (p *ProgressBar) GetMax64() int64 {
return p.config.max
}

// ChangeMax takes in a int
// and changes the max value
// of the progress bar
func (p *ProgressBar) ChangeMax(newMax int) {
p.ChangeMax64(int64(newMax))
}

// ChangeMax64 is basically
// the same as ChangeMax,
// but takes in a int64
// to avoid casting
func (p *ProgressBar) ChangeMax64(newMax int64) {
p.config.max = newMax
p.Add(0) // re-render
}

// render renders the progress bar, updating the maximum
// rendered line width. this function is not thread-safe,
// so it must be called with an acquired lock.
Expand Down Expand Up @@ -410,7 +424,6 @@ func (p *ProgressBar) State() State {
s := State{}
s.CurrentPercent = float64(p.state.currentNum) / float64(p.config.max)
s.CurrentBytes = p.state.currentBytes
s.MaxBytes = p.config.maxBytes
s.SecondsSince = time.Since(p.state.startTime).Seconds()
if p.state.currentNum > 0 {
s.SecondsLeft = s.SecondsSince / float64(p.state.currentNum) * (float64(p.config.max) - float64(p.state.currentNum))
Expand Down
8 changes: 8 additions & 0 deletions progressbar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ func ExampleOptionSetPredictTime() {
// 10% |█ | [10:100]
}

func ExampleOptionChangeMax() {
bar := NewOptions(100, OptionSetWidth(10), OptionSetPredictTime(false))
bar.ChangeMax(50)
bar.Add(50)
// Output:
// 100% |██████████| [50:50]
}

func ExampleIgnoreLength_WithIteration() {
/*
IgnoreLength test with iteration count and iteration rate
Expand Down

0 comments on commit d4d0bbc

Please sign in to comment.