Simpler progress on windows

* no colors
* no braille spinner
This commit is contained in:
Djordje Lukic 2020-06-18 14:16:14 +02:00
parent d6084e1b6e
commit cee5b697bb
3 changed files with 35 additions and 17 deletions

View File

@ -1,6 +1,9 @@
package progress package progress
import "time" import (
"runtime"
"time"
)
type spinner struct { type spinner struct {
time time.Time time time.Time
@ -11,13 +14,21 @@ type spinner struct {
} }
func newSpinner() *spinner { func newSpinner() *spinner {
chars := []string{
"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏",
}
done := "⠿"
if runtime.GOOS == "windows" {
chars = []string{"-"}
done = "-"
}
return &spinner{ return &spinner{
index: 0, index: 0,
time: time.Now(), time: time.Now(),
chars: []string{ chars: chars,
"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏", done: done,
},
done: "⠿",
} }
} }

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"runtime"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -102,7 +103,7 @@ func (w *ttyWriter) print() {
numLines := 0 numLines := 0
for _, v := range w.eventIDs { for _, v := range w.eventIDs {
line := lineText(w.events[v], terminalWidth, statusPadding) line := lineText(w.events[v], terminalWidth, statusPadding, runtime.GOOS != "windows")
// nolint: errcheck // nolint: errcheck
fmt.Fprint(w.out, line) fmt.Fprint(w.out, line)
numLines++ numLines++
@ -111,7 +112,7 @@ func (w *ttyWriter) print() {
w.numLines = numLines w.numLines = numLines
} }
func lineText(event Event, terminalWidth, statusPadding int) string { func lineText(event Event, terminalWidth, statusPadding int, color bool) string {
endTime := time.Now() endTime := time.Now()
if event.Status != Working { if event.Status != Working {
endTime = event.endTime endTime = event.endTime
@ -134,15 +135,18 @@ func lineText(event Event, terminalWidth, statusPadding int) string {
timer := fmt.Sprintf("%.1fs\n", elapsed) timer := fmt.Sprintf("%.1fs\n", elapsed)
o := align(text, timer, terminalWidth) o := align(text, timer, terminalWidth)
color := aec.WhiteF if color {
if event.Status == Done { color := aec.WhiteF
color = aec.BlueF if event.Status == Done {
} color = aec.BlueF
if event.Status == Error { }
color = aec.RedF 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 { func numDone(events map[string]Event) int {

View File

@ -24,14 +24,17 @@ func TestLineText(t *testing.T) {
lineWidth := len(fmt.Sprintf("%s %s", ev.ID, ev.Text)) 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) 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 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) assert.Equal(t, "\x1b[34m . id Text Status 0.0s\n\x1b[0m", out)
ev.Status = Error 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) assert.Equal(t, "\x1b[31m . id Text Status 0.0s\n\x1b[0m", out)
} }