Skip to content
Myr

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

1 file+29 added−22 removed

modified:tea.go+29 added-22 removed
126 unmodified lines
127// Program is a terminal user interface.128type Program struct {129 initialModel Model130131 // handlers is a list of channels that need to be waited on before the132 // program can exit.133 handlers channelHandlers134135 // 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 lines
468// 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.471func (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 lines
515 }516517 // Handle signals.518 if !p.startupOptions.has(withoutSignalHandler) {515 handlers.add(p.handleSignals())519 p.handlers.add(p.handleSignals())520 }521522 // Recover from panics.523 if !p.startupOptions.has(withoutCatchPanics) {
38 unmodified lines
562 // 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)567568 go func() {569 defer close(ch)570
14 unmodified lines
585 }586 }587588 // Handle resize events.585 handlers.add(p.handleResize())589 p.handlers.add(p.handleResize())590591 // Process commands.588 handlers.add(p.handleCommands(cmds))592 p.handlers.add(p.handleCommands(cmds))593594 // Run event loop, handle updates and draw.595 model, err := p.eventLoop(model, cmds)596 killed := p.ctx.Err() != nil
3 unmodified lines
600 // Ensure we rendered the final state of the model.601 p.renderer.write(model.View())602 }603600 // Tear down.601 p.cancel()602603 // 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 }611612 // Wait for all handlers to finish.613 handlers.shutdown()614604 // Restore terminal state.605 p.shutdown(killed)606607 return model, err
46 unmodified lines
654// 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.657func (p *Program) Kill() {669 p.cancel()658 p.shutdown(true)659}660661// Wait waits/blocks until the underlying Program finished shutting down.662func (p *Program) Wait() {
2 unmodified lines
665666// shutdown performs operations to free up resources and restore the terminal667// to its original state.668func (p *Program) shutdown(kill bool) {669 p.cancel()670671 // Wait for all handlers to finish.672 p.handlers.shutdown()673674 // 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 }684685 if p.renderer != nil {686 if kill {687 p.renderer.kill()688 } else {
1 unmodified line
690 }691 }692693 _ = p.restoreTerminalState()694 if !kill {689 p.finished <- struct{}{}695 p.finished <- struct{}{}696 }697}698699// recoverFromPanic recovers from a panic, prints the stack trace, and restores700// the terminal to a usable state.
88 unmodified lines