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

1package main23// A simple example demonstrating the use of multiple text input components4// from the Bubbles component library.56import (7	"fmt"8	"os"9	"strings"1011	"charm.land/bubbles/v2/cursor"12	"charm.land/bubbles/v2/textinput"13	tea "charm.land/bubbletea/v2"14	"charm.land/lipgloss/v2"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.Mode33	quitting   bool34}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 = 324546		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)5354		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) {79	case tea.KeyPressMsg:80		switch msg.String() {81		case "ctrl+c", "esc":82			m.quitting = true83			return m, tea.Quit8485		// 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 {93				s := m.inputs[i].Styles()94				s.Cursor.Blink = m.cursorMode == cursor.CursorBlink95				m.inputs[i].SetStyles(s)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}154155func (m model) View() tea.View {156	var b strings.Builder157	var c *tea.Cursor158159	for i, in := range m.inputs {160		b.WriteString(m.inputs[i].View())161		if i < len(m.inputs)-1 {162			b.WriteRune('\n')163		}164		if m.cursorMode != cursor.CursorHide && in.Focused() {165			c = in.Cursor()166			if c != nil {167				c.Y += i168			}169		}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)"))181182	if m.quitting {183		b.WriteRune('\n')184	}185186	v := tea.NewView(b.String())187	v.Cursor = c188	return v189}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}