main.go
This file
Path
examples/textinput/main.go
80 lines · 1.4 KiB · Go
Last changed by
Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
History of this fileTake it
The walk stopped after 200 commits: lines older than c07fa72 point at it.
chore(ci): sync golangci-lint configaymanbagabasbefore adoptionc07fa72
lines 1–8why, one step back1package main23// A simple program demonstrating the text input component from the Bubbles4// component library.56import (7 "log"8Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
lines 9–11why, one step back9 "charm.land/bubbles/v2/textinput"10 tea "charm.land/bubbletea/v2"11 "charm.land/lipgloss/v2"chore(ci): sync golangci-lint configaymanbagabasbefore adoptionc07fa72
lines 12–27why, one step back12)1314func main() {15 p := tea.NewProgram(initialModel())16 if _, err := p.Run(); err != nil {17 log.Fatal(err)18 }19}2021type (22 errMsg error23)2425type model struct {26 textInput textinput.Model27 err errorMerge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 28why, one step back28 quitting boolchore(ci): sync golangci-lint configaymanbagabasbefore adoptionc07fa72
lines 29–33why, one step back29}3031func initialModel() model {32 ti := textinput.New()33 ti.Placeholder = "Pikachu"Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 34why, one step back34 ti.SetVirtualCursor(false)chore(ci): sync golangci-lint configaymanbagabasbefore adoptionc07fa72
lines 35–36why, one step back35 ti.Focus()36 ti.CharLimit = 156Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 37why, one step back37 ti.SetWidth(20)chore(ci): sync golangci-lint configaymanbagabasbefore adoptionc07fa72
line 38why, one step back38Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 39why, one step back39 return model{textInput: ti}chore(ci): sync golangci-lint configaymanbagabasbefore adoptionc07fa72
lines 40–49why, one step back40}4142func (m model) Init() tea.Cmd {43 return textinput.Blink44}4546func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {47 var cmd tea.Cmd4849 switch msg := msg.(type) {Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
lines 50–53why, one step back50 case tea.KeyPressMsg:51 switch msg.String() {52 case "enter", "ctrl+c", "esc":53 m.quitting = truechore(ci): sync golangci-lint configaymanbagabasbefore adoptionc07fa72
lines 54–61why, one step back54 return m, tea.Quit55 }56 }5758 m.textInput, cmd = m.textInput.Update(msg)59 return m, cmd60}61Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
lines 62–76why, one step back62func (m model) View() tea.View {63 var c *tea.Cursor64 if !m.textInput.VirtualCursor() {65 c = m.textInput.Cursor()66 c.Y += lipgloss.Height(m.headerView())67 }6869 str := lipgloss.JoinVertical(lipgloss.Top, m.headerView(), m.textInput.View(), m.footerView())70 if m.quitting {71 str += "\n"72 }7374 v := tea.NewView(str)75 v.Cursor = c76 return vchore(ci): sync golangci-lint configaymanbagabasbefore adoptionc07fa72
line 77why, one step back77}Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
lines 78–80why, one step back7879func (m model) headerView() string { return "What’s your favorite Pokémon?\n" }80func (m model) footerView() string { return "\n(esc to quit)" }