Browse Source

Merge pull request #239 from docker/fix-windows-progress

Simpler progress on windows
Djordje Lukic 5 years ago
parent
commit
56bfc5e6b9
3 changed files with 35 additions and 17 deletions
  1. 16 5
      progress/spinner.go
  2. 13 9
      progress/tty.go
  3. 6 3
      progress/tty_test.go

+ 16 - 5
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,
 	}
 }
 

+ 13 - 9
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 {

+ 6 - 3
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)
 }