Skip to content

Commit 71b0237

Browse files
authored
Merge pull request #9855 from alonyb/w/add-spinner
Add spinner at preparing Kubernetes...
2 parents b84f249 + 2d80cfc commit 71b0237

File tree

6 files changed

+58
-17
lines changed

6 files changed

+58
-17
lines changed

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
1313
github.com/VividCortex/godaemon v0.0.0-20201030160542-15e3f4925a21
1414
github.com/blang/semver v3.5.0+incompatible
15+
github.com/briandowns/spinner v1.11.1
1516
github.com/c4milo/gotoolkit v0.0.0-20170318115440-bcc06269efa9 // indirect
1617
github.com/cenkalti/backoff v2.2.1+incompatible
1718
github.com/cenkalti/backoff/v4 v4.1.0
@@ -104,6 +105,7 @@ require (
104105

105106
replace (
106107
git.apache.org/thrift.git => github.com/apache/thrift v0.0.0-20180902110319-2566ecd5d999
108+
github.com/briandowns/spinner => github.com/alonyb/spinner v1.12.1
107109
github.com/docker/docker => github.com/docker/docker v1.4.2-0.20190924003213-a8608b5b67c7
108110
github.com/docker/machine => github.com/machine-drivers/machine v0.7.1-0.20200810185219-7d42fed1b770
109111
github.com/google/go-containerregistry => github.com/afbjorklund/go-containerregistry v0.0.0-20200902152226-fbad78ec2813

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZq
143143
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
144144
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
145145
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
146+
github.com/alonyb/spinner v1.12.1 h1:zB6IQ29/kTR/NWHJhIgU2tXW+fhXa3K5zrDQMddd9H0=
147+
github.com/alonyb/spinner v1.12.1/go.mod h1:QOuQk7x+EaDASo80FEXwlwiA+j/PPIcX3FScO+3/ZPQ=
146148
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
147149
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
148150
github.com/apex/log v1.1.4/go.mod h1:AlpoD9aScyQfJDVHmLMEcx4oU6LqzkWp4Mg9GdAcEvQ=

pkg/minikube/out/out.go

+39-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ import (
2626
"os"
2727
"strconv"
2828
"strings"
29+
"time"
2930

31+
"github.com/briandowns/spinner"
3032
isatty "github.com/mattn/go-isatty"
3133

3234
"k8s.io/klog/v2"
@@ -58,10 +60,14 @@ var (
5860
OverrideEnv = "MINIKUBE_IN_STYLE"
5961
// JSON is whether or not we should output stdout in JSON format. Set using SetJSON()
6062
JSON = false
63+
// spin is spinner showed at starting minikube
64+
spin = spinner.New(spinner.CharSets[style.SpinnerCharacter], 100*time.Millisecond)
6165
)
6266

6367
// MaxLogEntries controls the number of log entries to show for each source
64-
const MaxLogEntries = 3
68+
const (
69+
MaxLogEntries = 3
70+
)
6571

6672
// fdWriter is the subset of file.File that implements io.Writer and Fd()
6773
type fdWriter interface {
@@ -78,18 +84,22 @@ func Step(st style.Enum, format string, a ...V) {
7884
Infof(format, a...)
7985
return
8086
}
81-
outStyled := stylized(st, useColor, format, a...)
87+
outStyled, spinner := stylized(st, useColor, format, a...)
8288
if JSON {
8389
register.PrintStep(outStyled)
8490
return
8591
}
8692
register.RecordStep(outStyled)
87-
String(outStyled)
93+
if spinner {
94+
spinnerString(outStyled)
95+
} else {
96+
String(outStyled)
97+
}
8898
}
8999

90100
// Infof is used for informational logs (options, env variables, etc)
91101
func Infof(format string, a ...V) {
92-
outStyled := stylized(style.Option, useColor, format, a...)
102+
outStyled, _ := stylized(style.Option, useColor, format, a...)
93103
if JSON {
94104
register.PrintInfo(outStyled)
95105
return
@@ -106,13 +116,34 @@ func String(format string, a ...interface{}) {
106116
klog.Warningf("[unset outFile]: %s", fmt.Sprintf(format, a...))
107117
return
108118
}
109-
110119
klog.Infof(format, a...)
120+
// if spin is active from a previous step, it will stop spinner displaying
121+
if spin.Active() {
122+
spin.Stop()
123+
}
124+
_, err := fmt.Fprintf(outFile, format, a...)
125+
if err != nil {
126+
klog.Errorf("Fprintf failed: %v", err)
127+
}
128+
}
111129

130+
// spinnerString writes a basic formatted string to stdout with spinner character
131+
func spinnerString(format string, a ...interface{}) {
132+
// Flush log buffer so that output order makes sense
133+
klog.Flush()
134+
135+
if outFile == nil {
136+
klog.Warningf("[unset outFile]: %s", fmt.Sprintf(format, a...))
137+
return
138+
}
139+
140+
klog.Infof(format, a...)
112141
_, err := fmt.Fprintf(outFile, format, a...)
113142
if err != nil {
114143
klog.Errorf("Fprintf failed: %v", err)
115144
}
145+
// Start spinning at the end of the printed line
146+
spin.Start()
116147
}
117148

118149
// Ln writes a basic formatted string with a newline to stdout
@@ -126,7 +157,7 @@ func Ln(format string, a ...interface{}) {
126157

127158
// ErrT writes a stylized and templated error message to stderr
128159
func ErrT(st style.Enum, format string, a ...V) {
129-
errStyled := stylized(st, useColor, format, a...)
160+
errStyled, _ := stylized(st, useColor, format, a...)
130161
Err(errStyled)
131162
}
132163

@@ -169,7 +200,8 @@ func FatalT(format string, a ...V) {
169200
// WarningT is a shortcut for writing a templated warning message to stderr
170201
func WarningT(format string, a ...V) {
171202
if JSON {
172-
register.PrintWarning(stylized(style.Warning, useColor, format, a...))
203+
st, _ := stylized(style.Warning, useColor, format, a...)
204+
register.PrintWarning(st)
173205
return
174206
}
175207
ErrT(style.Warning, format, a...)

pkg/minikube/out/out_style.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func applyPrefix(prefix, format string) string {
3131
}
3232

3333
// applyStyle translates the given string if necessary then adds any appropriate style prefix.
34-
func applyStyle(st style.Enum, useColor bool, format string) string {
34+
func applyStyle(st style.Enum, useColor bool, format string) (string, bool) {
3535
format = translate.T(format)
3636

3737
s, ok := style.Config[st]
@@ -41,20 +41,21 @@ func applyStyle(st style.Enum, useColor bool, format string) string {
4141

4242
// Similar to CSS styles, if no style matches, output an unformatted string.
4343
if !ok || JSON {
44-
return format
44+
return format, s.Spinner
4545
}
4646

4747
if !useColor {
48-
return applyPrefix(style.LowPrefix(s), format)
48+
return applyPrefix(style.LowPrefix(s), format), s.Spinner
4949
}
50-
return applyPrefix(s.Prefix, format)
50+
return applyPrefix(s.Prefix, format), s.Spinner
5151
}
5252

5353
// stylized applies formatting to the provided template
54-
func stylized(st style.Enum, useColor bool, format string, a ...V) string {
54+
func stylized(st style.Enum, useColor bool, format string, a ...V) (string, bool) {
55+
var spinner bool
5556
if a == nil {
5657
a = []V{{}}
5758
}
58-
format = applyStyle(st, useColor, format)
59-
return Fmt(format, a...)
59+
format, spinner = applyStyle(st, useColor, format)
60+
return Fmt(format, a...), spinner
6061
}

pkg/minikube/out/out_style_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func TestApplyStyle(t *testing.T) {
8383
}
8484
for _, test := range tests {
8585
t.Run(test.description, func(t *testing.T) {
86-
rawGot := applyStyle(test.styleEnum, test.useColor, test.format)
86+
rawGot, _ := applyStyle(test.styleEnum, test.useColor, test.format)
8787
got := strings.TrimSpace(rawGot)
8888
if got != test.expected {
8989
t.Errorf("Expected '%v' but got '%v'", test.expected, got)
@@ -139,7 +139,7 @@ func TestApplyTemplateFormating(t *testing.T) {
139139
}
140140
for _, test := range tests {
141141
t.Run(test.description, func(t *testing.T) {
142-
rawGot := stylized(test.styleEnum, test.useColor, test.format, test.a...)
142+
rawGot, _ := stylized(test.styleEnum, test.useColor, test.format, test.a...)
143143
got := strings.TrimSpace(rawGot)
144144
if got != test.expected {
145145
t.Errorf("Expected '%v' but got '%v'", test.expected, got)

pkg/minikube/style/style.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@ type Options struct {
4141
LowPrefix string
4242
// OmitNewline omits a newline at the end of a message.
4343
OmitNewline bool
44+
// Spinner is a character to place at ending of message
45+
Spinner bool
4446
}
4547

48+
const SpinnerCharacter = 9
49+
4650
// Config is a map of style name to style struct
4751
// For consistency, ensure that emojis added render with the same width across platforms.
4852
var Config = map[Enum]Options{
@@ -104,7 +108,7 @@ var Config = map[Enum]Options{
104108
Copying: {Prefix: "✨ "},
105109
CRIO: {Prefix: "🎁 "}, // This should be a snow-flake, but the emoji has a strange width on macOS
106110
DeletingHost: {Prefix: "🔥 "},
107-
Docker: {Prefix: "🐳 "},
111+
Docker: {Prefix: "🐳 ", OmitNewline: true, Spinner: true},
108112
DryRun: {Prefix: "🌵 "},
109113
Enabling: {Prefix: "🔌 "},
110114
FileDownload: {Prefix: "💾 "},

0 commit comments

Comments
 (0)