* docs(examples): update textinput examples per Bubbles v2 API changes * docs(examples): clean up textinput example a little * docs(examples): update examples using textinput/textarea per API updates * docs(examples): bump bubbles/v2 to latest v2-exp
78 lines
1.5 KiB
Go
78 lines
1.5 KiB
Go
package main
|
||
|
||
// A simple program demonstrating the text input component from the Bubbles
|
||
// component library.
|
||
|
||
import (
|
||
"log"
|
||
|
||
"github.com/charmbracelet/bubbles/v2/textinput"
|
||
tea "github.com/charmbracelet/bubbletea/v2"
|
||
"github.com/charmbracelet/lipgloss/v2"
|
||
)
|
||
|
||
func main() {
|
||
p := tea.NewProgram(initialModel())
|
||
if _, err := p.Run(); err != nil {
|
||
log.Fatal(err)
|
||
}
|
||
}
|
||
|
||
type (
|
||
errMsg error
|
||
)
|
||
|
||
type model struct {
|
||
textInput textinput.Model
|
||
err error
|
||
quitting bool
|
||
}
|
||
|
||
func initialModel() model {
|
||
ti := textinput.New()
|
||
ti.Placeholder = "Pikachu"
|
||
ti.SetVirtualCursor(false)
|
||
ti.Focus()
|
||
ti.CharLimit = 156
|
||
ti.SetWidth(20)
|
||
|
||
return model{textInput: ti}
|
||
}
|
||
|
||
func (m model) Init() tea.Cmd {
|
||
return textinput.Blink
|
||
}
|
||
|
||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||
var cmd tea.Cmd
|
||
|
||
switch msg := msg.(type) {
|
||
case tea.KeyPressMsg:
|
||
switch msg.String() {
|
||
case "enter", "ctrl+c", "esc":
|
||
m.quitting = true
|
||
return m, tea.Quit
|
||
}
|
||
}
|
||
|
||
m.textInput, cmd = m.textInput.Update(msg)
|
||
return m, cmd
|
||
}
|
||
|
||
func (m model) View() (string, *tea.Cursor) {
|
||
var c *tea.Cursor
|
||
if !m.textInput.VirtualCursor() {
|
||
c = m.textInput.Cursor()
|
||
c.Y += lipgloss.Height(m.headerView())
|
||
}
|
||
|
||
str := lipgloss.JoinVertical(lipgloss.Top, m.headerView(), m.textInput.View(), m.footerView())
|
||
if m.quitting {
|
||
str += "\n"
|
||
}
|
||
|
||
return str, c
|
||
}
|
||
|
||
func (m model) headerView() string { return "What’s your favorite Pokémon?\n" }
|
||
func (m model) footerView() string { return "\n(esc to quit)" }
|