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
Provenance
- Commit
cb93959- Tree
7437345- Author
Carlos Alexandro Becker <caarlos0@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
2ea49dd
3 files changed+47 added−12 removed
modified:examples/suspend/main.go+9 added-2 removed
1 package main2 3 import (4+ "errors"5 "fmt"6 "os"7 8 tea "github.com/charmbracelet/bubbletea"14 unmodified lines23 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 lines42 if m.suspending || m.quitting {43 return ""44 }45 42- 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 }48 49 func 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 lines185 //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.191 func WithANSICompressor() ProgramOption {192 return func(p *Program) {193 p.startupOptions |= withANSICompressor59 unmodified linesmodified:tea.go+37 added-9 removed
24 unmodified lines25 "github.com/muesli/cancelreader"26 "golang.org/x/sync/errgroup"27 )28 29-// ErrProgramKilled is returned by [Program.Run] when the program got killed.29+// ErrProgramKilled is returned by [Program.Run] when the program gets killed.30 var ErrProgramKilled = errors.New("program was killed")31 32+// ErrInterrupted is returned by [Program.Run] when the program get a SIGINT33+// signal, or when it receives a [InterruptMsg].34+var ErrInterrupted = errors.New("program was interrupted")35+36 // Msg contain data from the result of a IO operation. Msgs trigger the update37 // function and, henceforth, the UI.38 type Msg interface{}39 149 unmodified lines189 func Quit() Msg {190 return QuitMsg{}191 }192 189-// 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].195 type QuitMsg struct{}196 197 // Suspend is a special command that tells the Bubble Tea program to suspend.198 func Suspend() Msg {3 unmodified lines202 // 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()].208 type SuspendMsg struct{}209 210 // ResumeMsg can be listen to to do something once a program is resumed back211 // from a suspend state.212 type ResumeMsg struct{}213 214+// 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()].220+type InterruptMsg struct{}221+222+// Interrupt is a special command that tells the Bubble Tea program to223+// interrupt.224+func Interrupt() Msg {225+ return InterruptMsg{}226+}227+228 // NewProgram creates a new Program.229 func NewProgram(model Model, opts ...ProgramOption) *Program {230 p := &Program{231 initialModel: model,49 unmodified lines281 select {282 case <-p.ctx.Done():283 return284 266- 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 lines385 switch msg := msg.(type) {386 case QuitMsg:387 return model, nil388 389+ case InterruptMsg:390+ return model, ErrInterrupted391+392 case SuspendMsg:393 if suspendSupported {394 p.suspend()395 }223 unmodified lines619 p.handlers.add(p.handleCommands(cmds))620 621 // 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