/
progress.go
119 lines (101 loc) · 2.58 KB
/
progress.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package output
import (
"fmt"
"strings"
"time"
)
const moveCaretBackEscapeSequence = "\x1b[%dD" // %d is the number of characters to move back
var SpinnerFrames = []string{`|`, `/`, `-`, `\`}
type Spinner struct {
frame int
frames []string
out Outputer
stop chan struct{}
interval time.Duration
reportedError bool
}
var _ Marshaller = &Spinner{}
func (d *Spinner) MarshalOutput(f Format) interface{} {
return d.frames[d.frame]
}
func StartSpinner(out Outputer, msg string, interval time.Duration) *Spinner {
frames := []string{".", "..", "..."}
if out.Config().Interactive {
frames = []string{`|`, `/`, `-`, `\`}
}
d := &Spinner{0, frames, out, make(chan struct{}, 1), interval, false}
if msg != "" && d.out.Type() == PlainFormatName {
d.out.Fprint(d.out.Config().ErrWriter, strings.TrimSuffix(msg, " ")+" ")
}
if d.out.Type() == PlainFormatName { // Nothing will be printer otherwise
go func() {
d.ticker()
}()
}
return d
}
func (d *Spinner) moveCaretBack() int {
if !d.out.Config().Interactive {
return 0
}
prevPos := d.frame - 1
if prevPos < 0 {
prevPos = len(d.frames) - 1
}
prevFrame := d.frames[prevPos]
if d.out.Config().ShellName != "cmd" { // cannot use subshell/cmd.Name due to import cycle
d.moveCaretBackInTerminal(len(prevFrame))
} else {
d.moveCaretBackInCommandPrompt(len(prevFrame))
}
return len(prevFrame)
}
func (d *Spinner) tick(isFirstFrame bool) {
clear := ""
if !isFirstFrame {
nMoved := d.moveCaretBack()
if nMoved > 0 {
clear = strings.Repeat(" ", nMoved-1)
}
}
d.out.Fprint(d.out.Config().ErrWriter, d.frames[d.frame]+clear)
d.frame++
if d.frame == len(d.frames) {
d.frame = 0
}
}
func (d *Spinner) ticker() {
d.tick(true) // print the first frame immediately
ticker := time.NewTicker(d.interval)
for {
select {
case <-ticker.C:
d.tick(false)
case <-d.stop:
return
}
}
}
func (d *Spinner) Stop(msg string) {
d.stop <- struct{}{}
close(d.stop)
// We're done, so remove the last spinner frame
if d.out.Config().Interactive {
nMoved := d.moveCaretBack()
if nMoved > len(msg) {
msg += strings.Repeat(" ", nMoved-len(msg)-1)
}
}
if msg != "" && d.out.Type() == PlainFormatName {
if !d.out.Config().Interactive {
d.out.Fprint(d.out.Config().ErrWriter, " ")
}
d.out.Fprint(d.out.Config().ErrWriter, strings.TrimPrefix(msg, " "))
}
if d.out.Type() == PlainFormatName {
d.out.Fprint(d.out.Config().ErrWriter, "\n")
}
}
func (d *Spinner) moveCaretBackInTerminal(n int) {
d.out.Fprint(d.out.Config().ErrWriter, fmt.Sprintf(moveCaretBackEscapeSequence, n))
}