@@ -170,6 +170,8 @@ type AskerConsole struct {
170
170
// holds the last 2 bytes written by message or messageUX. This is used to detect when there is already an empty
171
171
// line (\n\n)
172
172
last2Byte [2 ]byte
173
+
174
+ spinnerStopChan chan struct {}
173
175
}
174
176
175
177
type ConsoleOptions struct {
@@ -406,7 +408,20 @@ func (c *AskerConsole) ShowSpinner(ctx context.Context, title string, format Spi
406
408
// While it is indeed safe to call Start regardless of whether the spinner is running,
407
409
// calling Start may result in an additional line of output being written in non-tty scenarios
408
410
_ = c .spinner .Start ()
411
+
412
+ go func () {
413
+ c .spinnerStopChan = make (chan struct {}, 1 )
414
+
415
+ select {
416
+ case <- ctx .Done ():
417
+ case <- c .spinnerStopChan :
418
+ }
419
+
420
+ _ = c .spinner .Stop ()
421
+ return
422
+ }()
409
423
}
424
+
410
425
c .spinnerLineMu .Unlock ()
411
426
}
412
427
@@ -468,7 +483,7 @@ func (c *AskerConsole) StopSpinner(ctx context.Context, lastMessage string, form
468
483
lastMessage = c .getStopChar (format ) + " " + lastMessage
469
484
}
470
485
471
- _ = c . spinner . Stop ()
486
+ c . spinnerStopChan <- struct {}{}
472
487
if lastMessage != "" {
473
488
// Avoid using StopMessage() as it may result in an extra Message line print in non-tty scenarios
474
489
fmt .Fprintln (c .writer , lastMessage )
@@ -938,19 +953,6 @@ func watchTerminalResize(c *AskerConsole) {
938
953
}
939
954
}
940
955
941
- func watchTerminalInterrupt (c * AskerConsole ) {
942
- signalChan := make (chan os.Signal , 1 )
943
- signal .Notify (signalChan , os .Interrupt )
944
- go func () {
945
- <- signalChan
946
-
947
- // unhide the cursor if applicable
948
- _ = c .spinner .Stop ()
949
-
950
- os .Exit (1 )
951
- }()
952
- }
953
-
954
956
// Writers that back the underlying console.
955
957
type Writers struct {
956
958
// The writer to write output to.
@@ -1015,7 +1017,6 @@ func NewConsole(
1015
1017
if isTerminal {
1016
1018
c .consoleWidth = atomic .NewInt32 (consoleWidth ())
1017
1019
watchTerminalResize (c )
1018
- watchTerminalInterrupt (c )
1019
1020
}
1020
1021
1021
1022
return c
0 commit comments