This repository has been archived by the owner on Jul 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathwow.go
163 lines (145 loc) · 2.94 KB
/
wow.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package wow
import (
"context"
"fmt"
"io"
"os"
"runtime"
"sync"
"time"
"github.com/gernest/wow/spin"
"golang.org/x/crypto/ssh/terminal"
)
//go:generate go run ./scripts/generate_spin.go
const erase = "\033[2K\r"
// Wow writes beautiful spinners on the terminal.
type Wow struct {
txt string
s spin.Spinner
out io.Writer
running bool
done func()
mu sync.RWMutex
IsTerminal bool
}
// New creates a new wow instance ready to start spinning.
func New(o io.Writer, s spin.Spinner, text string, options ...func(*Wow)) *Wow {
isTerminal := terminal.IsTerminal(int(os.Stdout.Fd()))
wow := Wow{out: o, s: s, txt: text, IsTerminal: isTerminal}
for _, option := range options {
option(&wow)
}
return &wow
}
func (w *Wow) run(ctx context.Context) {
t := time.NewTicker(time.Duration(w.s.Interval) * time.Millisecond)
at := 0
for {
select {
case <-ctx.Done():
t.Stop()
return
case <-t.C:
txt := erase + w.s.Frames[at%len(w.s.Frames)] + w.txt
if w.IsTerminal && w.running {
fmt.Fprint(w.out, txt)
}
at++
}
}
}
// Start starts the spinner. The frames are written based on the spinner
// interval.
func (w *Wow) Start() {
if !w.running {
ctx, done := context.WithCancel(context.Background())
w.done = done
w.running = true
go w.run(ctx)
}
}
// Stop stops the spinner
func (w *Wow) Stop() {
w.running = false
if w.done != nil {
w.done()
}
}
// Spinner sets s to the current spinner
func (w *Wow) Spinner(s spin.Spinner) *Wow {
w.Stop()
w.s = s
w.Start()
return w
}
// Text adds text to the current spinner
func (w *Wow) Text(txt string) *Wow {
w.mu.Lock()
w.txt = txt
w.mu.Unlock()
return w
}
// Persist writes the last character of the currect spinner frames together with
// the text on stdout.
//
// A new line is added at the end to ensure the text stay that way.
func (w *Wow) Persist() {
w.Stop()
at := len(w.s.Frames) - 1
txt := erase + w.s.Frames[at] + w.txt + "\n"
if w.IsTerminal {
fmt.Fprint(w.out, txt)
}
}
// PersistWith writes the last frame of s together with text with a new line
// added to make it stick.
func (w *Wow) PersistWith(s spin.Spinner, text string) {
w.Stop()
var a string
if len(s.Frames) > 0 {
a = s.Frames[len(s.Frames)-1]
}
txt := erase + a + text + "\n"
if w.IsTerminal {
fmt.Fprint(w.out, txt)
}
}
// ForceOutput forces all output even if not not outputting directly to a terminal
func ForceOutput(w *Wow) {
w.IsTerminal = true
}
// LogSymbol is a log severity level
type LogSymbol uint
// common log symbos
const (
INFO LogSymbol = iota
SUCCESS
WARNING
ERROR
)
func (s LogSymbol) String() string {
if runtime.GOOS != "windows" {
switch s {
case INFO:
return "ℹ"
case SUCCESS:
return "✔"
case WARNING:
return "⚠"
case ERROR:
return "✖"
}
} else {
switch s {
case INFO:
return "i"
case SUCCESS:
return "v"
case WARNING:
return "!!"
case ERROR:
return "x"
}
}
return ""
}