editor.go

 1package chat
 2
 3import (
 4	"github.com/charmbracelet/bubbles/key"
 5	"github.com/charmbracelet/bubbles/textarea"
 6	tea "github.com/charmbracelet/bubbletea"
 7	"github.com/charmbracelet/lipgloss"
 8	"github.com/kujtimiihoxha/termai/internal/tui/layout"
 9	"github.com/kujtimiihoxha/termai/internal/tui/styles"
10)
11
12type editorCmp struct {
13	textarea textarea.Model
14}
15
16func (m *editorCmp) Init() tea.Cmd {
17	return textarea.Blink
18}
19
20func (m *editorCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
21	var cmd tea.Cmd
22	m.textarea, cmd = m.textarea.Update(msg)
23	return m, cmd
24}
25
26func (m *editorCmp) View() string {
27	style := lipgloss.NewStyle().Padding(0, 0, 0, 1).Bold(true)
28
29	return lipgloss.JoinHorizontal(lipgloss.Top, style.Render(">"), m.textarea.View())
30}
31
32func (m *editorCmp) SetSize(width, height int) {
33	m.textarea.SetWidth(width - 3) // account for the prompt and padding right
34	m.textarea.SetHeight(height)
35}
36
37func (m *editorCmp) GetSize() (int, int) {
38	return m.textarea.Width(), m.textarea.Height()
39}
40
41func (m *editorCmp) BindingKeys() []key.Binding {
42	return layout.KeyMapToSlice(m.textarea.KeyMap)
43}
44
45func NewEditorCmp() tea.Model {
46	ti := textarea.New()
47	ti.Prompt = " "
48	ti.ShowLineNumbers = false
49	ti.BlurredStyle.Base = ti.BlurredStyle.Base.Background(styles.Background)
50	ti.BlurredStyle.CursorLine = ti.BlurredStyle.CursorLine.Background(styles.Background)
51	ti.BlurredStyle.Placeholder = ti.BlurredStyle.Placeholder.Background(styles.Background)
52	ti.BlurredStyle.Text = ti.BlurredStyle.Text.Background(styles.Background)
53
54	ti.FocusedStyle.Base = ti.FocusedStyle.Base.Background(styles.Background)
55	ti.FocusedStyle.CursorLine = ti.FocusedStyle.CursorLine.Background(styles.Background)
56	ti.FocusedStyle.Placeholder = ti.FocusedStyle.Placeholder.Background(styles.Background)
57	ti.FocusedStyle.Text = ti.BlurredStyle.Text.Background(styles.Background)
58	ti.Focus()
59	return &editorCmp{
60		textarea: ti,
61	}
62}