Skip to content

Commit

Permalink
Merge pull request #50 from theckman/hide_cursor_by_default
Browse files Browse the repository at this point in the history
Hide cursor by default when animating spinner
  • Loading branch information
theckman committed Dec 15, 2021
2 parents 5a67d42 + ae0e587 commit b6ca861
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
22 changes: 21 additions & 1 deletion spinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,24 @@ type Config struct {
// defaults to os.Stdout.
Writer io.Writer

// ShowCursor specifies that the cursor should be shown by the spinner while
// animating. If it is not shown, the cursor will be restored when the
// spinner stops. This can't be changed after the *Spinner has been
// constructed.
//
// Please note, if you don't show the cursor and the program crashes or is
// killed, you may need to reset your terminal for the cursor to appear
// again.
ShowCursor bool

// HideCursor describes whether the cursor should be hidden by the spinner
// while animating. If it is hidden, it will be restored when the spinner
// stops. This can't be changed after the *Spinner has been constructed.
//
// Please note, if the program crashes or is killed you may need to reset
// your terminal for the cursor to appear again.
//
// Deprecated: use ShowCursor instead.
HideCursor bool

// SpinnerAtEnd configures the spinner to render the animation at the end of
Expand Down Expand Up @@ -252,6 +264,14 @@ func New(cfg Config) (*Spinner, error) {
return nil, errors.New("cfg.Frequency must be greater than 0")
}

if cfg.ShowCursor && cfg.HideCursor {
return nil, errors.New("cfg.ShowCursor and cfg.HideCursor cannot be true")
}

if cfg.HideCursor {
cfg.ShowCursor = false
}

if !isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd()) {
cfg.NotTTY = true
}
Expand All @@ -268,7 +288,7 @@ func New(cfg Config) (*Spinner, error) {
dataUpdateCh: make(chan struct{}),

colorAll: cfg.ColorAll,
cursorHidden: cfg.HideCursor,
cursorHidden: !cfg.ShowCursor,
spinnerAtEnd: cfg.SpinnerAtEnd,
suffixAutoColon: cfg.SuffixAutoColon,
isDumbTerm: os.Getenv("TERM") == "dumb",
Expand Down
37 changes: 35 additions & 2 deletions spinner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,17 @@ func TestNew(t *testing.T) {
err: "failed to build stop fail color function: invalid is not a valid color",
},
{
name: "full_config",
name: "config_with_conflicting_cursor_settings",
writer: os.Stdout,
cfg: Config{
Frequency: 100 * time.Millisecond,
ShowCursor: true,
HideCursor: true,
},
err: "cfg.ShowCursor and cfg.HideCursor cannot be true",
},
{
name: "full_config_with_deprecated_hidden_cursor",
writer: os.Stderr,
maxWidth: 3,
cfg: Config{
Expand All @@ -118,6 +128,29 @@ func TestNew(t *testing.T) {
SpinnerAtEnd: true,
},
},
{
name: "full_config",
writer: os.Stderr,
maxWidth: 3,
cfg: Config{
Frequency: 100 * time.Millisecond,
Writer: os.Stderr,
ShowCursor: true,
ColorAll: true,
Colors: []string{"fgYellow"},
CharSet: CharSets[59],
Prefix: "test prefix: ",
Suffix: " test suffix",
Message: "test message",
StopMessage: "test stop message",
StopCharacter: "✓",
StopColors: []string{"fgGreen"},
StopFailMessage: "test stop fail message",
StopFailCharacter: "✗",
StopFailColors: []string{"fgHiRed"},
SpinnerAtEnd: true,
},
},
}

for _, tt := range tests {
Expand All @@ -140,7 +173,7 @@ func TestNew(t *testing.T) {
t.Fatalf("spinner.colorAll = %t, want %t", spinner.colorAll, tt.cfg.ColorAll)
}

if spinner.cursorHidden != tt.cfg.HideCursor {
if spinner.cursorHidden != !tt.cfg.ShowCursor {
t.Fatalf("spinner.cursorHiddenn = %t, want %t", spinner.cursorHidden, tt.cfg.HideCursor)
}

Expand Down

0 comments on commit b6ca861

Please sign in to comment.