feat: allow to suspend bubbletea programs (#1054)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* feat: allow to suspend bubbletea programs closes #497 closes #1053 * fix: typo * fix: move things around, add example Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * fix: block until CONT * fix: ResumeMsg * docs: improve example * docs: one more example Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * fix: examples tests Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> ---------
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
Diff stats
10 files+131 added−12 removed
- other:+131 added−12 removed
Provenance
- Commit
ea13ffb- Tree
5d52f03- Author
Carlos Alexandro Becker <caarlos0@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
0208ac5
10 files changed+131 added−12 removed
modified:examples/altscreen-toggle/main.go+14 added-3 removed
12 unmodified lines13 helpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("241"))14 )15 16 type model struct {17- altscreen bool18- quitting bool17+ altscreen bool18+ quitting bool19+ suspending bool20 }21 22 func (m model) Init() tea.Cmd {23 return nil24 }25 26 func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {27 switch msg := msg.(type) {28+ case tea.ResumeMsg:29+ m.suspending = false30+ return m, nil31 case tea.KeyMsg:32 switch msg.String() {33 case "q", "ctrl+c", "esc":34 m.quitting = true35 return m, tea.Quit36+ case "ctrl+z":37+ m.suspending = true38+ return m, tea.Suspend39 case " ":40 var cmd tea.Cmd41 if m.altscreen {42 cmd = tea.ExitAltScreen7 unmodified lines50 return m, nil51 }52 53 func (m model) View() string {54+ if m.suspending {55+ return ""56+ }57+58 if m.quitting {59 return "Bye!\n"60 }61 9 unmodified lines71 mode = inlineMode72 }73 74 return fmt.Sprintf("\n\n You're in %s\n\n\n", keywordStyle.Render(mode)) +64- helpStyle.Render(" space: switch modes • q: exit\n")75+ helpStyle.Render(" space: switch modes • ctrl-z: suspend • q: exit\n")76 }77 78 func main() {79 if _, err := tea.NewProgram(model{}).Run(); err != nil {4 unmodified linesmodified:examples/simple/testdata/TestApp.golden+5 added-3 removed
1-[?25l[?2004h
Hi. This program will exit in 10 seconds. To quit sooner press any key2-[70D[A[70D[2KHi. This program will exit in 9 seconds. To quit sooner press any key.3-[70D[2K[?2004l[?25h[?1002l[?1003l[?1006l1+[?25l[?2004h
Hi. This program will exit in 10 seconds.2+3+To quit sooner press ctrl-c, or press ctrl-z to suspend...4+[70D[A[A[A[70D[2KHi. This program will exit in 9 seconds.5+[B[B[70D[2K[?2004l[?25h[?1002l[?1003l[?1006lmodified:examples/simple/main.go+9 added-3 removed
41 unmodified lines42 // Update is called when messages are received. The idea is that you inspect the43 // message and send back an updated model accordingly. You can also return44 // a command, which is a function that performs I/O and returns a message.45 func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {46- switch msg.(type) {46+ switch msg := msg.(type) {47 case tea.KeyMsg:48+ switch msg.String() {49+ case "ctrl+c", "q":48- return m, tea.Quit50+ return m, tea.Quit51+ case "ctrl+z":52+ return m, tea.Suspend53+ }54+55 case tickMsg:56 m--57 if m <= 0 {58 return m, tea.Quit5 unmodified lines64 65 // View returns a string based on data in the model. That string which will be66 // rendered to the terminal.67 func (m model) View() string {62- return fmt.Sprintf("Hi. This program will exit in %d seconds. To quit sooner press any key.\n", m)68+ return fmt.Sprintf("Hi. This program will exit in %d seconds.\n\nTo quit sooner press ctrl-c, or press ctrl-z to suspend...\n", m)69 }70 71 // Messages are events that we respond to in our Update function. This72 // particular one indicates that the timer has ticked.6 unmodified linesadded:examples/suspend/main.go+50 added
1+package main2+3+import (4+ "fmt"5+ "os"6+7+ tea "github.com/charmbracelet/bubbletea"8+)9+10+type model struct {11+ quitting bool12+ suspending bool13+}14+15+func (m model) Init() tea.Cmd {16+ return nil17+}18+19+func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {20+ switch msg := msg.(type) {21+ case tea.ResumeMsg:22+ m.suspending = false23+ return m, nil24+ case tea.KeyMsg:25+ switch msg.String() {26+ case "q", "ctrl+c", "esc":27+ m.quitting = true28+ return m, tea.Quit29+ case "ctrl+z":30+ m.suspending = true31+ return m, tea.Suspend32+ }33+ }34+ return m, nil35+}36+37+func (m model) View() string {38+ if m.suspending || m.quitting {39+ return ""40+ }41+42+ return "\nPress ctrl-z to suspend, or ctrl+c to exit\n"43+}44+45+func main() {46+ if _, err := tea.NewProgram(model{}).Run(); err != nil {47+ fmt.Println("Error running program:", err)48+ os.Exit(1)49+ }50+}modified:examples/go.mod+1 added-1 removed
19 unmodified lines20 github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect21 github.com/aymanbagabas/go-udiff v0.2.0 // indirect22 github.com/aymerick/douceur v0.2.0 // indirect23 github.com/charmbracelet/x/ansi v0.1.3 // indirect24- github.com/charmbracelet/x/exp/golden v0.0.0-20240521172236-71f88323a7ca // indirect24+ github.com/charmbracelet/x/exp/golden v0.0.0-20240710172756-1a5969e3146f // indirect25 github.com/charmbracelet/x/input v0.1.0 // indirect26 github.com/charmbracelet/x/term v0.1.1 // indirect27 github.com/charmbracelet/x/windows v0.1.0 // indirect28 github.com/dlclark/regexp2 v1.4.0 // indirect23 unmodified linesmodified:examples/go.sum+2 added-2 removed
18 unmodified lines19 github.com/charmbracelet/lipgloss v0.11.1 h1:a8KgVPHa7kOoP95vm2tQQrjD2AKhbWmfr4uJ2RW6kNk=20 github.com/charmbracelet/lipgloss v0.11.1/go.mod h1:beLlcmkF7MWA+5UrKKIRo/VJ21xGXr7YJ9miWfdMRIU=21 github.com/charmbracelet/x/ansi v0.1.3 h1:RBh/eleNWML5R524mjUF0yVRePTwqN9tPtV+DPgO5Lw=22 github.com/charmbracelet/x/ansi v0.1.3/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=23-github.com/charmbracelet/x/exp/golden v0.0.0-20240521172236-71f88323a7ca h1:Cw9p8EJdhDGIWICF34TIxTcQrAdzBdgkvaLA4AmqDVk=24-github.com/charmbracelet/x/exp/golden v0.0.0-20240521172236-71f88323a7ca/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=23+github.com/charmbracelet/x/exp/golden v0.0.0-20240710172756-1a5969e3146f h1:baeZl5dM2HFldOVxnFTWkljEe26O/o4i0bMFnIu1Q2M=24+github.com/charmbracelet/x/exp/golden v0.0.0-20240710172756-1a5969e3146f/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=25 github.com/charmbracelet/x/exp/teatest v0.0.0-20240521184646-23081fb03b28 h1:sOWKNRjt8uOEVgPiJVIJCse1+mUDM2F/vYY6W0Go640=26 github.com/charmbracelet/x/exp/teatest v0.0.0-20240521184646-23081fb03b28/go.mod h1:l1w+LTJZCCozeGzMEWGxRw6Mo2DfcZUvupz8HGubdes=27 github.com/charmbracelet/x/input v0.1.0 h1:TEsGSfZYQyOtp+STIjyBq6tpRaorH0qpwZUj8DavAhQ=28 github.com/charmbracelet/x/input v0.1.0/go.mod h1:ZZwaBxPF7IG8gWWzPUVqHEtWhc1+HXJPNuerJGRGZ28=62 unmodified linesmodified:tea.go+21 added
179 unmodified lines180 // QuitMsg signals that the program should quit. You can send a QuitMsg with181 // Quit.182 type QuitMsg struct{}183 184+// Suspend is a special command that tells the Bubble Tea program to suspend.185+func Suspend() Msg {186+ return SuspendMsg{}187+}188+189+// SuspendMsg signals the program should suspend.190+// This usually happens when ctrl+z is pressed on common programs, but since191+// bubbletea puts the terminal in raw mode, we need to handle it in a192+// per-program basis.193+// You can send this message with Suspend.194+type SuspendMsg struct{}195+196+// ResumeMsg can be listen to to do something once a program is resumed back197+// from a suspend state.198+type ResumeMsg struct{}199+200 // NewProgram creates a new Program.201 func NewProgram(model Model, opts ...ProgramOption) *Program {202 p := &Program{203 initialModel: model,138 unmodified lines342 switch msg := msg.(type) {343 case QuitMsg:344 return model, nil345 346+ case SuspendMsg:347+ if suspendSupported {348+ p.suspend()349+ }350+351 case clearScreenMsg:352 p.renderer.clearScreen()353 354 case enterAltScreenMsg:393 unmodified linesmodified:tty_unix.go+13 added
4 unmodified lines5 6 import (7 "fmt"8 "os"9+ "os/signal"10+ "syscall"11 12 "github.com/charmbracelet/x/term"13 )14 20 unmodified lines35 return nil, fmt.Errorf("could not open a new TTY: %w", err)36 }37 return f, nil38 }39+40+const suspendSupported = true41+42+// Send SIGTSTP to the entire process group.43+func suspendProcess() {44+ c := make(chan os.Signal, 1)45+ signal.Notify(c, syscall.SIGCONT)46+ _ = syscall.Kill(0, syscall.SIGTSTP)47+ // blocks until a CONT happens...48+ <-c49+}modified:tty_windows.go+4 added
60 unmodified lines61 return nil, err62 }63 return f, nil64 }65+66+const suspendSupported = false67+68+func suspendProcess() {}modified:tty.go+12 added
8 unmodified lines9 "github.com/charmbracelet/x/term"10 "github.com/muesli/cancelreader"11 )12 13+func (p *Program) suspend() {14+ if err := p.ReleaseTerminal(); err != nil {15+ // If we can't release input, abort.16+ return17+ }18+19+ suspendProcess()20+21+ _ = p.RestoreTerminal()22+ go p.Send(ResumeMsg{})23+}24+25 func (p *Program) initTerminal() error {26 if err := p.initInput(); err != nil {27 return err28 }99 unmodified lines