Skip to content
Myr

main.go

This file

Path

examples/chat/main.go

123 lines · 2.8 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 program demonstrating the text area component from the Bubbles4// component library.56import (7	"fmt"8	"os"9	"strings"1011	"charm.land/bubbles/v2/cursor"12	"charm.land/bubbles/v2/textarea"13	"charm.land/bubbles/v2/viewport"14	tea "charm.land/bubbletea/v2"15	"charm.land/lipgloss/v2"16)1718func main() {19	p := tea.NewProgram(initialModel())20	if _, err := p.Run(); err != nil {21		fmt.Fprintf(os.Stderr, "Oof: %v\n", err)22	}23}2425type model struct {26	viewport    viewport.Model27	messages    []string28	textarea    textarea.Model29	senderStyle lipgloss.Style30	err         error31}3233func initialModel() model {34	ta := textarea.New()35	ta.Placeholder = "Send a message..."36	ta.SetVirtualCursor(false)37	ta.Focus()3839	ta.Prompt = "┃ "40	ta.CharLimit = 2804142	ta.SetWidth(30)43	ta.SetHeight(3)4445	// Remove cursor line styling46	s := ta.Styles()47	s.Focused.CursorLine = lipgloss.NewStyle()48	ta.SetStyles(s)4950	ta.ShowLineNumbers = false5152	vp := viewport.New(viewport.WithWidth(30), viewport.WithHeight(5))53	vp.SetContent(`Welcome to the chat room!54Type a message and press Enter to send.`)55	vp.KeyMap.Left.SetEnabled(false)56	vp.KeyMap.Right.SetEnabled(false)5758	ta.KeyMap.InsertNewline.SetEnabled(false)5960	return model{61		textarea:    ta,62		messages:    []string{},63		viewport:    vp,64		senderStyle: lipgloss.NewStyle().Foreground(lipgloss.Color("5")),65		err:         nil,66	}67}6869func (m model) Init() tea.Cmd {70	return textarea.Blink71}7273func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {74	switch msg := msg.(type) {75	case tea.WindowSizeMsg:76		m.viewport.SetWidth(msg.Width)77		m.textarea.SetWidth(msg.Width)78		m.viewport.SetHeight(msg.Height - m.textarea.Height())7980		if len(m.messages) > 0 {81			// Wrap content before setting it.82			m.viewport.SetContent(lipgloss.NewStyle().Width(m.viewport.Width()).Render(strings.Join(m.messages, "\n")))83		}84		m.viewport.GotoBottom()85	case tea.KeyPressMsg:86		switch msg.String() {87		case "ctrl+c", "esc":88			fmt.Println(m.textarea.Value())89			return m, tea.Quit90		case "enter":91			m.messages = append(m.messages, m.senderStyle.Render("You: ")+m.textarea.Value())92			m.viewport.SetContent(lipgloss.NewStyle().Width(m.viewport.Width()).Render(strings.Join(m.messages, "\n")))93			m.textarea.Reset()94			m.viewport.GotoBottom()95			return m, nil96		default:97			// Send all other keypresses to the textarea.98			var cmd tea.Cmd99			m.textarea, cmd = m.textarea.Update(msg)100			return m, cmd101		}102103	case cursor.BlinkMsg:104		// Textarea should also process cursor blinks.105		var cmd tea.Cmd106		m.textarea, cmd = m.textarea.Update(msg)107		return m, cmd108	}109110	return m, nil111}112113func (m model) View() tea.View {114	viewportView := m.viewport.View()115	v := tea.NewView(viewportView + "\n" + m.textarea.View())116	c := m.textarea.Cursor()117	if c != nil {118		c.Y += lipgloss.Height(viewportView)119	}120	v.Cursor = c121	v.AltScreen = true122	return v123}