-
Notifications
You must be signed in to change notification settings - Fork 69
/
spinner_progress_indicator.go
121 lines (100 loc) · 2.75 KB
/
spinner_progress_indicator.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
package ux
import (
"fmt"
"strings"
"time"
spinnerLib "github.com/briandowns/spinner"
"github.com/fatih/color"
log "github.com/sirupsen/logrus"
"github.com/newrelic/newrelic-cli/internal/config"
)
type SpinnerProgressIndicator struct {
*spinnerLib.Spinner
showSpinner bool
}
func NewSpinnerProgressIndicator() *SpinnerProgressIndicator {
s := &SpinnerProgressIndicator{}
s.Spinner = spinnerLib.New(spinnerLib.CharSets[4], 750*time.Millisecond)
_ = s.Spinner.Color("green")
s.Spinner.HideCursor = true
s.showSpinner = true
return s
}
func (s *SpinnerProgressIndicator) ShowSpinner(ss bool) {
s.showSpinner = ss
}
func (s *SpinnerProgressIndicator) Start(msg string) {
// Suppress spinner output when logging at debug or trace level.
// Output is garbled when verbose log messages are sent during an active spinner.
if !config.Logger.IsLevelEnabled(log.DebugLevel) && s.showSpinner {
dots := ""
s.Spinner.PostUpdate = func(s *spinnerLib.Spinner) {
if dots == ".." {
dots = ""
}
dots += "."
s.Suffix = fmt.Sprintf(" %s%s", msg, dots)
}
s.Spinner.Start() // Start the spinner
} else {
c := color.New(color.FgCyan)
c.Printf("==>")
x := color.New(color.Bold)
x.Printf(" %s", msg)
fmt.Println()
}
}
func (s *SpinnerProgressIndicator) Stop() {
if !config.Logger.IsLevelEnabled(log.DebugLevel) && s.showSpinner {
s.Suffix = ""
s.Spinner.Stop()
}
}
func (s *SpinnerProgressIndicator) Fail(msg string) {
msg = fmt.Sprintf("%v %s\n", IconError, msg)
if !config.Logger.IsLevelEnabled(log.DebugLevel) && s.showSpinner {
s.Suffix = ""
s.FinalMSG = msg
s.Spinner.Stop()
} else {
fmt.Print(msg)
}
printInstallFinalMessage("Failed", color.BgMagenta)
}
func (s *SpinnerProgressIndicator) Success(msg string) {
msg = fmt.Sprintf("%v %s\n", IconSuccess, msg)
if !config.Logger.IsLevelEnabled(log.DebugLevel) && s.showSpinner {
s.Suffix = ""
s.FinalMSG = msg
s.Spinner.Stop()
} else {
fmt.Print(msg)
}
if strings.Contains(msg, "Complete!") {
fmt.Println()
return
} else if strings.Contains(msg, "Installing") {
printInstallFinalMessage("Installed", color.BgGreen)
} else {
printInstallFinalMessage("Connected", color.BgGreen)
}
}
func printInstallFinalMessage(printText string, bgColor color.Attribute) {
white := color.New(color.FgWhite)
boldWhite := white.Add(color.Bold)
background := boldWhite.Add(bgColor)
fmt.Print(" ")
background.Print(fmt.Sprintf(" %s ", printText))
fmt.Println()
}
func (s *SpinnerProgressIndicator) Canceled(msg string) {
msg = fmt.Sprintf("%v %s\n", IconExclamation, msg)
if !config.Logger.IsLevelEnabled(log.DebugLevel) && s.showSpinner {
s.Suffix = ""
s.FinalMSG = msg
s.Spinner.Stop()
} else {
fmt.Print(msg)
}
printInstallFinalMessage("Cancelled", color.BgBlue)
}