feat: make signal handler optional

You can now initialize a tea app without a signal handler:

p := NewProgram(model, WithoutSignalHandler())
This commit is contained in:
Christian Muehlhaeuser 2022-10-07 20:24:23 +02:00
parent 2696b2f339
commit 0ac6702e11
3 changed files with 19 additions and 1 deletions

View File

@ -38,6 +38,14 @@ func WithInputTTY() ProgramOption {
} }
} }
// WithoutSignalHandler disables the signal handler that Bubble Tea sets up for
// Programs. This is useful if you want to handle signals yourself.
func WithoutSignalHandler() ProgramOption {
return func(p *Program) {
p.startupOptions |= withoutSignalHandler
}
}
// WithoutCatchPanics disables the panic catching that Bubble Tea does by // WithoutCatchPanics disables the panic catching that Bubble Tea does by
// default. If panic catching is disabled the terminal will be in a fairly // default. If panic catching is disabled the terminal will be in a fairly
// unusable state after a panic because Bubble Tea will not perform its usual // unusable state after a panic because Bubble Tea will not perform its usual

View File

@ -62,6 +62,10 @@ func TestOptions(t *testing.T) {
exercise(t, WithANSICompressor(), withANSICompressor) exercise(t, WithANSICompressor(), withANSICompressor)
}) })
t.Run("without signal handler", func(t *testing.T) {
exercise(t, WithoutSignalHandler(), withoutSignalHandler)
})
t.Run("mouse cell motion", func(t *testing.T) { t.Run("mouse cell motion", func(t *testing.T) {
p := NewProgram(nil, WithMouseAllMotion(), WithMouseCellMotion()) p := NewProgram(nil, WithMouseAllMotion(), WithMouseCellMotion())
if !p.startupOptions.has(withMouseCellMotion) { if !p.startupOptions.has(withMouseCellMotion) {

8
tea.go
View File

@ -71,6 +71,7 @@ const (
withInputTTY withInputTTY
withCustomInput withCustomInput
withANSICompressor withANSICompressor
withoutSignalHandler
) )
// Program is a terminal user interface. // Program is a terminal user interface.
@ -375,7 +376,12 @@ func (p *Program) StartReturningModel() (Model, error) {
} }
// Handle signals. // Handle signals.
sigintLoopDone := p.handleSignals() sigintLoopDone := make(chan struct{})
if !p.startupOptions.has(withoutSignalHandler) {
sigintLoopDone = p.handleSignals()
} else {
close(sigintLoopDone)
}
if p.CatchPanics { if p.CatchPanics {
defer func() { defer func() {