This repository has been archived by the owner on Jun 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
printer.go
318 lines (272 loc) · 7.2 KB
/
printer.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package printer
import (
"fmt"
"io"
"log"
"os"
"sync"
"time"
"github.com/briandowns/spinner"
"github.com/ttacon/chalk"
"golang.org/x/crypto/ssh/terminal"
)
var instantiated *spinner.Spinner
var once sync.Once
// Set to true when testing, to never call os.Exit
var testing bool
var verbose bool
var color string
var spinnerStyle int
var previousProgressMessage string
var previousStepMessage string
var previousSubStepMessage string
var writer io.Writer
// Is the output a full terminal
var outputTTY bool
// Default way of terminating (from Fatal) is to os.Exit, but allow this to be
// changed for the purposes of testing.
var Terminate = ExitTerminate
func ExitTerminate(err error) {
os.Exit(1)
}
func PanicTerminate(err error) {
panic(err)
}
func init() {
verbose = false
color = "yellow"
spinnerStyle = 14
writer = os.Stdout
testing = false
if terminal.IsTerminal(int(os.Stdout.Fd())) {
outputTTY = true
} else {
outputTTY = false
}
}
// Init the spinner with a verbose flag, and color.
// This is optional, and allows some customisation over the printer.
// You should do invoke this as early as posisble, before the first printer function
// is called.
func Init(initVerbose bool, initColor string, initSpinner int, writer io.Writer) {
verbose = initVerbose
color = initColor
spinnerStyle = initSpinner
getPrinter()
}
// Test to set testing to true, to prevent exiting on Fatal errors
func Test() {
Terminate = PanicTerminate
}
// SetOutput to an io.Writer compatitble interface. Designed
// to capture error messages for use in testing
func SetOutput(out io.Writer) {
s := getPrinter()
s.Writer = out
}
// Create a singleton to the Spinner
// This ensures we only output to one line
func getPrinter() *spinner.Spinner {
if instantiated == nil {
once.Do(func() {
instantiated = spinner.New(spinner.CharSets[spinnerStyle], 100*time.Millisecond)
instantiated.Writer = writer
})
}
return instantiated
}
// Progress message with a spinner
func Progress(message string) {
if outputTTY {
spinner := getPrinter()
if message != previousProgressMessage {
previousProgressMessage = message
spinner.Suffix = fmt.Sprintf(" %s", message)
spinner.Color(color)
}
spinner.Start()
}
}
// Progress with formatted message
func Progressf(message string, args ...interface{}) {
Progress(
fmt.Sprintf(message, args),
)
}
// Step prints a line console and stops the spinner
func Step(message string) {
if outputTTY {
if message != previousStepMessage {
previousStepMessage = message
spinner := getPrinter()
spinner.Stop()
fmt.Println(fmt.Sprintf("%s %s", chalk.Yellow.Color("➜"), chalk.Bold.TextStyle(message)))
}
} else {
log.Print(message)
}
}
// Step with formatted message
func Stepf(message string, args ...interface{}) {
Step(
fmt.Sprintf(message, args),
)
}
// SubStep prints a line console, at a given indent and stops the spinner
// ignoreVerboseRule true, ensures the SubStep always prints
func SubStep(message string, indent int, last, ignoreVerboseRule bool) {
if outputTTY {
if message != previousSubStepMessage {
previousSubStepMessage = message
// Substeps are only printed if the verbose flag is set at init
// Unless it's the last substep
if verbose || ignoreVerboseRule || last {
var indentString string
for i := 1; i <= indent; i++ {
indentString = fmt.Sprintf(" %s", indentString)
}
icon := "├─"
if last {
icon = "└─"
}
spinner := getPrinter()
spinner.Stop()
fmt.Println(fmt.Sprintf("%s%s %s", chalk.Dim.TextStyle(indentString), chalk.Dim.TextStyle(icon), chalk.Dim.TextStyle(message)))
}
}
} else {
log.Print(message)
}
}
// SubStep with formatted message
// FIXME: The extra arguments for SubStep being between the format string and
// it's formatting arguments is rather awkward
func SubStepf(message string, indent int, last, ignoreVerboseRule bool, args ...interface{}) {
SubStep(
fmt.Sprintf(message, args),
indent,
last,
ignoreVerboseRule,
)
}
// Finish prints message to the console and stops the spinner with success.
// This is best used to indicated the end of a task
func Finish(message string) {
if outputTTY {
spinner := getPrinter()
spinner.Stop()
fmt.Println(fmt.Sprintf("%s %s", chalk.Green.Color("✔"), chalk.Bold.TextStyle(message)))
} else {
log.Printf("✔ %s", message)
}
}
// Finish with formatted message
func Finishf(message string, args ...interface{}) {
Finish(
fmt.Sprintf(message, args),
)
}
// Warn prints a warning to the screen. It's formatted like other errors, and coloured yellow.
func Warn(err error, resolution string, link string) {
if outputTTY {
spinner := getPrinter()
spinner.Stop()
errMessage := fmt.Sprintf(
"%s %s",
chalk.Bold.TextStyle(chalk.Yellow.Color("! Error:")),
chalk.Yellow.Color(err.Error()),
)
if resolution != "" {
errMessage = fmt.Sprintf(
"%s\n%s%s",
errMessage,
chalk.Dim.TextStyle(chalk.Bold.TextStyle("☞ Resolution: ")),
chalk.Dim.TextStyle(resolution),
)
}
if link != "" {
errMessage = fmt.Sprintf(
"%s\n%s%s",
errMessage,
chalk.Dim.TextStyle(chalk.Bold.TextStyle("∞ More info: ")),
chalk.Italic.TextStyle(link),
)
}
fmt.Println(errMessage)
} else {
log.Printf("WARN: %s", err.Error())
}
}
// Error prints an error to the screen. As it's intended reader is a user of your program,
// it expects both the error message, a way for the reader to resolve the error, and if
// possible a link to futher information.
// If the error doesn't have a link, pass a blank string ""
func Error(err error, resolution string, link string) {
if outputTTY {
spinner := getPrinter()
spinner.Stop()
errMessage := fmt.Sprintf(
"%s %s",
chalk.Bold.TextStyle(chalk.Red.Color("✖ Error:")),
chalk.Red.Color(err.Error()),
)
if resolution != "" {
errMessage = fmt.Sprintf(
"%s\n%s%s",
errMessage,
chalk.Dim.TextStyle(chalk.Bold.TextStyle("☞ Resolution: ")),
chalk.Dim.TextStyle(resolution),
)
}
if link != "" {
errMessage = fmt.Sprintf(
"%s\n%s%s",
errMessage,
chalk.Dim.TextStyle(chalk.Bold.TextStyle("∞ More info: ")),
chalk.Italic.TextStyle(link),
)
}
fmt.Println(errMessage)
} else {
log.Printf("ERROR: %s", err.Error())
}
}
// Fatal prints an error in the exact same way as Error, except it prefixes with "Fatal",
// and the function ends with a panic
func Fatal(err error, resolution string, link string) {
if outputTTY {
spinner := getPrinter()
errMessage := fmt.Sprintf(
"%s %s",
chalk.Bold.TextStyle(chalk.Red.Color("✖ Fatal:")),
chalk.Red.Color(err.Error()),
)
if resolution != "" {
errMessage = fmt.Sprintf(
"%s\n%s%s",
errMessage,
chalk.Dim.TextStyle(chalk.Bold.TextStyle("☞ Resolution: ")),
chalk.Dim.TextStyle(resolution),
)
}
// Add the link if a valid one was supplied
if link != "" {
errMessage = fmt.Sprintf(
"%s\n%s%s",
errMessage,
chalk.Dim.TextStyle(chalk.Bold.TextStyle("∞ More info: ")),
chalk.Italic.TextStyle(link),
)
}
spinner.Stop()
fmt.Println(errMessage)
} else {
log.Printf("FATAL: %s", err.Error())
}
Terminate(err)
}
// Stop the spinner
func Stop() {
spinner := getPrinter()
spinner.Stop()
}