-
Notifications
You must be signed in to change notification settings - Fork 18
/
interactive.go
119 lines (110 loc) · 3.38 KB
/
interactive.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 main
import (
"fmt"
"github.com/briandowns/spinner"
"github.com/subpop/go-log"
"github.com/urfave/cli/v2"
"os"
"text/tabwriter"
"time"
)
const (
colorGreen = "\u001B[32m"
colorYellow = "\u001B[33m"
colorRed = "\u001B[31m"
colorReset = "\u001B[0m"
)
// userInterfaceSettings manages standard output preference.
// It tracks colors, icons and machine-readable output (e.g. json).
//
// It is instantiated via uiSettings by calling configureUISettings.
type userInterfaceSettings struct {
// isMachineReadable describes the machine-readable mode (e.g., `--format json`)
isMachineReadable bool
// isRich describes the ability to display colors and animations
isRich bool
iconOK string
iconInfo string
iconError string
}
// uiSettings is an instance that keeps actual data of output preference.
//
// It is managed by calling the configureUISettings method.
var uiSettings = userInterfaceSettings{}
// configureUISettings is called by the CLI library when it loads up.
// It sets up the uiSettings object.
func configureUISettings(ctx *cli.Context) {
if ctx.Bool("no-color") {
uiSettings = userInterfaceSettings{
isRich: false,
isMachineReadable: false,
iconOK: "✓",
iconInfo: "·",
iconError: "𐄂",
}
} else {
uiSettings = userInterfaceSettings{
isRich: true,
isMachineReadable: false,
iconOK: colorGreen + "●" + colorReset,
iconInfo: colorYellow + "●" + colorReset,
iconError: colorRed + "●" + colorReset,
}
}
}
// showProgress calls function and, when it is possible display spinner with
// some progress message.
func showProgress(
progressMessage string,
function func() error,
) error {
var s *spinner.Spinner
if uiSettings.isRich {
s = spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.Suffix = progressMessage
s.Start()
// Stop spinner after running function
defer func() { s.Stop() }()
}
return function()
}
// showTimeDuration shows table with duration of each sub-action
func showTimeDuration(durations map[string]time.Duration) {
if log.CurrentLevel() >= log.LevelDebug {
fmt.Println()
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
_, _ = fmt.Fprintln(w, "STEP\tDURATION\t")
for step, duration := range durations {
_, _ = fmt.Fprintf(w, "%v\t%v\t\n", step, duration.Truncate(time.Millisecond))
}
_ = w.Flush()
}
}
// showErrorMessages shows table with all error messages gathered during action
func showErrorMessages(action string, errorMessages map[string]LogMessage) error {
if hasPriorityErrors(errorMessages, log.CurrentLevel()) {
if !uiSettings.isMachineReadable {
fmt.Println()
fmt.Printf("The following errors were encountered during %s:\n\n", action)
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
_, _ = fmt.Fprintln(w, "TYPE\tSTEP\tERROR\t")
for step, logMsg := range errorMessages {
if logMsg.level <= log.CurrentLevel() {
_, _ = fmt.Fprintf(w, "%v\t%v\t%v\n", logMsg.level, step, logMsg.message)
}
}
_ = w.Flush()
if hasPriorityErrors(errorMessages, log.LevelError) {
return cli.Exit("", 1)
}
}
}
return nil
}
// interactivePrintf is method for printing human-readable output. It suppresses output, when
// machine-readable format is used.
func interactivePrintf(format string, a ...interface{}) {
if !uiSettings.isMachineReadable {
fmt.Printf(format, a...)
}
}