Skip to content

Commit 9724787

Browse files
authored
Merge pull request #3 from liorj-orca/disable_exit_code
feat(general): the ability not to exit on interrupt
2 parents 577c49e + a2a0d2d commit 9724787

File tree

1 file changed

+40
-30
lines changed

1 file changed

+40
-30
lines changed

spinner.go

+40-30
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,22 @@ func getStatusSymbols() (successSymbol, errorSymbol string) {
4646

4747
// Spinner defines our spinner data.
4848
type Spinner struct {
49-
message *synx.String // message to display
50-
stopChan chan struct{} // exit channel
51-
speedUpdated *synx.Bool // Indicates speed has been updated
52-
exitStatus status // Status of exit
53-
successSymbol *synx.String // Symbol printed when Success() called
54-
errorSymbol *synx.String // Symbol printed when Error() called
55-
spinFrames *synx.StringSlice // Spinset frames
56-
frameNumber int // Current frame [default 0]
57-
termWidth *synx.Int // Terminal width
58-
termHeight *synx.Int // Terminal Height
59-
spinSpeed *synx.Int // Delay between spinner updates in milliseconds [default 100ms]
60-
currentLine *synx.String // The current line being displayed
61-
running *synx.Bool // Indicates if the spinner is running
62-
abortMessage *synx.String // Printed when handling ctrl-c interrupt
63-
isTerminal *synx.Bool // Flag indicating if we are outputting to terminal
49+
message *synx.String // message to display
50+
stopChan chan struct{} // exit channel
51+
speedUpdated *synx.Bool // Indicates speed has been updated
52+
exitStatus status // Status of exit
53+
successSymbol *synx.String // Symbol printed when Success() called
54+
errorSymbol *synx.String // Symbol printed when Error() called
55+
spinFrames *synx.StringSlice // Spinset frames
56+
frameNumber int // Current frame [default 0]
57+
termWidth *synx.Int // Terminal width
58+
termHeight *synx.Int // Terminal Height
59+
spinSpeed *synx.Int // Delay between spinner updates in milliseconds [default 100ms]
60+
currentLine *synx.String // The current line being displayed
61+
running *synx.Bool // Indicates if the spinner is running
62+
abortMessage *synx.String // Printed when handling ctrl-c interrupt
63+
isTerminal *synx.Bool // Flag indicating if we are outputting to terminal
64+
exitOnInterrupt *synx.Bool // Indicates the spinner will os.Exit on interrupt
6465
}
6566

6667
// NewSpinner creates a new spinner and sets up the default values.
@@ -72,19 +73,20 @@ func NewSpinner(optionalMessage ...string) *Spinner {
7273
message = optionalMessage[0]
7374
}
7475
result := &Spinner{
75-
message: synx.NewString(message),
76-
stopChan: make(chan struct{}),
77-
speedUpdated: synx.NewBool(true),
78-
successSymbol: synx.NewString(successSymbol),
79-
errorSymbol: synx.NewString(errorSymbol),
80-
spinFrames: synx.NewStringSlice(getDefaultSpinnerFrames()),
81-
spinSpeed: synx.NewInt(100),
82-
termWidth: synx.NewInt(1),
83-
termHeight: synx.NewInt(1),
84-
abortMessage: synx.NewString("Aborted."),
85-
frameNumber: 0,
86-
running: synx.NewBool(false),
87-
isTerminal: synx.NewBool(isatty.IsTerminal(os.Stdout.Fd())),
76+
message: synx.NewString(message),
77+
stopChan: make(chan struct{}),
78+
speedUpdated: synx.NewBool(true),
79+
successSymbol: synx.NewString(successSymbol),
80+
errorSymbol: synx.NewString(errorSymbol),
81+
spinFrames: synx.NewStringSlice(getDefaultSpinnerFrames()),
82+
spinSpeed: synx.NewInt(100),
83+
termWidth: synx.NewInt(1),
84+
termHeight: synx.NewInt(1),
85+
abortMessage: synx.NewString("Aborted."),
86+
frameNumber: 0,
87+
running: synx.NewBool(false),
88+
isTerminal: synx.NewBool(isatty.IsTerminal(os.Stdout.Fd())),
89+
exitOnInterrupt: synx.NewBool(true),
8890
}
8991

9092
return result
@@ -187,14 +189,20 @@ func (s *Spinner) getRunning() bool {
187189
func (s *Spinner) setRunning(value bool) {
188190
s.running.SetValue(value)
189191
}
192+
func (s *Spinner) getExitOnInterrupt() bool {
193+
return s.exitOnInterrupt.GetValue()
194+
}
195+
196+
func (s *Spinner) SetExitOnInterrupt(value bool) {
197+
s.exitOnInterrupt.SetValue(value)
198+
}
190199

191200
func (s *Spinner) printSuccess(message string, args ...interface{}) {
192201
color.HiGreen(message, args...)
193202
}
194203

195204
// Start the spinner!
196205
func (s *Spinner) Start(optionalMessage ...string) {
197-
198206
// If we're trying to start an already running spinner,
199207
// add a slight delay and retry. This allows the spinner
200208
// to complete a previous stop command gracefully.
@@ -232,7 +240,9 @@ func (s *Spinner) Start(optionalMessage ...string) {
232240
s.stopChan <- struct{}{}
233241
fmt.Println("")
234242
color.HiRed("\r%s %s", s.getErrorSymbol(), s.getAbortMessage())
235-
os.Exit(1)
243+
if s.getExitOnInterrupt() {
244+
os.Exit(1)
245+
}
236246
}(s.stopChan)
237247

238248
// spawn off a goroutine to handle the animation.

0 commit comments

Comments
 (0)