forked from Mirrors/bubbletea
Gracefully recover from panics (but still print stack traces) by default
This commit is contained in:
parent
4609b8afd9
commit
fb48e03639
19
tea.go
19
tea.go
|
@ -12,6 +12,7 @@ package tea
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"runtime/debug"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
te "github.com/muesli/termenv"
|
te "github.com/muesli/termenv"
|
||||||
|
@ -66,6 +67,12 @@ type Program struct {
|
||||||
output *os.File // where to send output. this will usually be os.Stdout.
|
output *os.File // where to send output. this will usually be os.Stdout.
|
||||||
renderer *renderer
|
renderer *renderer
|
||||||
altScreenActive bool
|
altScreenActive bool
|
||||||
|
|
||||||
|
// CatchPanics is incredibly useful for restoring the terminal to a useable
|
||||||
|
// 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
|
||||||
}
|
}
|
||||||
|
|
||||||
// Quit is a special command that tells the Bubble Tea program to exit.
|
// Quit is a special command that tells the Bubble Tea program to exit.
|
||||||
|
@ -96,6 +103,7 @@ func NewProgram(init Init, update Update, view View) *Program {
|
||||||
view: view,
|
view: view,
|
||||||
|
|
||||||
output: os.Stdout,
|
output: os.Stdout,
|
||||||
|
CatchPanics: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,6 +116,17 @@ func (p *Program) Start() error {
|
||||||
done = make(chan struct{})
|
done = make(chan struct{})
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if p.CatchPanics {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
p.ExitAltScreen()
|
||||||
|
fmt.Print("Caught panic. Restoring terminal...\n\n")
|
||||||
|
debug.PrintStack()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
p.renderer = newRenderer(p.output, &p.mtx)
|
p.renderer = newRenderer(p.output, &p.mtx)
|
||||||
|
|
||||||
err := initTerminal()
|
err := initTerminal()
|
||||||
|
|
Loading…
Reference in New Issue