Skip to content

Commit 214a788

Browse files
committed
Add ability for spinner to hide cursor
This can be a bit more asthetically pleasing, so let's add an option for hiding the cursor. To keep the implementation simple, this option cannot be toggled on the spinner and must be provided at creation time.
1 parent e6b45b6 commit 214a788

1 file changed

Lines changed: 47 additions & 3 deletions

File tree

‎spinner.go‎

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,20 @@ func atomicCharacter(v *atomic.Value) character {
9393
return c
9494
}
9595

96+
func atomicBool(v *atomic.Value) bool {
97+
av := v.Load()
98+
if av == nil {
99+
return false
100+
}
101+
102+
b, ok := av.(bool)
103+
if !ok {
104+
return false
105+
}
106+
107+
return b
108+
}
109+
96110
type character struct {
97111
value string
98112
size int
@@ -133,6 +147,11 @@ type Config struct {
133147
// changed on the fly. If omitted, this defaults to os.Stdout.
134148
Writer io.Writer
135149

150+
// HideCursor describes whether the cursor should be hidden by the spinner.
151+
// If it is hidden, it will be restored when the spinner stops. This can't
152+
// be changed on the fly.
153+
HideCursor bool
154+
136155
// ColorAll describes whether to color everything (all) or just the spinner
137156
// character(s). This cannot be changed.
138157
ColorAll bool
@@ -187,8 +206,9 @@ type Config struct {
187206
//
188207
// Note: You need to use New() to construct a *Spinner.
189208
type Spinner struct {
190-
writer io.Writer
191-
colorAll bool
209+
writer io.Writer
210+
colorAll bool
211+
cursorHidden bool
192212

193213
mu *sync.RWMutex
194214
chars []character
@@ -225,7 +245,9 @@ func New(cfg Config) (*Spinner, error) {
225245
mu: &sync.RWMutex{},
226246
delayDuration: int64Ptr(int64(cfg.Delay)),
227247
active: uint32Ptr(0),
228-
colorAll: cfg.ColorAll,
248+
249+
colorAll: cfg.ColorAll,
250+
cursorHidden: cfg.HideCursor,
229251

230252
colorFn: &atomic.Value{},
231253
prefix: &atomic.Value{},
@@ -397,6 +419,12 @@ func (s *Spinner) painter(cancel, sig chan struct{}) {
397419
panic(fmt.Sprintf("failed to erase line: %v", err))
398420
}
399421

422+
if s.cursorHidden {
423+
if err := s.unhideCursor(); err != nil {
424+
panic(fmt.Sprintf("failed to unhide cursor: %v", err))
425+
}
426+
}
427+
400428
var m string
401429
var c character
402430
var cFn func(format string, a ...interface{}) string
@@ -438,6 +466,12 @@ func (s *Spinner) painter(cancel, sig chan struct{}) {
438466
panic(fmt.Sprintf("failed to erase line: %v", err))
439467
}
440468

469+
if s.cursorHidden {
470+
if err := s.hideCursor(); err != nil {
471+
panic(fmt.Sprintf("failed to hide cursor: %v", err))
472+
}
473+
}
474+
441475
if err := s.paint(c, atomicString(s.message), atomicColorFn(s.colorFn)); err != nil {
442476
panic(fmt.Sprintf("failed to paint line: %v", err))
443477
}
@@ -454,6 +488,16 @@ func (s *Spinner) erase() error {
454488
return err
455489
}
456490

491+
func (s *Spinner) hideCursor() error {
492+
_, err := fmt.Fprint(s.writer, "\r\033[?25l\r")
493+
return err
494+
}
495+
496+
func (s *Spinner) unhideCursor() error {
497+
_, err := fmt.Fprint(s.writer, "\r\033[?25h\r")
498+
return err
499+
}
500+
457501
// padChar pads the spinner character so suffix / message offset from left is
458502
// consistent
459503
func padChar(char character, maxWidth int) string {

0 commit comments

Comments
 (0)
⌂ 换网址