浏览代码

Avoid negative elapsed the display when only one event is sent.

Signed-off-by: Guillaume Tardif <[email protected]>
Guillaume Tardif 4 年之前
父节点
当前提交
f0dbc60676
共有 2 个文件被更改,包括 23 次插入1 次删除
  1. 4 1
      api/progress/tty.go
  2. 19 0
      api/progress/tty_test.go

+ 4 - 1
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()

+ 19 - 0
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{},