Skip to content
Myr

main.go

This file

Path

examples/help/main.go

147 lines · 3.2 KiB · Go

Last changed by

chore(examples): update help example to use SetWidthAyman Bagabasbefore adoptionbf89717

History of this file

Take it

1package main23import (4	"fmt"5	"os"6	"strings"78	"charm.land/bubbles/v2/help"9	"charm.land/bubbles/v2/key"10	tea "charm.land/bubbletea/v2"11	"charm.land/lipgloss/v2"12)1314// keyMap defines a set of keybindings. To work for help it must satisfy15// key.Map. It could also very easily be a map[string]key.Binding.16type keyMap struct {17	Up    key.Binding18	Down  key.Binding19	Left  key.Binding20	Right key.Binding21	Help  key.Binding22	Quit  key.Binding23}2425// ShortHelp returns keybindings to be shown in the mini help view. It's part26// of the key.Map interface.27func (k keyMap) ShortHelp() []key.Binding {28	return []key.Binding{k.Help, k.Quit}29}3031// FullHelp returns keybindings for the expanded help view. It's part of the32// key.Map interface.33func (k keyMap) FullHelp() [][]key.Binding {34	return [][]key.Binding{35		{k.Up, k.Down, k.Left, k.Right}, // first column36		{k.Help, k.Quit},                // second column37	}38}3940var keys = keyMap{41	Up: key.NewBinding(42		key.WithKeys("up", "k"),43		key.WithHelp("↑/k", "move up"),44	),45	Down: key.NewBinding(46		key.WithKeys("down", "j"),47		key.WithHelp("↓/j", "move down"),48	),49	Left: key.NewBinding(50		key.WithKeys("left", "h"),51		key.WithHelp("←/h", "move left"),52	),53	Right: key.NewBinding(54		key.WithKeys("right", "l"),55		key.WithHelp("→/l", "move right"),56	),57	Help: key.NewBinding(58		key.WithKeys("?"),59		key.WithHelp("?", "toggle help"),60	),61	Quit: key.NewBinding(62		key.WithKeys("q", "esc", "ctrl+c"),63		key.WithHelp("q", "quit"),64	),65}6667type model struct {68	keys       keyMap69	help       help.Model70	inputStyle lipgloss.Style71	lastKey    string72	quitting   bool73}7475func newModel() model {76	return model{77		keys:       keys,78		help:       help.New(),79		inputStyle: lipgloss.NewStyle().Foreground(lipgloss.Color("#FF75B7")),80	}81}8283func (m model) Init() tea.Cmd {84	return nil85}8687func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {88	switch msg := msg.(type) {89	case tea.WindowSizeMsg:90		// If we set a width on the help menu it can gracefully truncate91		// its view as needed.92		m.help.SetWidth(msg.Width)9394	case tea.KeyPressMsg:95		switch {96		case key.Matches(msg, m.keys.Up):97			m.lastKey = "↑"98		case key.Matches(msg, m.keys.Down):99			m.lastKey = "↓"100		case key.Matches(msg, m.keys.Left):101			m.lastKey = "←"102		case key.Matches(msg, m.keys.Right):103			m.lastKey = "→"104		case key.Matches(msg, m.keys.Help):105			m.help.ShowAll = !m.help.ShowAll106		case key.Matches(msg, m.keys.Quit):107			m.quitting = true108			return m, tea.Quit109		}110	}111112	return m, nil113}114115func (m model) View() tea.View {116	if m.quitting {117		return tea.NewView("Bye!\n")118	}119120	var status string121	if m.lastKey == "" {122		status = "Waiting for input..."123	} else {124		status = "You chose: " + m.inputStyle.Render(m.lastKey)125	}126127	helpView := m.help.View(m.keys)128	height := 8 - strings.Count(status, "\n") - strings.Count(helpView, "\n")129130	return tea.NewView(status + strings.Repeat("\n", height) + helpView)131}132133func main() {134	if os.Getenv("HELP_DEBUG") != "" {135		f, err := tea.LogToFile("debug.log", "help")136		if err != nil {137			fmt.Println("Couldn't open a file for logging:", err)138			os.Exit(1)139		}140		defer f.Close() // nolint:errcheck141	}142143	if _, err := tea.NewProgram(newModel()).Run(); err != nil {144		fmt.Printf("Could not start program :(\n%v\n", err)145		os.Exit(1)146	}147}