* 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
132 lines
3.2 KiB
Go
132 lines
3.2 KiB
Go
package main
|
|
|
|
// A simple program demonstrating the text area component from the Bubbles
|
|
// component library.
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/charmbracelet/bubbles/v2/cursor"
|
|
"github.com/charmbracelet/bubbles/v2/textarea"
|
|
"github.com/charmbracelet/bubbles/v2/viewport"
|
|
tea "github.com/charmbracelet/bubbletea/v2"
|
|
"github.com/charmbracelet/lipgloss/v2"
|
|
)
|
|
|
|
func main() {
|
|
p := tea.NewProgram(initialModel())
|
|
if _, err := p.Run(); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Oof: %v\n", err)
|
|
}
|
|
}
|
|
|
|
type model struct {
|
|
viewport viewport.Model
|
|
messages []string
|
|
textarea textarea.Model
|
|
senderStyle lipgloss.Style
|
|
err error
|
|
}
|
|
|
|
func initialModel() model {
|
|
ta := textarea.New()
|
|
ta.Placeholder = "Send a message..."
|
|
ta.SetVirtualCursor(false)
|
|
ta.Focus()
|
|
|
|
ta.Prompt = "┃ "
|
|
ta.CharLimit = 280
|
|
|
|
ta.SetWidth(30)
|
|
ta.SetHeight(3)
|
|
|
|
// Remove cursor line styling
|
|
s := ta.Styles()
|
|
s.Focused.CursorLine = lipgloss.NewStyle()
|
|
ta.SetStyles(s)
|
|
|
|
ta.ShowLineNumbers = false
|
|
|
|
vp := viewport.New(viewport.WithWidth(30), viewport.WithHeight(5))
|
|
vp.SetContent(`Welcome to the chat room!
|
|
Type a message and press Enter to send.`)
|
|
vp.KeyMap.Left.SetEnabled(false)
|
|
vp.KeyMap.Right.SetEnabled(false)
|
|
|
|
ta.KeyMap.InsertNewline.SetEnabled(false)
|
|
|
|
return model{
|
|
textarea: ta,
|
|
messages: []string{},
|
|
viewport: vp,
|
|
senderStyle: lipgloss.NewStyle().Foreground(lipgloss.Color("5")),
|
|
err: nil,
|
|
}
|
|
}
|
|
|
|
func (m model) Init() tea.Cmd {
|
|
return textarea.Blink
|
|
}
|
|
|
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
switch msg := msg.(type) {
|
|
case tea.WindowSizeMsg:
|
|
m.viewport.SetWidth(msg.Width)
|
|
m.textarea.SetWidth(msg.Width)
|
|
m.viewport.SetHeight(msg.Height - m.textarea.Height())
|
|
|
|
if len(m.messages) > 0 {
|
|
// Wrap content before setting it.
|
|
m.viewport.SetContent(lipgloss.NewStyle().Width(m.viewport.Width()).Render(strings.Join(m.messages, "\n")))
|
|
}
|
|
m.viewport.GotoBottom()
|
|
case tea.KeyPressMsg:
|
|
switch msg.String() {
|
|
case "ctrl+c", "esc":
|
|
fmt.Println(m.textarea.Value())
|
|
return m, tea.Quit
|
|
case "enter":
|
|
m.messages = append(m.messages, m.senderStyle.Render("You: ")+m.textarea.Value())
|
|
m.viewport.SetContent(lipgloss.NewStyle().Width(m.viewport.Width()).Render(strings.Join(m.messages, "\n")))
|
|
m.textarea.Reset()
|
|
m.viewport.GotoBottom()
|
|
return m, nil
|
|
default:
|
|
// Send all other keypresses to the textarea.
|
|
var cmd tea.Cmd
|
|
m.textarea, cmd = m.textarea.Update(msg)
|
|
return m, cmd
|
|
}
|
|
|
|
case cursor.BlinkMsg:
|
|
// Textarea should also process cursor blinks.
|
|
var cmd tea.Cmd
|
|
m.textarea, cmd = m.textarea.Update(msg)
|
|
return m, cmd
|
|
}
|
|
|
|
return m, nil
|
|
}
|
|
|
|
func (m model) View() string {
|
|
viewportView := m.viewport.View()
|
|
return viewportView + "\n" + m.textarea.View()
|
|
}
|
|
|
|
// To render a real cursor implement the Cursor() method on your model. To hide
|
|
// the cursor, return nil.
|
|
func (m model) Cursor() *tea.Cursor {
|
|
// Textarea provides a cursor implementation that can be used directly.
|
|
// This is the cursor position relative to the textarea, so you'll need to
|
|
// calculate the absolute position to the textarea.
|
|
//
|
|
// Note that when the virtual cursor is enabled, textarea.Cursor() will be
|
|
// nil.
|
|
c := m.textarea.Cursor()
|
|
if c != nil {
|
|
c.Y += lipgloss.Height(m.viewport.View())
|
|
}
|
|
return c
|
|
}
|