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 fileTake it
The walk stopped after 200 commits: lines older than 3dfbcd9 point at it.
feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 1–7why, one step back1package main23import (4 "fmt"5 "os"6 "strings"7Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
lines 8–11why, one step back8 "charm.land/bubbles/v2/help"9 "charm.land/bubbles/v2/key"10 tea "charm.land/bubbletea/v2"11 "charm.land/lipgloss/v2"feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 12–91why, one step back12)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.chore(examples): update help example to use SetWidthAyman Bagabasbefore adoptionbf89717
line 92why, one step back92 m.help.SetWidth(msg.Width)feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
line 93why, one step back93Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 94why, one step back94 case tea.KeyPressMsg:feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 95–114why, one step back95 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}114Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 115why, one step back115func (m model) View() tea.View {feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
line 116why, one step back116 if m.quitting {Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 117why, one step back117 return tea.NewView("Bye!\n")feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 118–129why, one step back118 }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")129Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 130why, one step back130 return tea.NewView(status + strings.Repeat("\n", height) + helpView)feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 131–147why, one step back131}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}