Skip to content

Commit

Permalink
Merge pull request #184 from wjf3121/wjf3121/fix_elapsed_time
Browse files Browse the repository at this point in the history
fix: make elapsedTime option work in non-spinner mode
  • Loading branch information
schollz authored May 20, 2024
2 parents 091201b + 4c4baf0 commit 35b4e07
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
7 changes: 3 additions & 4 deletions progressbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func NewOptions64(max int64, options ...Option) *ProgressBar {
width: 40,
max: max,
throttleDuration: 0 * time.Nanosecond,
elapsedTime: true,
elapsedTime: max == -1,
predictTime: true,
spinnerType: 9,
invisible: false,
Expand Down Expand Up @@ -847,7 +847,7 @@ func renderProgressBar(c config, s *state) (int, error) {
}
rightBrac = rightBracNum.String()
fallthrough
case c.elapsedTime:
case c.elapsedTime || c.showElapsedTimeOnFinish:
leftBrac = (time.Duration(time.Since(s.startTime).Seconds()) * time.Second).String()
}

Expand Down Expand Up @@ -951,8 +951,7 @@ func renderProgressBar(c config, s *state) (int, error) {
strings.Repeat(c.theme.SaucerPadding, repeatAmount),
c.theme.BarEnd,
sb.String())

if s.currentPercent == 100 && c.showElapsedTimeOnFinish {
if (s.currentPercent == 100 && c.showElapsedTimeOnFinish) || c.elapsedTime {
str = fmt.Sprintf("%s [%s]", str, leftBrac)
}

Expand Down
43 changes: 37 additions & 6 deletions progressbar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,37 @@ func TestOptionSetElapsedTime_spinner(t *testing.T) {
}
}

func TestOptionSetElapsedTime(t *testing.T) {
buf := strings.Builder{}
bar := NewOptions(
10,
OptionSetElapsedTime(false),
OptionSetPredictTime(false),
OptionSetWidth(10),
OptionSetWriter(&buf),
)

_ = bar.Add(2)
result := strings.TrimSpace(buf.String())
expect := "20% |██ |"

if result != expect {
t.Errorf("Render miss-match\nResult: '%s'\nExpect: '%s'\n%+v", result, expect, bar)
}

bar.Reset()
bar.config.elapsedTime = true
buf.Reset()

_ = bar.Add(7)
result = strings.TrimSpace(buf.String())
expect = "70% |███████ | [0s]"

if result != expect {
t.Errorf("Render miss-match\nResult: '%s'\nExpect: '%s'\n%+v", result, expect, bar)
}
}

func TestShowElapsedTimeOnFinish(t *testing.T) {
buf := strings.Builder{}
bar := NewOptions(10,
Expand Down Expand Up @@ -741,9 +772,9 @@ func TestOptionFullWidth(t *testing.T) {
{ // 4
[]Option{OptionSetPredictTime(false)},
"" +
"\r 10% |██████ | " +
"\r \r" +
"\r 100% |████████████████████████████████████████████████████████████████| ",
"\r 10% |██████ | " +
"\r \r" +
"\r 100% |█████████████████████████████████████████████████████████████████████| ",
},
{ // 5
[]Option{OptionSetPredictTime(false), OptionShowElapsedTimeOnFinish()},
Expand Down Expand Up @@ -797,9 +828,9 @@ func TestOptionFullWidth(t *testing.T) {
{ // 12
[]Option{OptionShowIts(), OptionShowCount(), OptionSetPredictTime(false)},
"" +
"\r 10% |████ | (10/100, 10 it/s) " +
"\r \r" +
"\r 100% |██████████████████████████████████████████████| (100/100, 50 it/s) ",
"\r 10% |████ | (10/100, 10 it/s) " +
"\r \r" +
"\r 100% |███████████████████████████████████████████████████| (100/100, 50 it/s) ",
},
{ // 13
[]Option{OptionShowIts(), OptionShowCount(), OptionSetPredictTime(false), OptionShowElapsedTimeOnFinish()},
Expand Down

0 comments on commit 35b4e07

Please sign in to comment.