diff --git a/progress/tty.go b/progress/tty.go index daef04b51..caac150d3 100644 --- a/progress/tty.go +++ b/progress/tty.go @@ -67,14 +67,17 @@ func (w *ttyWriter) Event(e Event) { w.eventIDs = append(w.eventIDs, e.ID) } if _, ok := w.events[e.ID]; ok { - event := w.events[e.ID] - if event.Status != Done && e.Status == Done { - event.stop() + last := w.events[e.ID] + switch e.Status { + case Done, Error: + if last.Status != e.Status { + last.stop() + } } - event.Status = e.Status - event.Text = e.Text - event.StatusText = e.StatusText - w.events[e.ID] = event + last.Status = e.Status + last.Text = e.Text + last.StatusText = e.StatusText + w.events[e.ID] = last } else { e.startTime = time.Now() e.spinner = newSpinner() diff --git a/progress/tty_test.go b/progress/tty_test.go index 0a10eb45c..eac286dbf 100644 --- a/progress/tty_test.go +++ b/progress/tty_test.go @@ -18,6 +18,7 @@ package progress import ( "fmt" + "sync" "testing" "time" @@ -54,3 +55,32 @@ func TestLineText(t *testing.T) { out = lineText(ev, 50, lineWidth, true) assert.Equal(t, "\x1b[31m . id Text Status 0.0s\n\x1b[0m", out) } + +func TestErrorEvent(t *testing.T) { + w := &ttyWriter{ + events: map[string]Event{}, + mtx: &sync.RWMutex{}, + } + e := Event{ + ID: "id", + Text: "Text", + Status: Working, + StatusText: "Working", + startTime: time.Now(), + spinner: &spinner{ + chars: []string{"."}, + }, + } + // Fire "Working" event and check end time isn't touched + w.Event(e) + event, ok := w.events[e.ID] + assert.True(t, ok) + assert.True(t, event.endTime.Equal(time.Time{})) + + // Fire "Error" event and check end time is set + e.Status = Error + w.Event(e) + event, ok = w.events[e.ID] + assert.True(t, ok) + assert.True(t, event.endTime.After(time.Now().Add(-10*time.Second))) +}