Skip to content
Myr

main.go

This file

Path

examples/textinputs/main.go

196 lines · 4.2 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 3dfbcd9 point at it.

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 1–10why, one step back
1package main
23// A simple example demonstrating the use of multiple text input components4// from the Bubbles component library.56import (7 "fmt"8 "os"9 "strings"10

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

lines 11–14why, one step back
11 "charm.land/bubbles/v2/cursor"
12 "charm.land/bubbles/v2/textinput"13 tea "charm.land/bubbletea/v2"14 "charm.land/lipgloss/v2"

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 15–32why, one step back
15)
1617var (18 focusedStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))19 blurredStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("240"))20 cursorStyle = focusedStyle21 noStyle = lipgloss.NewStyle()22 helpStyle = blurredStyle23 cursorModeHelpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("244"))2425 focusedButton = focusedStyle.Render("[ Submit ]")26 blurredButton = fmt.Sprintf("[ %s ]", blurredStyle.Render("Submit"))27)2829type model struct {30 focusIndex int31 inputs []textinput.Model32 cursorMode cursor.Mode

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

line 33why, one step back
33 quitting bool

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 34–45why, one step back
34}
3536func initialModel() model {37 m := model{38 inputs: make([]textinput.Model, 3),39 }4041 var t textinput.Model42 for i := range m.inputs {43 t = textinput.New()44 t.CharLimit = 3245

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

lines 46–53why, one step back
46 s := t.Styles()
47 s.Cursor.Color = lipgloss.Color("205")48 s.Focused.Prompt = focusedStyle49 s.Focused.Text = focusedStyle50 s.Blurred.Prompt = blurredStyle51 s.Focused.Text = focusedStyle52 t.SetStyles(s)53

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 54–78why, one step back
54 switch i {
55 case 0:56 t.Placeholder = "Nickname"57 t.Focus()58 case 1:59 t.Placeholder = "Email"60 t.CharLimit = 6461 case 2:62 t.Placeholder = "Password"63 t.EchoMode = textinput.EchoPassword64 t.EchoCharacter = '•'65 }6667 m.inputs[i] = t68 }6970 return m71}7273func (m model) Init() tea.Cmd {74 return textinput.Blink75}7677func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {78 switch msg := msg.(type) {

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

line 79why, one step back
79 case tea.KeyPressMsg:

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 80–81why, one step back
80 switch msg.String() {
81 case "ctrl+c", "esc":

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

line 82why, one step back
82 m.quitting = true

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 83–92why, one step back
83 return m, tea.Quit
8485 // Change cursor mode86 case "ctrl+r":87 m.cursorMode++88 if m.cursorMode > cursor.CursorHide {89 m.cursorMode = cursor.CursorBlink90 }91 cmds := make([]tea.Cmd, len(m.inputs))92 for i := range m.inputs {

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

lines 93–95why, one step back
93 s := m.inputs[i].Styles()
94 s.Cursor.Blink = m.cursorMode == cursor.CursorBlink95 m.inputs[i].SetStyles(s)

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 96–154why, one step back
96 }
97 return m, tea.Batch(cmds...)9899 // Set focus to next input100 case "tab", "shift+tab", "enter", "up", "down":101 s := msg.String()102103 // Did the user press enter while the submit button was focused?104 // If so, exit.105 if s == "enter" && m.focusIndex == len(m.inputs) {106 return m, tea.Quit107 }108109 // Cycle indexes110 if s == "up" || s == "shift+tab" {111 m.focusIndex--112 } else {113 m.focusIndex++114 }115116 if m.focusIndex > len(m.inputs) {117 m.focusIndex = 0118 } else if m.focusIndex < 0 {119 m.focusIndex = len(m.inputs)120 }121122 cmds := make([]tea.Cmd, len(m.inputs))123 for i := 0; i <= len(m.inputs)-1; i++ {124 if i == m.focusIndex {125 // Set focused state126 cmds[i] = m.inputs[i].Focus()127 continue128 }129 // Remove focused state130 m.inputs[i].Blur()131 }132133 return m, tea.Batch(cmds...)134 }135 }136137 // Handle character input and blinking138 cmd := m.updateInputs(msg)139140 return m, cmd141}142143func (m *model) updateInputs(msg tea.Msg) tea.Cmd {144 cmds := make([]tea.Cmd, len(m.inputs))145146 // Only text inputs with Focus() set will respond, so it's safe to simply147 // update all of them here without any further logic.148 for i := range m.inputs {149 m.inputs[i], cmds[i] = m.inputs[i].Update(msg)150 }151152 return tea.Batch(cmds...)153}154

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

line 155why, one step back
155func (m model) View() tea.View {

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

line 156why, one step back
156 var b strings.Builder

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

line 157why, one step back
157 var c *tea.Cursor

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

line 158why, one step back
158

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

line 159why, one step back
159 for i, in := range m.inputs {

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 160–163why, one step back
160 b.WriteString(m.inputs[i].View())
161 if i < len(m.inputs)-1 {162 b.WriteRune('\n')163 }

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

lines 164–169why, one step back
164 if m.cursorMode != cursor.CursorHide && in.Focused() {
165 c = in.Cursor()166 if c != nil {167 c.Y += i168 }169 }

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 170–181why, one step back
170 }
171172 button := &blurredButton173 if m.focusIndex == len(m.inputs) {174 button = &focusedButton175 }176 fmt.Fprintf(&b, "\n\n%s\n\n", *button)177178 b.WriteString(helpStyle.Render("cursor mode is "))179 b.WriteString(cursorModeHelpStyle.Render(m.cursorMode.String()))180 b.WriteString(helpStyle.Render(" (ctrl+r to change style)"))181

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

lines 182–188why, one step back
182 if m.quitting {
183 b.WriteRune('\n')184 }185186 v := tea.NewView(b.String())187 v.Cursor = c188 return v

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 189–196why, one step back
189}
190191func main() {192 if _, err := tea.NewProgram(initialModel()).Run(); err != nil {193 fmt.Printf("could not start program: %s\n", err)194 os.Exit(1)195 }196}