Skip to content
Myr

feat(SIGINT): better handle interrupts (#1255)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* feat(SIGINT): better handle interrupts This PR introduces `Interrupt()`, `InterruptMsg{}`, and `ErrInterrupted`. Users can still handle "ctrl+c" as before, but instead of return `tea.Quit`, they can return `tea.Interrupt`. Later on, they can `errors.If(err, tea.ErrInterrupted)` and `os.Exit(130)` if they want to. * chore: fix unrelated typo it was bothering me

authored by Carlos Alexandro Becker · committed by GitHub · parent 2ea49dd · Browse files · 3 files+47 added−12 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+47 added−12 removed

  • other:+47 added−12 removed

3 files+47 added−12 removed

modified:examples/suspend/main.go+9 added-2 removed
1package main23import (4 "errors"5 "fmt"6 "os"78 tea "github.com/charmbracelet/bubbletea"
14 unmodified lines
23 m.suspending = false24 return m, nil25 case tea.KeyMsg:26 switch msg.String() {26 case "q", "ctrl+c", "esc":27 case "q", "esc":28 m.quitting = true29 return m, tea.Quit30 case "ctrl+c":31 m.quitting = true32 return m, tea.Interrupt33 case "ctrl+z":34 m.suspending = true35 return m, tea.Suspend36 }
5 unmodified lines
42 if m.suspending || m.quitting {43 return ""44 }4542 return "\nPress ctrl-z to suspend, or ctrl+c to exit\n"46 return "\nPress ctrl-z to suspend, ctrl+c to interrupt, q, or esc to exit\n"47}4849func main() {50 if _, err := tea.NewProgram(model{}).Run(); err != nil {51 fmt.Println("Error running program:", err)52 if errors.Is(err, tea.ErrInterrupted) {53 os.Exit(130)54 }55 os.Exit(1)56 }57}
modified:options.go+1 added-1 removed
184 unmodified lines
185//186// This feature is provisional, and may be changed or removed in a future version187// of this package.188//189// Deprecated: this incurs a noticable performance hit. A future release will189// Deprecated: this incurs a noticeable performance hit. A future release will190// optimize ANSI automatically without the performance penalty.191func WithANSICompressor() ProgramOption {192 return func(p *Program) {193 p.startupOptions |= withANSICompressor
59 unmodified lines
modified:tea.go+37 added-9 removed
24 unmodified lines
25 "github.com/muesli/cancelreader"26 "golang.org/x/sync/errgroup"27)2829// ErrProgramKilled is returned by [Program.Run] when the program got killed.29// ErrProgramKilled is returned by [Program.Run] when the program gets killed.30var ErrProgramKilled = errors.New("program was killed")3132// ErrInterrupted is returned by [Program.Run] when the program get a SIGINT33// signal, or when it receives a [InterruptMsg].34var ErrInterrupted = errors.New("program was interrupted")3536// Msg contain data from the result of a IO operation. Msgs trigger the update37// function and, henceforth, the UI.38type Msg interface{}39
149 unmodified lines
189func Quit() Msg {190 return QuitMsg{}191}192189// QuitMsg signals that the program should quit. You can send a QuitMsg with190// Quit.193// QuitMsg signals that the program should quit. You can send a [QuitMsg] with194// [Quit].195type QuitMsg struct{}196197// Suspend is a special command that tells the Bubble Tea program to suspend.198func Suspend() Msg {
3 unmodified lines
202// SuspendMsg signals the program should suspend.203// This usually happens when ctrl+z is pressed on common programs, but since204// bubbletea puts the terminal in raw mode, we need to handle it in a205// per-program basis.206//202// You can send this message with Suspend.207// You can send this message with [Suspend()].208type SuspendMsg struct{}209210// ResumeMsg can be listen to to do something once a program is resumed back211// from a suspend state.212type ResumeMsg struct{}213214// InterruptMsg signals the program should suspend.215// This usually happens when ctrl+c is pressed on common programs, but since216// bubbletea puts the terminal in raw mode, we need to handle it in a217// per-program basis.218//219// You can send this message with [Interrupt()].220type InterruptMsg struct{}221222// Interrupt is a special command that tells the Bubble Tea program to223// interrupt.224func Interrupt() Msg {225 return InterruptMsg{}226}227228// NewProgram creates a new Program.229func NewProgram(model Model, opts ...ProgramOption) *Program {230 p := &Program{231 initialModel: model,
49 unmodified lines
281 select {282 case <-p.ctx.Done():283 return284266 case <-sig:285 case s := <-sig:286 if atomic.LoadUint32(&p.ignoreSignals) == 0 {287 switch s {288 case syscall.SIGINT:289 p.msgs <- InterruptMsg{}290 default:268 p.msgs <- QuitMsg{}291 p.msgs <- QuitMsg{}292 }293 return294 }295 }296 }
88 unmodified lines
385 switch msg := msg.(type) {386 case QuitMsg:387 return model, nil388389 case InterruptMsg:390 return model, ErrInterrupted391392 case SuspendMsg:393 if suspendSupported {394 p.suspend()395 }
223 unmodified lines
619 p.handlers.add(p.handleCommands(cmds))620621 // Run event loop, handle updates and draw.622 model, err := p.eventLoop(model, cmds)596 killed := p.ctx.Err() != nil597 if killed {623 killed := p.ctx.Err() != nil || err != nil624 if killed && err == nil {625 err = fmt.Errorf("%w: %s", ErrProgramKilled, p.ctx.Err())599 } else {626 }627 if err == nil {628 // Ensure we rendered the final state of the model.629 p.renderer.write(model.View())630 }631
185 unmodified lines