fix(iostreams): spinner env var is now in factory · cli/cli@3eba461 · GitHub
Skip to content

Commit 3eba461

Browse files
committed
fix(iostreams): spinner env var is now in factory
- Move env var evaluation to the factory defaults. - Use only the label for the spinner instead of adding "working..." every time. "working..." remains the default when no label is provided.
1 parent 3aadd51 commit 3eba461

4 files changed

Lines changed: 88 additions & 20 deletions

File tree

‎pkg/cmd/factory/default.go‎

Lines changed: 7 additions & 0 deletions

‎pkg/cmd/factory/default_test.go‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,56 @@ func Test_ioStreams_prompt(t *testing.T) {
432432
}
433433
}
434434

435+
func Test_ioStreams_spinnerDisabled(t *testing.T) {
436+
tests := []struct {
437+
name string
438+
spinnerDisabled bool
439+
env map[string]string
440+
}{
441+
{
442+
name: "default config",
443+
spinnerDisabled: false,
444+
},
445+
{
446+
name: "spinner disabled via GH_SPINNER_DISABLED env var = 0",
447+
env: map[string]string{"GH_SPINNER_DISABLED": "0"},
448+
spinnerDisabled: false,
449+
},
450+
{
451+
name: "spinner disabled via GH_SPINNER_DISABLED env var = false",
452+
env: map[string]string{"GH_SPINNER_DISABLED": "false"},
453+
spinnerDisabled: false,
454+
},
455+
{
456+
name: "spinner disabled via GH_SPINNER_DISABLED env var = no",
457+
env: map[string]string{"GH_SPINNER_DISABLED": "no"},
458+
spinnerDisabled: false,
459+
},
460+
{
461+
name: "spinner enabled via GH_SPINNER_DISABLED env var = 1",
462+
env: map[string]string{"GH_SPINNER_DISABLED": "1"},
463+
spinnerDisabled: true,
464+
},
465+
{
466+
name: "spinner enabled via GH_SPINNER_DISABLED env var = true",
467+
env: map[string]string{"GH_SPINNER_DISABLED": "true"},
468+
spinnerDisabled: true,
469+
},
470+
}
471+
for _, tt := range tests {
472+
t.Run(tt.name, func(t *testing.T) {
473+
if tt.env != nil {
474+
for k, v := range tt.env {
475+
t.Setenv(k, v)
476+
}
477+
}
478+
f := New("1")
479+
io := ioStreams(f)
480+
assert.Equal(t, tt.spinnerDisabled, io.GetSpinnerDisabled())
481+
})
482+
}
483+
}
484+
435485
func Test_ioStreams_colorLabels(t *testing.T) {
436486
tests := []struct {
437487
name string

‎pkg/iostreams/iostreams.go‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"os"
99
"os/exec"
1010
"os/signal"
11-
"slices"
1211
"strings"
1312
"sync"
1413
"time"
@@ -79,7 +78,8 @@ type IOStreams struct {
7978
pagerCommand string
8079
pagerProcess *os.Process
8180

82-
neverPrompt bool
81+
neverPrompt bool
82+
spinnerDisabled bool
8383

8484
TempFileOverride *os.File
8585
}
@@ -274,6 +274,14 @@ func (s *IOStreams) SetNeverPrompt(v bool) {
274274
s.neverPrompt = v
275275
}
276276

277+
func (s *IOStreams) GetSpinnerDisabled() bool {
278+
return s.spinnerDisabled
279+
}
280+
281+
func (s *IOStreams) SetSpinnerDisabled(v bool) {
282+
s.spinnerDisabled = v
283+
}
284+
277285
func (s *IOStreams) StartProgressIndicator() {
278286
s.StartProgressIndicatorWithLabel("")
279287
}
@@ -295,21 +303,28 @@ func (s *IOStreams) StartProgressIndicatorWithLabel(label string) {
295303
return
296304
}
297305

298-
spinnerDisabledValue, spinnerDisabledIsSet := os.LookupEnv("GH_SPINNER_DISABLED")
299-
falseyValues := []string{"false", "0", "no", ""}
300-
301306
var spinnerStyle []string
302-
if spinnerDisabledIsSet && !slices.Contains(falseyValues, spinnerDisabledValue) {
303-
// This progress indicator must be readable by screen readers
304-
spinnerStyle = []string{"Working..."}
307+
if s.spinnerDisabled {
308+
// Default label when spinner disabled is "Working..."
309+
if label == "" {
310+
label = "Working..."
311+
}
312+
313+
// Add an ellipsis to the label if it doesn't already have one.
314+
ellipsis := "..."
315+
if !strings.HasSuffix(label, ellipsis) {
316+
label = label + ellipsis
317+
}
318+
319+
spinnerStyle = []string{label}
305320
} else {
306321
// https://github.com/briandowns/spinner#available-character-sets
307322
// ⣾ ⣷ ⣽ ⣻ ⡿
308323
spinnerStyle = spinner.CharSets[11]
309324
}
310325

311326
sp := spinner.New(spinnerStyle, 120*time.Millisecond, spinner.WithWriter(s.ErrOut), spinner.WithColor("fgCyan"))
312-
if label != "" {
327+
if label != "" && !s.spinnerDisabled {
313328
sp.Prefix = label + " "
314329
}
315330

‎pkg/iostreams/iostreams_progress_indicator_test.go‎

Lines changed: 7 additions & 11 deletions

0 commit comments

Comments
 (0)