Skip to content
Myr

main.go

This file

Path

examples/prevent-quit/main.go

154 lines · 3.2 KiB · Go

Last changed by

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

History of this file

Take it

1package main23// A program demonstrating how to use the WithFilter option to intercept events.45import (6	"fmt"7	"log"89	"charm.land/bubbles/v2/help"10	"charm.land/bubbles/v2/key"11	"charm.land/bubbles/v2/textarea"12	tea "charm.land/bubbletea/v2"13	"charm.land/lipgloss/v2"14)1516var (17	choiceStyle   = lipgloss.NewStyle().PaddingLeft(1).Foreground(lipgloss.Color("241"))18	saveTextStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("170"))19	quitViewStyle = lipgloss.NewStyle().Padding(1, 3).Border(lipgloss.RoundedBorder()).BorderForeground(lipgloss.Color("170"))20)2122func main() {23	p := tea.NewProgram(initialModel(), tea.WithFilter(filter))2425	if _, err := p.Run(); err != nil {26		log.Fatal(err)27	}28}2930func filter(teaModel tea.Model, msg tea.Msg) tea.Msg {31	if _, ok := msg.(tea.QuitMsg); !ok {32		return msg33	}3435	m := teaModel.(model)36	if m.hasChanges {37		return nil38	}3940	return msg41}4243type model struct {44	textarea   textarea.Model45	help       help.Model46	keymap     keymap47	saveText   string48	hasChanges bool49	quitting   bool50}5152type keymap struct {53	save key.Binding54	quit key.Binding55}5657func initialModel() model {58	ti := textarea.New()59	ti.Placeholder = "Only the best words"60	ti.Focus()6162	return model{63		textarea: ti,64		help:     help.New(),65		keymap: keymap{66			save: key.NewBinding(67				key.WithKeys("ctrl+s"),68				key.WithHelp("ctrl+s", "save"),69			),70			quit: key.NewBinding(71				key.WithKeys("esc", "ctrl+c"),72				key.WithHelp("esc", "quit"),73			),74		},75	}76}7778func (m model) Init() tea.Cmd {79	return textarea.Blink80}8182func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {83	if m.quitting {84		return m.updatePromptView(msg)85	}8687	return m.updateTextView(msg)88}8990func (m model) updateTextView(msg tea.Msg) (tea.Model, tea.Cmd) {91	var cmds []tea.Cmd92	var cmd tea.Cmd9394	switch msg := msg.(type) {95	case tea.KeyPressMsg:96		m.saveText = ""97		switch {98		case key.Matches(msg, m.keymap.save):99			m.saveText = "Changes saved!"100			m.hasChanges = false101		case key.Matches(msg, m.keymap.quit):102			m.quitting = true103			return m, tea.Quit104		case len(msg.Text) > 0:105			m.saveText = ""106			m.hasChanges = true107			fallthrough108		default:109			if !m.textarea.Focused() {110				cmd = m.textarea.Focus()111				cmds = append(cmds, cmd)112			}113		}114	}115	m.textarea, cmd = m.textarea.Update(msg)116	cmds = append(cmds, cmd)117	return m, tea.Batch(cmds...)118}119120func (m model) updatePromptView(msg tea.Msg) (tea.Model, tea.Cmd) {121	switch msg := msg.(type) {122	case tea.KeyPressMsg:123		// For simplicity's sake, we'll treat any key besides "y" as "no"124		if key.Matches(msg, m.keymap.quit) || msg.String() == "y" {125			m.hasChanges = false126			return m, tea.Quit127		}128		m.quitting = false129	}130131	return m, nil132}133134func (m model) View() tea.View {135	if m.quitting {136		if m.hasChanges {137			text := lipgloss.JoinHorizontal(lipgloss.Top, "You have unsaved changes. Quit without saving?", choiceStyle.Render("[yN]"))138			return tea.NewView(quitViewStyle.Render(text))139		}140		return tea.NewView("Very important. Thank you.\n")141	}142143	helpView := m.help.ShortHelpView([]key.Binding{144		m.keymap.save,145		m.keymap.quit,146	})147148	return tea.NewView(fmt.Sprintf(149		"Type some important things.\n%s\n %s\n %s",150		m.textarea.View(),151		saveTextStyle.Render(m.saveText),152		helpView,153	) + "\n\n")154}