Skip to content

Commit 4c4baf0

Browse files
committed
fix: make elapsedTime option work in non-spinner mode
Display elapsed time at the end of the bar when elapsedTime is enabled while predictTime is set to false. Demo: ``` 70% |███████ | [0s] ```
1 parent 304f5f4 commit 4c4baf0

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

progressbar.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ func NewOptions64(max int64, options ...Option) *ProgressBar {
323323
width: 40,
324324
max: max,
325325
throttleDuration: 0 * time.Nanosecond,
326-
elapsedTime: true,
326+
elapsedTime: max == -1,
327327
predictTime: true,
328328
spinnerType: 9,
329329
invisible: false,
@@ -840,7 +840,7 @@ func renderProgressBar(c config, s *state) (int, error) {
840840
}
841841
rightBrac = rightBracNum.String()
842842
fallthrough
843-
case c.elapsedTime:
843+
case c.elapsedTime || c.showElapsedTimeOnFinish:
844844
leftBrac = (time.Duration(time.Since(s.startTime).Seconds()) * time.Second).String()
845845
}
846846

@@ -944,8 +944,7 @@ func renderProgressBar(c config, s *state) (int, error) {
944944
strings.Repeat(c.theme.SaucerPadding, repeatAmount),
945945
c.theme.BarEnd,
946946
sb.String())
947-
948-
if s.currentPercent == 100 && c.showElapsedTimeOnFinish {
947+
if (s.currentPercent == 100 && c.showElapsedTimeOnFinish) || c.elapsedTime {
949948
str = fmt.Sprintf("%s [%s]", str, leftBrac)
950949
}
951950

progressbar_test.go

+37-6
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,37 @@ func TestOptionSetElapsedTime_spinner(t *testing.T) {
464464
}
465465
}
466466

467+
func TestOptionSetElapsedTime(t *testing.T) {
468+
buf := strings.Builder{}
469+
bar := NewOptions(
470+
10,
471+
OptionSetElapsedTime(false),
472+
OptionSetPredictTime(false),
473+
OptionSetWidth(10),
474+
OptionSetWriter(&buf),
475+
)
476+
477+
_ = bar.Add(2)
478+
result := strings.TrimSpace(buf.String())
479+
expect := "20% |██ |"
480+
481+
if result != expect {
482+
t.Errorf("Render miss-match\nResult: '%s'\nExpect: '%s'\n%+v", result, expect, bar)
483+
}
484+
485+
bar.Reset()
486+
bar.config.elapsedTime = true
487+
buf.Reset()
488+
489+
_ = bar.Add(7)
490+
result = strings.TrimSpace(buf.String())
491+
expect = "70% |███████ | [0s]"
492+
493+
if result != expect {
494+
t.Errorf("Render miss-match\nResult: '%s'\nExpect: '%s'\n%+v", result, expect, bar)
495+
}
496+
}
497+
467498
func TestShowElapsedTimeOnFinish(t *testing.T) {
468499
buf := strings.Builder{}
469500
bar := NewOptions(10,
@@ -739,9 +770,9 @@ func TestOptionFullWidth(t *testing.T) {
739770
{ // 4
740771
[]Option{OptionSetPredictTime(false)},
741772
"" +
742-
"\r 10% |██████ | " +
743-
"\r \r" +
744-
"\r 100% |████████████████████████████████████████████████████████████████| ",
773+
"\r 10% |██████ | " +
774+
"\r \r" +
775+
"\r 100% |█████████████████████████████████████████████████████████████████████| ",
745776
},
746777
{ // 5
747778
[]Option{OptionSetPredictTime(false), OptionShowElapsedTimeOnFinish()},
@@ -795,9 +826,9 @@ func TestOptionFullWidth(t *testing.T) {
795826
{ // 12
796827
[]Option{OptionShowIts(), OptionShowCount(), OptionSetPredictTime(false)},
797828
"" +
798-
"\r 10% |████ | (10/100, 10 it/s) " +
799-
"\r \r" +
800-
"\r 100% |██████████████████████████████████████████████| (100/100, 50 it/s) ",
829+
"\r 10% |████ | (10/100, 10 it/s) " +
830+
"\r \r" +
831+
"\r 100% |███████████████████████████████████████████████████| (100/100, 50 it/s) ",
801832
},
802833
{ // 13
803834
[]Option{OptionShowIts(), OptionShowCount(), OptionSetPredictTime(false), OptionShowElapsedTimeOnFinish()},

0 commit comments

Comments
 (0)