From cee5b697bb89219a12690e276308e8b0588d5a4a Mon Sep 17 00:00:00 2001 From: Djordje Lukic Date: Thu, 18 Jun 2020 14:16:14 +0200 Subject: [PATCH] Simpler progress on windows * no colors * no braille spinner --- progress/spinner.go | 21 ++++++++++++++++----- progress/tty.go | 22 +++++++++++++--------- progress/tty_test.go | 9 ++++++--- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/progress/spinner.go b/progress/spinner.go index 8abb9821b..695a56429 100644 --- a/progress/spinner.go +++ b/progress/spinner.go @@ -1,6 +1,9 @@ package progress -import "time" +import ( + "runtime" + "time" +) type spinner struct { time time.Time @@ -11,13 +14,21 @@ type spinner struct { } func newSpinner() *spinner { + chars := []string{ + "⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏", + } + done := "⠿" + + if runtime.GOOS == "windows" { + chars = []string{"-"} + done = "-" + } + return &spinner{ index: 0, time: time.Now(), - chars: []string{ - "⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏", - }, - done: "⠿", + chars: chars, + done: done, } } diff --git a/progress/tty.go b/progress/tty.go index 591c2a11d..56b109ef9 100644 --- a/progress/tty.go +++ b/progress/tty.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "io" + "runtime" "strings" "sync" "time" @@ -102,7 +103,7 @@ func (w *ttyWriter) print() { numLines := 0 for _, v := range w.eventIDs { - line := lineText(w.events[v], terminalWidth, statusPadding) + line := lineText(w.events[v], terminalWidth, statusPadding, runtime.GOOS != "windows") // nolint: errcheck fmt.Fprint(w.out, line) numLines++ @@ -111,7 +112,7 @@ func (w *ttyWriter) print() { w.numLines = numLines } -func lineText(event Event, terminalWidth, statusPadding int) string { +func lineText(event Event, terminalWidth, statusPadding int, color bool) string { endTime := time.Now() if event.Status != Working { endTime = event.endTime @@ -134,15 +135,18 @@ func lineText(event Event, terminalWidth, statusPadding int) string { timer := fmt.Sprintf("%.1fs\n", elapsed) o := align(text, timer, terminalWidth) - color := aec.WhiteF - if event.Status == Done { - color = aec.BlueF - } - if event.Status == Error { - color = aec.RedF + if color { + color := aec.WhiteF + if event.Status == Done { + color = aec.BlueF + } + if event.Status == Error { + color = aec.RedF + } + return aec.Apply(o, color) } - return aec.Apply(o, color) + return o } func numDone(events map[string]Event) int { diff --git a/progress/tty_test.go b/progress/tty_test.go index bb7d31b9f..d080b37ba 100644 --- a/progress/tty_test.go +++ b/progress/tty_test.go @@ -24,14 +24,17 @@ func TestLineText(t *testing.T) { lineWidth := len(fmt.Sprintf("%s %s", ev.ID, ev.Text)) - out := lineText(ev, 50, lineWidth) + out := lineText(ev, 50, lineWidth, true) assert.Equal(t, "\x1b[37m . id Text Status 0.0s\n\x1b[0m", out) + out = lineText(ev, 50, lineWidth, false) + assert.Equal(t, " . id Text Status 0.0s\n", out) + ev.Status = Done - out = lineText(ev, 50, lineWidth) + out = lineText(ev, 50, lineWidth, true) assert.Equal(t, "\x1b[34m . id Text Status 0.0s\n\x1b[0m", out) ev.Status = Error - out = lineText(ev, 50, lineWidth) + out = lineText(ev, 50, lineWidth, true) assert.Equal(t, "\x1b[31m . id Text Status 0.0s\n\x1b[0m", out) }