Skip to content

Commit

Permalink
Merge pull request #133 from hexnaught/upstream/issue/128
Browse files Browse the repository at this point in the history
feat: option to keep showing elapsed time on finish
  • Loading branch information
schollz committed Sep 1, 2022
2 parents c621381 + 874bd56 commit be3c9b6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
14 changes: 13 additions & 1 deletion progressbar.go
Expand Up @@ -80,6 +80,8 @@ type config struct {
// always enabled if predictTime is true.
elapsedTime bool

showElapsedTimeOnFinish 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 @@ -211,7 +213,14 @@ func OptionShowIts() Option {
}
}

// OptionSetItsString sets what's displayed for interations a second. The default is "it" which would display: "it/s"
// OptionShowElapsedOnFinish will keep the display of elapsed time on finish
func OptionShowElapsedTimeOnFinish() Option {
return func(p *ProgressBar) {
p.config.showElapsedTimeOnFinish = true
}
}

// OptionSetItsString sets what's displayed for iterations a second. The default is "it" which would display: "it/s"
func OptionSetItsString(iterationString string) Option {
return func(p *ProgressBar) {
p.config.iterationString = iterationString
Expand Down Expand Up @@ -837,6 +846,9 @@ func renderProgressBar(c config, s *state) (int, error) {
c.theme.BarEnd,
bytesString,
)
if c.showElapsedTimeOnFinish {
str = fmt.Sprintf("%s [%s]", str, leftBrac)
}
} else {
str = fmt.Sprintf("\r%s%4d%% %s%s%s%s %s [%s:%s]",
c.description,
Expand Down
20 changes: 20 additions & 0 deletions progressbar_test.go
Expand Up @@ -402,6 +402,26 @@ func TestOptionSetElapsedTime(t *testing.T) {
// - (5/-, 5 it/s)
}

func TestShowElapsedTimeOnFinish(t *testing.T) {
buf := strings.Builder{}
bar := NewOptions(10,
OptionShowElapsedTimeOnFinish(),
OptionSetWidth(10),
OptionSetWriter(&buf),
)

bar.Reset()
time.Sleep(3 * time.Second)
bar.Add(10)

result := strings.TrimSpace(buf.String())
expect := "100% |██████████| [3s]"

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

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

0 comments on commit be3c9b6

Please sign in to comment.