Skip to content
Myr

main.go

This file

Path

examples/suspend/main.go

57 lines · 981 bytes · Go

Last changed by

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

History of this file

Take it

1package main23import (4	"errors"5	"fmt"6	"os"78	tea "charm.land/bubbletea/v2"9)1011type model struct {12	quitting   bool13	suspending bool14}1516func (m model) Init() tea.Cmd {17	return nil18}1920func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {21	switch msg := msg.(type) {22	case tea.ResumeMsg:23		m.suspending = false24		return m, nil25	case tea.KeyPressMsg:26		switch msg.String() {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		}37	}38	return m, nil39}4041func (m model) View() tea.View {42	if m.suspending || m.quitting {43		return tea.NewView("")44	}4546	return tea.NewView("\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}