Skip to content
Myr

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> ---------

authored by Carlos Alexandro Becker · committed by GitHub · parent 0208ac5 · Browse files · 10 files+131 added−12 removed
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

10 files+131 added−12 removed

modified:examples/altscreen-toggle/main.go+14 added-3 removed
12 unmodified lines
13 helpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("241"))14)1516type model struct {17 altscreen bool18 quitting bool17 altscreen bool18 quitting bool19 suspending bool20}2122func (m model) Init() tea.Cmd {23 return nil24}2526func (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.ExitAltScreen
7 unmodified lines
50 return m, nil51}5253func (m model) View() string {54 if m.suspending {55 return ""56 }5758 if m.quitting {59 return "Bye!\n"60 }61
9 unmodified lines
71 mode = inlineMode72 }7374 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}7778func main() {79 if _, err := tea.NewProgram(model{}).Run(); err != nil {
4 unmodified lines
modified: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 key2Hi. This program will exit in 9 seconds. To quit sooner press any key.3[?2004l[?25h[?1002l[?1003l[?1006l1[?25l[?2004h Hi. This program will exit in 10 seconds.23To quit sooner press ctrl-c, or press ctrl-z to suspend...4Hi. This program will exit in 9 seconds.5[?2004l[?25h[?1002l[?1003l[?1006l
modified:examples/simple/main.go+9 added-3 removed
41 unmodified lines
42// 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.45func (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 }5455 case tickMsg:56 m--57 if m <= 0 {58 return m, tea.Quit
5 unmodified lines
6465// View returns a string based on data in the model. That string which will be66// rendered to the terminal.67func (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}7071// Messages are events that we respond to in our Update function. This72// particular one indicates that the timer has ticked.
6 unmodified lines
added:examples/suspend/main.go+50 added
1package main23import (4 "fmt"5 "os"67 tea "github.com/charmbracelet/bubbletea"8)910type model struct {11 quitting bool12 suspending bool13}1415func (m model) Init() tea.Cmd {16 return nil17}1819func (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}3637func (m model) View() string {38 if m.suspending || m.quitting {39 return ""40 }4142 return "\nPress ctrl-z to suspend, or ctrl+c to exit\n"43}4445func 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 lines
20 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 // indirect
23 unmodified lines
modified:examples/go.sum+2 added-2 removed
18 unmodified lines
19github.com/charmbracelet/lipgloss v0.11.1 h1:a8KgVPHa7kOoP95vm2tQQrjD2AKhbWmfr4uJ2RW6kNk=20github.com/charmbracelet/lipgloss v0.11.1/go.mod h1:beLlcmkF7MWA+5UrKKIRo/VJ21xGXr7YJ9miWfdMRIU=21github.com/charmbracelet/x/ansi v0.1.3 h1:RBh/eleNWML5R524mjUF0yVRePTwqN9tPtV+DPgO5Lw=22github.com/charmbracelet/x/ansi v0.1.3/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=23github.com/charmbracelet/x/exp/golden v0.0.0-20240521172236-71f88323a7ca h1:Cw9p8EJdhDGIWICF34TIxTcQrAdzBdgkvaLA4AmqDVk=24github.com/charmbracelet/x/exp/golden v0.0.0-20240521172236-71f88323a7ca/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=23github.com/charmbracelet/x/exp/golden v0.0.0-20240710172756-1a5969e3146f h1:baeZl5dM2HFldOVxnFTWkljEe26O/o4i0bMFnIu1Q2M=24github.com/charmbracelet/x/exp/golden v0.0.0-20240710172756-1a5969e3146f/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=25github.com/charmbracelet/x/exp/teatest v0.0.0-20240521184646-23081fb03b28 h1:sOWKNRjt8uOEVgPiJVIJCse1+mUDM2F/vYY6W0Go640=26github.com/charmbracelet/x/exp/teatest v0.0.0-20240521184646-23081fb03b28/go.mod h1:l1w+LTJZCCozeGzMEWGxRw6Mo2DfcZUvupz8HGubdes=27github.com/charmbracelet/x/input v0.1.0 h1:TEsGSfZYQyOtp+STIjyBq6tpRaorH0qpwZUj8DavAhQ=28github.com/charmbracelet/x/input v0.1.0/go.mod h1:ZZwaBxPF7IG8gWWzPUVqHEtWhc1+HXJPNuerJGRGZ28=
62 unmodified lines
modified:tea.go+21 added
179 unmodified lines
180// QuitMsg signals that the program should quit. You can send a QuitMsg with181// Quit.182type QuitMsg struct{}183184// Suspend is a special command that tells the Bubble Tea program to suspend.185func Suspend() Msg {186 return SuspendMsg{}187}188189// 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.194type SuspendMsg struct{}195196// ResumeMsg can be listen to to do something once a program is resumed back197// from a suspend state.198type ResumeMsg struct{}199200// NewProgram creates a new Program.201func NewProgram(model Model, opts ...ProgramOption) *Program {202 p := &Program{203 initialModel: model,
138 unmodified lines
342 switch msg := msg.(type) {343 case QuitMsg:344 return model, nil345346 case SuspendMsg:347 if suspendSupported {348 p.suspend()349 }350351 case clearScreenMsg:352 p.renderer.clearScreen()353354 case enterAltScreenMsg:
393 unmodified lines
modified:tty_unix.go+13 added
4 unmodified lines
56import (7 "fmt"8 "os"9 "os/signal"10 "syscall"1112 "github.com/charmbracelet/x/term"13)14
20 unmodified lines
35 return nil, fmt.Errorf("could not open a new TTY: %w", err)36 }37 return f, nil38}3940const suspendSupported = true4142// Send SIGTSTP to the entire process group.43func 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 lines
61 return nil, err62 }63 return f, nil64}6566const suspendSupported = false6768func suspendProcess() {}
modified:tty.go+12 added
8 unmodified lines
9 "github.com/charmbracelet/x/term"10 "github.com/muesli/cancelreader"11)1213func (p *Program) suspend() {14 if err := p.ReleaseTerminal(); err != nil {15 // If we can't release input, abort.16 return17 }1819 suspendProcess()2021 _ = p.RestoreTerminal()22 go p.Send(ResumeMsg{})23}2425func (p *Program) initTerminal() error {26 if err := p.initInput(); err != nil {27 return err28 }
99 unmodified lines