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

feat: option to keep showing elapsed time on finish #133

Merged
merged 1 commit into from Sep 1, 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
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