Skip to content
Myr

confirm.go

This file

Path

confirm/confirm.go

168 lines · 3.6 KiB · Go

Last changed by

chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e

History of this file

Take it

1// Package confirm provides an interface to ask a user to confirm an action.2// The user is provided with an interface to choose an affirmative or negative3// answer, which is then reflected in the exit code for use in scripting.4//5// If the user selects the affirmative answer, the program exits with 0. If the6// user selects the negative answer, the program exits with 1.7//8// I.e. confirm if the user wants to delete a file9//10// $ gum confirm "Are you sure?" && rm file.txt11package confirm1213import (14	"charm.land/bubbles/v2/help"15	"charm.land/bubbles/v2/key"1617	tea "charm.land/bubbletea/v2"18	"charm.land/lipgloss/v2"19)2021func defaultKeymap(affirmative, negative string) keymap {22	return keymap{23		Abort: key.NewBinding(24			key.WithKeys("ctrl+c"),25			key.WithHelp("ctrl+c", "cancel"),26		),27		Quit: key.NewBinding(28			key.WithKeys("esc"),29			key.WithHelp("esc", "quit"),30		),31		Negative: key.NewBinding(32			key.WithKeys("n", "N", "q"),33			key.WithHelp("n", negative),34		),35		Affirmative: key.NewBinding(36			key.WithKeys("y", "Y"),37			key.WithHelp("y", affirmative),38		),39		Toggle: key.NewBinding(40			key.WithKeys(41				"left",42				"h",43				"ctrl+n",44				"shift+tab",45				"right",46				"l",47				"ctrl+p",48				"tab",49			),50			key.WithHelp("←→", "toggle"),51		),52		Submit: key.NewBinding(53			key.WithKeys("enter"),54			key.WithHelp("enter", "submit"),55		),56	}57}5859type keymap struct {60	Abort       key.Binding61	Quit        key.Binding62	Negative    key.Binding63	Affirmative key.Binding64	Toggle      key.Binding65	Submit      key.Binding66}6768// FullHelp implements help.KeyMap.69func (k keymap) FullHelp() [][]key.Binding { return nil }7071// ShortHelp implements help.KeyMap.72func (k keymap) ShortHelp() []key.Binding {73	return []key.Binding{k.Toggle, k.Submit, k.Affirmative, k.Negative}74}7576type model struct {77	prompt      string78	affirmative string79	negative    string80	quitting    bool81	showHelp    bool82	help        help.Model83	keys        keymap8485	showOutput   bool86	confirmation bool8788	defaultSelection bool8990	// styles91	promptStyle     lipgloss.Style92	selectedStyle   lipgloss.Style93	unselectedStyle lipgloss.Style94	padding         []int95}9697func (m model) Init() tea.Cmd { return nil }9899func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {100	switch msg := msg.(type) {101	case tea.WindowSizeMsg:102		return m, nil103	case tea.KeyPressMsg:104		switch {105		case key.Matches(msg, m.keys.Abort):106			m.confirmation = false107			return m, tea.Interrupt108		case key.Matches(msg, m.keys.Quit):109			m.confirmation = false110			m.quitting = true111			return m, tea.Quit112		case key.Matches(msg, m.keys.Negative):113			m.confirmation = false114			m.quitting = true115			return m, tea.Quit116		case key.Matches(msg, m.keys.Toggle):117			if m.negative == "" {118				break119			}120			m.confirmation = !m.confirmation121		case key.Matches(msg, m.keys.Submit):122			m.quitting = true123			return m, tea.Quit124		case key.Matches(msg, m.keys.Affirmative):125			m.quitting = true126			m.confirmation = true127			return m, tea.Quit128		}129	}130	return m, nil131}132133func (m model) View() tea.View {134	if m.quitting {135		return tea.NewView("")136	}137138	var aff, neg string139140	if m.confirmation {141		aff = m.selectedStyle.Render(m.affirmative)142		neg = m.unselectedStyle.Render(m.negative)143	} else {144		aff = m.unselectedStyle.Render(m.affirmative)145		neg = m.selectedStyle.Render(m.negative)146	}147148	// If the option is intentionally empty, do not show it.149	if m.negative == "" {150		neg = ""151	}152153	parts := []string{154		m.promptStyle.Render(m.prompt) + "\n",155		lipgloss.JoinHorizontal(lipgloss.Left, aff, neg),156	}157158	if m.showHelp {159		parts = append(parts, "", m.help.View(m.keys))160	}161162	return tea.NewView(lipgloss.NewStyle().163		Padding(m.padding...).164		Render(lipgloss.JoinVertical(165			lipgloss.Left,166			parts...,167		)))168}