🐛 Program should still halt even with spinner
This commit is contained in:
parent
f7fca3ba73
commit
fd68ffbbf8
3 changed files with 31 additions and 2 deletions
21
cli/context/signal.go
Normal file
21
cli/context/signal.go
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package context
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func SelfCancelingCotext(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
c, done := context.WithCancel(ctx)
|
||||
ch := make(chan os.Signal, 1)
|
||||
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
|
||||
go func() {
|
||||
for range ch {
|
||||
done()
|
||||
}
|
||||
}()
|
||||
|
||||
return c, done
|
||||
}
|
||||
|
|
@ -94,7 +94,12 @@ func (s *spin) Err() error {
|
|||
}
|
||||
|
||||
func Spin(ctx context.Context, message string) Spinner {
|
||||
p := tea.NewProgram(newModel(message), tea.WithContext(ctx))
|
||||
p := tea.NewProgram(
|
||||
newModel(message),
|
||||
tea.WithContext(ctx),
|
||||
tea.WithoutSignalHandler(),
|
||||
tea.WithInput(nil),
|
||||
)
|
||||
s := &spin{p: p, finished: make(chan struct{}, 1)}
|
||||
go func() {
|
||||
_, err := s.p.Run()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue