From f0dbc606767c7df298f419aff38f93ac2627c8a7 Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Wed, 3 Feb 2021 15:18:16 +0100 Subject: [PATCH] Avoid negative elapsed the display when only one event is sent. Signed-off-by: Guillaume Tardif --- api/progress/tty.go | 5 ++++- api/progress/tty_test.go | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/api/progress/tty.go b/api/progress/tty.go index 1b7fc74df..5465e7e0c 100644 --- a/api/progress/tty.go +++ b/api/progress/tty.go @@ -154,7 +154,10 @@ func (w *ttyWriter) print() { func lineText(event Event, pad string, terminalWidth, statusPadding int, color bool) string { endTime := time.Now() if event.Status != Working { - endTime = event.endTime + endTime = event.startTime + if (event.endTime != time.Time{}) { + endTime = event.endTime + } } elapsed := endTime.Sub(event.startTime).Seconds() diff --git a/api/progress/tty_test.go b/api/progress/tty_test.go index 5907ebd64..d95af857d 100644 --- a/api/progress/tty_test.go +++ b/api/progress/tty_test.go @@ -56,6 +56,25 @@ func TestLineText(t *testing.T) { assert.Equal(t, out, "\x1b[31m . id Text Status 0.0s\n\x1b[0m") } +func TestLineTextSingleEvent(t *testing.T) { + now := time.Now() + ev := Event{ + ID: "id", + Text: "Text", + Status: Done, + StatusText: "Status", + startTime: now, + spinner: &spinner{ + chars: []string{"."}, + }, + } + + lineWidth := len(fmt.Sprintf("%s %s", ev.ID, ev.Text)) + + out := lineText(ev, "", 50, lineWidth, true) + assert.Equal(t, out, "\x1b[34m . id Text Status 0.0s\n\x1b[0m") +} + func TestErrorEvent(t *testing.T) { w := &ttyWriter{ events: map[string]Event{},