Skip to content
Myr

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 file

Take 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 back
1package main
23// A simple program demonstrating the text input component from the Bubbles4// component library.56import (7 "log"8

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

lines 9–11why, one step back
9 "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 back
12)
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 error

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

line 28why, one step back
28 quitting bool

chore(ci): sync golangci-lint configaymanbagabasbefore adoptionc07fa72

lines 29–33why, one step back
29}
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 back
34 ti.SetVirtualCursor(false)

chore(ci): sync golangci-lint configaymanbagabasbefore adoptionc07fa72

lines 35–36why, one step back
35 ti.Focus()
36 ti.CharLimit = 156

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

line 37why, one step back
37 ti.SetWidth(20)

chore(ci): sync golangci-lint configaymanbagabasbefore adoptionc07fa72

line 38why, one step back
38

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

line 39why, one step back
39 return model{textInput: ti}

chore(ci): sync golangci-lint configaymanbagabasbefore adoptionc07fa72

lines 40–49why, one step back
40}
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 back
50 case tea.KeyPressMsg:
51 switch msg.String() {52 case "enter", "ctrl+c", "esc":53 m.quitting = true

chore(ci): sync golangci-lint configaymanbagabasbefore adoptionc07fa72

lines 54–61why, one step back
54 return m, tea.Quit
55 }56 }5758 m.textInput, cmd = m.textInput.Update(msg)59 return m, cmd60}61

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

lines 62–76why, one step back
62func (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 v

chore(ci): sync golangci-lint configaymanbagabasbefore adoptionc07fa72

line 77why, one step back
77}

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

lines 78–80why, one step back
78
79func (m model) headerView() string { return "What’s your favorite Pokémon?\n" }80func (m model) footerView() string { return "\n(esc to quit)" }