Skip to content

Commit

Permalink
Merge pull request #122 from gabe565/time-no-length
Browse files Browse the repository at this point in the history
Show elapsed time when no length is given
  • Loading branch information
schollz committed Jul 31, 2022
2 parents 3ebeac9 + 093c313 commit a96b4b9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
40 changes: 32 additions & 8 deletions progressbar.go
Expand Up @@ -76,6 +76,10 @@ type config struct {
showIterationsPerSecond bool
showIterationsCount bool

// whether the progress bar should show elapsed time.
// always enabled if predictTime is true.
elapsedTime bool

// whether the progress bar should attempt to predict the finishing
// time of the progress based on the start time and the average
// number of seconds between increments.
Expand Down Expand Up @@ -179,6 +183,13 @@ func OptionEnableColorCodes(colorCodes bool) Option {
}
}

// OptionSetElapsedTime will enable elapsed time. always enabled if OptionSetPredictTime is true.
func OptionSetElapsedTime(elapsedTime bool) Option {
return func(p *ProgressBar) {
p.config.elapsedTime = elapsedTime
}
}

// OptionSetPredictTime will also attempt to predict the time remaining.
func OptionSetPredictTime(predictTime bool) Option {
return func(p *ProgressBar) {
Expand Down Expand Up @@ -264,6 +275,7 @@ func NewOptions64(max int64, options ...Option) *ProgressBar {
width: 40,
max: max,
throttleDuration: 0 * time.Nanosecond,
elapsedTime: true,
predictTime: true,
spinnerType: 9,
invisible: false,
Expand Down Expand Up @@ -735,13 +747,16 @@ func renderProgressBar(c config, s *state) (int, error) {
}

// show time prediction in "current/total" seconds format
if c.predictTime {
leftBrac = (time.Duration(time.Since(s.startTime).Seconds()) * time.Second).String()
switch {
case c.predictTime:
rightBracNum := (time.Duration((1/averageRate)*(float64(c.max)-float64(s.currentNum))) * time.Second)
if rightBracNum.Seconds() < 0 {
rightBracNum = 0 * time.Second
}
rightBrac = rightBracNum.String()
fallthrough
case c.elapsedTime:
leftBrac = (time.Duration(time.Since(s.startTime).Seconds()) * time.Second).String()
}

if c.fullWidth && !c.ignoreLength {
Expand Down Expand Up @@ -787,12 +802,21 @@ func renderProgressBar(c config, s *state) (int, error) {
repeatAmount = 0
}
if c.ignoreLength {
str = fmt.Sprintf("\r%s %s %s ",
spinners[c.spinnerType][int(math.Round(math.Mod(float64(time.Since(s.startTime).Milliseconds()/100), float64(len(spinners[c.spinnerType])))))],
c.description,
bytesString,
)
} else if leftBrac == "" {
if c.elapsedTime {
str = fmt.Sprintf("\r%s %s %s [%s] ",
spinners[c.spinnerType][int(math.Round(math.Mod(float64(time.Since(s.startTime).Milliseconds()/100), float64(len(spinners[c.spinnerType])))))],
c.description,
bytesString,
leftBrac,
)
} else {
str = fmt.Sprintf("\r%s %s %s ",
spinners[c.spinnerType][int(math.Round(math.Mod(float64(time.Since(s.startTime).Milliseconds()/100), float64(len(spinners[c.spinnerType])))))],
c.description,
bytesString,
)
}
} else if rightBrac == "" {
str = fmt.Sprintf("\r%s%4d%% %s%s%s%s %s ",
c.description,
s.currentPercent,
Expand Down
22 changes: 20 additions & 2 deletions progressbar_test.go
Expand Up @@ -168,7 +168,7 @@ func ExampleIgnoreLength_WithIteration() {
bar.Add(5)

// Output:
// - (5/-, 5 it/s)
// - (5/-, 5 it/s) [1s]
}

func TestSpinnerType(t *testing.T) {
Expand Down Expand Up @@ -232,7 +232,7 @@ func ExampleIgnoreLength_WithSpeed() {
bar.Add(11)

// Output:
// - (11 B/s)
// - (11 B/s) [1s]
}

func TestBarSlowAdd(t *testing.T) {
Expand Down Expand Up @@ -384,6 +384,24 @@ func TestOptionSetPredictTime(t *testing.T) {
}
}

func TestOptionSetElapsedTime(t *testing.T) {
/*
IgnoreLength test with iteration count and iteration rate
*/
bar := NewOptions(-1,
OptionSetWidth(10),
OptionShowIts(),
OptionShowCount(),
OptionSetElapsedTime(false),
)
bar.Reset()
time.Sleep(1 * time.Second)
bar.Add(5)

// Output:
// - (5/-, 5 it/s)
}

func TestIgnoreLength(t *testing.T) {
bar := NewOptions(
-1,
Expand Down

0 comments on commit a96b4b9

Please sign in to comment.