fix: restore the terminal on kill
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Ayman Bagabas · committed by Ayman Bagabas · parent 0589921 · Browse files · 1 file·+29 added−22 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Supersedes: https://github.com/charmbracelet/bubbletea/pull/1133
- Fixes: https://github.com/charmbracelet/bubbletea/issues/1127
Diff stats
1 file+29 added−22 removed
- other:+29 added−22 removed
Provenance
- Commit
6e71f52- Tree
8c97d75- Author
Ayman Bagabas <ayman.bagabas@gmail.com>- Committer
Ayman Bagabas <ayman.bagabas@gmail.com>- Parent
0589921
1 file changed+29 added−22 removed
modified:tea.go+29 added-22 removed
126 unmodified lines127 // Program is a terminal user interface.128 type Program struct {129 initialModel Model130 131+ // handlers is a list of channels that need to be waited on before the132+ // program can exit.133+ handlers channelHandlers134+135 // Configuration options that will set as the program is initializing,136 // treated as bits. These options can be set via various ProgramOptions.137 startupOptions startupOptions138 329 unmodified lines468 // Run initializes the program and runs its event loops, blocking until it gets469 // terminated by either [Program.Quit], [Program.Kill], or its signal handler.470 // Returns the final model.471 func (p *Program) Run() (Model, error) {468- handlers := channelHandlers{}472+ p.handlers = channelHandlers{}473 cmds := make(chan Cmd)474 p.errs = make(chan error)475 p.finished = make(chan struct{}, 1)476 38 unmodified lines515 }516 517 // Handle signals.518 if !p.startupOptions.has(withoutSignalHandler) {515- handlers.add(p.handleSignals())519+ p.handlers.add(p.handleSignals())520 }521 522 // Recover from panics.523 if !p.startupOptions.has(withoutCatchPanics) {38 unmodified lines562 // Initialize the program.563 model := p.initialModel564 if initCmd := model.Init(); initCmd != nil {565 ch := make(chan struct{})562- handlers.add(ch)566+ p.handlers.add(ch)567 568 go func() {569 defer close(ch)570 14 unmodified lines585 }586 }587 588 // Handle resize events.585- handlers.add(p.handleResize())589+ p.handlers.add(p.handleResize())590 591 // Process commands.588- handlers.add(p.handleCommands(cmds))592+ p.handlers.add(p.handleCommands(cmds))593 594 // Run event loop, handle updates and draw.595 model, err := p.eventLoop(model, cmds)596 killed := p.ctx.Err() != nil3 unmodified lines600 // Ensure we rendered the final state of the model.601 p.renderer.write(model.View())602 }603 600- // Tear down.601- p.cancel()602-603- // Check if the cancel reader has been setup before waiting and closing.604- if p.cancelReader != nil {605- // Wait for input loop to finish.606- if p.cancelReader.Cancel() {607- p.waitForReadLoop()608- }609- _ = p.cancelReader.Close()610- }611-612- // Wait for all handlers to finish.613- handlers.shutdown()614-604 // Restore terminal state.605 p.shutdown(killed)606 607 return model, err46 unmodified lines654 // Kill stops the program immediately and restores the former terminal state.655 // The final render that you would normally see when quitting will be skipped.656 // [program.Run] returns a [ErrProgramKilled] error.657 func (p *Program) Kill() {669- p.cancel()658+ p.shutdown(true)659 }660 661 // Wait waits/blocks until the underlying Program finished shutting down.662 func (p *Program) Wait() {2 unmodified lines665 666 // shutdown performs operations to free up resources and restore the terminal667 // to its original state.668 func (p *Program) shutdown(kill bool) {669+ p.cancel()670+671+ // Wait for all handlers to finish.672+ p.handlers.shutdown()673+674+ // Check if the cancel reader has been setup before waiting and closing.675+ if p.cancelReader != nil {676+ // Wait for input loop to finish.677+ if p.cancelReader.Cancel() {678+ if !kill {679+ p.waitForReadLoop()680+ }681+ }682+ _ = p.cancelReader.Close()683+ }684+685 if p.renderer != nil {686 if kill {687 p.renderer.kill()688 } else {1 unmodified line690 }691 }692 693 _ = p.restoreTerminalState()694+ if !kill {689- p.finished <- struct{}{}695+ p.finished <- struct{}{}696+ }697 }698 699 // recoverFromPanic recovers from a panic, prints the stack trace, and restores700 // the terminal to a usable state.88 unmodified lines