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

Show elapsed time when no length is given #122

Merged
merged 1 commit into from Jul 31, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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