Skip to content

Commit

Permalink
chore: make CatchPanics an option flag
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed Oct 7, 2022
1 parent 0ac6702 commit b52ab75
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
2 changes: 1 addition & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func WithoutSignalHandler() ProgramOption {
// cleanup on exit.
func WithoutCatchPanics() ProgramOption {
return func(p *Program) {
p.CatchPanics = false
p.startupOptions |= withoutCatchPanics
}
}

Expand Down
11 changes: 4 additions & 7 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ func TestOptions(t *testing.T) {
}
})

t.Run("catch panics", func(t *testing.T) {
p := NewProgram(nil, WithoutCatchPanics())
if p.CatchPanics {
t.Errorf("catch panics should not have been set")
}
})

t.Run("renderer", func(t *testing.T) {
p := NewProgram(nil, WithoutRenderer())
switch p.renderer.(type) {
Expand Down Expand Up @@ -62,6 +55,10 @@ func TestOptions(t *testing.T) {
exercise(t, WithANSICompressor(), withANSICompressor)
})

t.Run("without catch panics", func(t *testing.T) {
exercise(t, WithoutCatchPanics(), withoutCatchPanics)
})

t.Run("without signal handler", func(t *testing.T) {
exercise(t, WithoutSignalHandler(), withoutSignalHandler)
})
Expand Down
33 changes: 17 additions & 16 deletions tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ const (
withCustomInput
withANSICompressor
withoutSignalHandler

// Catching panics is incredibly useful for restoring the terminal to a
// usable state after a panic occurs. When this is set, Bubble Tea will
// recover from panics, print the stack trace, and disable raw mode. This
// feature is on by default.
withoutCatchPanics
)

// Program is a terminal user interface.
Expand All @@ -88,26 +94,21 @@ type Program struct {
errs chan error
readLoopDone chan struct{}

output *termenv.Output // where to send output. this will usually be os.Stdout.
// where to send output, this will usually be os.Stdout.
output *termenv.Output
restoreOutput func() error
input io.Reader // this will usually be os.Stdin.
cancelReader cancelreader.CancelReader

renderer renderer
altScreenWasActive bool // was the altscreen active before releasing the terminal?
renderer renderer

// CatchPanics is incredibly useful for restoring the terminal to a usable
// state after a panic occurs. When this is set, Bubble Tea will recover
// from panics, print the stack trace, and disable raw mode. This feature
// is on by default.
CatchPanics bool
// where to read inputs from, this will usually be os.Stdin.
input io.Reader
cancelReader cancelreader.CancelReader
console console.Console

ignoreSignals bool
altScreenWasActive bool // was the altscreen active before releasing the terminal?
ignoreSignals bool

killc chan bool

console console.Console

// Stores the original reference to stdin for cases where input is not a
// TTY on windows and we've automatically opened CONIN$ to receive input.
// When the program exits this will be restored.
Expand All @@ -133,7 +134,6 @@ func NewProgram(model Model, opts ...ProgramOption) *Program {
initialModel: model,
input: os.Stdin,
msgs: make(chan Msg),
CatchPanics: true,
killc: make(chan bool, 1),
}

Expand Down Expand Up @@ -383,7 +383,8 @@ func (p *Program) StartReturningModel() (Model, error) {
close(sigintLoopDone)
}

if p.CatchPanics {
// Recover from panics.
if !p.startupOptions.has(withoutCatchPanics) {
defer func() {
if r := recover(); r != nil {
p.shutdown(true)
Expand Down

0 comments on commit b52ab75

Please sign in to comment.