editor.go

  1package chat
  2
  3import (
  4	"os"
  5	"os/exec"
  6
  7	"github.com/charmbracelet/bubbles/key"
  8	"github.com/charmbracelet/bubbles/textarea"
  9	tea "github.com/charmbracelet/bubbletea"
 10	"github.com/charmbracelet/lipgloss"
 11	"github.com/kujtimiihoxha/opencode/internal/app"
 12	"github.com/kujtimiihoxha/opencode/internal/session"
 13	"github.com/kujtimiihoxha/opencode/internal/tui/layout"
 14	"github.com/kujtimiihoxha/opencode/internal/tui/styles"
 15	"github.com/kujtimiihoxha/opencode/internal/tui/util"
 16)
 17
 18type editorCmp struct {
 19	app      *app.App
 20	session  session.Session
 21	textarea textarea.Model
 22}
 23
 24type FocusEditorMsg bool
 25
 26type focusedEditorKeyMaps struct {
 27	Send       key.Binding
 28	OpenEditor key.Binding
 29	Blur       key.Binding
 30}
 31
 32type bluredEditorKeyMaps struct {
 33	Send       key.Binding
 34	Focus      key.Binding
 35	OpenEditor key.Binding
 36}
 37
 38var focusedKeyMaps = focusedEditorKeyMaps{
 39	Send: key.NewBinding(
 40		key.WithKeys("ctrl+s"),
 41		key.WithHelp("ctrl+s", "send message"),
 42	),
 43	Blur: key.NewBinding(
 44		key.WithKeys("esc"),
 45		key.WithHelp("esc", "focus messages"),
 46	),
 47	OpenEditor: key.NewBinding(
 48		key.WithKeys("ctrl+e"),
 49		key.WithHelp("ctrl+e", "open editor"),
 50	),
 51}
 52
 53var bluredKeyMaps = bluredEditorKeyMaps{
 54	Send: key.NewBinding(
 55		key.WithKeys("ctrl+s", "enter"),
 56		key.WithHelp("ctrl+s/enter", "send message"),
 57	),
 58	Focus: key.NewBinding(
 59		key.WithKeys("i"),
 60		key.WithHelp("i", "focus editor"),
 61	),
 62	OpenEditor: key.NewBinding(
 63		key.WithKeys("ctrl+e"),
 64		key.WithHelp("ctrl+e", "open editor"),
 65	),
 66}
 67
 68func openEditor() tea.Cmd {
 69	editor := os.Getenv("EDITOR")
 70	if editor == "" {
 71		editor = "nvim"
 72	}
 73
 74	tmpfile, err := os.CreateTemp("", "msg_*.md")
 75	if err != nil {
 76		return util.ReportError(err)
 77	}
 78	tmpfile.Close()
 79	c := exec.Command(editor, tmpfile.Name()) //nolint:gosec
 80	c.Stdin = os.Stdin
 81	c.Stdout = os.Stdout
 82	c.Stderr = os.Stderr
 83	return tea.ExecProcess(c, func(err error) tea.Msg {
 84		if err != nil {
 85			return util.ReportError(err)
 86		}
 87		content, err := os.ReadFile(tmpfile.Name())
 88		if err != nil {
 89			return util.ReportError(err)
 90		}
 91		os.Remove(tmpfile.Name())
 92		return SendMsg{
 93			Text: string(content),
 94		}
 95	})
 96}
 97
 98func (m *editorCmp) Init() tea.Cmd {
 99	return textarea.Blink
100}
101
102func (m *editorCmp) send() tea.Cmd {
103	if m.app.CoderAgent.IsSessionBusy(m.session.ID) {
104		return util.ReportWarn("Agent is working, please wait...")
105	}
106
107	value := m.textarea.Value()
108	m.textarea.Reset()
109	m.textarea.Blur()
110	if value == "" {
111		return nil
112	}
113	return tea.Batch(
114		util.CmdHandler(SendMsg{
115			Text: value,
116		}),
117	)
118}
119
120func (m *editorCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
121	var cmd tea.Cmd
122	switch msg := msg.(type) {
123	case SessionSelectedMsg:
124		if msg.ID != m.session.ID {
125			m.session = msg
126		}
127		return m, nil
128	case FocusEditorMsg:
129		if msg {
130			m.textarea.Focus()
131			return m, tea.Batch(textarea.Blink, util.CmdHandler(EditorFocusMsg(true)))
132		}
133	case tea.KeyMsg:
134		if key.Matches(msg, focusedKeyMaps.OpenEditor) {
135			if m.app.CoderAgent.IsSessionBusy(m.session.ID) {
136				return m, util.ReportWarn("Agent is working, please wait...")
137			}
138			return m, openEditor()
139		}
140		// if the key does not match any binding, return
141		if m.textarea.Focused() && key.Matches(msg, focusedKeyMaps.Send) {
142			return m, m.send()
143		}
144		if !m.textarea.Focused() && key.Matches(msg, bluredKeyMaps.Send) {
145			return m, m.send()
146		}
147		if m.textarea.Focused() && key.Matches(msg, focusedKeyMaps.Blur) {
148			m.textarea.Blur()
149			return m, util.CmdHandler(EditorFocusMsg(false))
150		}
151		if !m.textarea.Focused() && key.Matches(msg, bluredKeyMaps.Focus) {
152			m.textarea.Focus()
153			return m, tea.Batch(textarea.Blink, util.CmdHandler(EditorFocusMsg(true)))
154		}
155	}
156	m.textarea, cmd = m.textarea.Update(msg)
157	return m, cmd
158}
159
160func (m *editorCmp) View() string {
161	style := lipgloss.NewStyle().Padding(0, 0, 0, 1).Bold(true)
162
163	return lipgloss.JoinHorizontal(lipgloss.Top, style.Render(">"), m.textarea.View())
164}
165
166func (m *editorCmp) SetSize(width, height int) tea.Cmd {
167	m.textarea.SetWidth(width - 3) // account for the prompt and padding right
168	m.textarea.SetHeight(height)
169	return nil
170}
171
172func (m *editorCmp) GetSize() (int, int) {
173	return m.textarea.Width(), m.textarea.Height()
174}
175
176func (m *editorCmp) BindingKeys() []key.Binding {
177	bindings := []key.Binding{}
178	if m.textarea.Focused() {
179		bindings = append(bindings, layout.KeyMapToSlice(focusedKeyMaps)...)
180	} else {
181		bindings = append(bindings, layout.KeyMapToSlice(bluredKeyMaps)...)
182	}
183
184	bindings = append(bindings, layout.KeyMapToSlice(m.textarea.KeyMap)...)
185	return bindings
186}
187
188func NewEditorCmp(app *app.App) tea.Model {
189	ti := textarea.New()
190	ti.Prompt = " "
191	ti.ShowLineNumbers = false
192	ti.BlurredStyle.Base = ti.BlurredStyle.Base.Background(styles.Background)
193	ti.BlurredStyle.CursorLine = ti.BlurredStyle.CursorLine.Background(styles.Background)
194	ti.BlurredStyle.Placeholder = ti.BlurredStyle.Placeholder.Background(styles.Background)
195	ti.BlurredStyle.Text = ti.BlurredStyle.Text.Background(styles.Background)
196
197	ti.FocusedStyle.Base = ti.FocusedStyle.Base.Background(styles.Background)
198	ti.FocusedStyle.CursorLine = ti.FocusedStyle.CursorLine.Background(styles.Background)
199	ti.FocusedStyle.Placeholder = ti.FocusedStyle.Placeholder.Background(styles.Background)
200	ti.FocusedStyle.Text = ti.BlurredStyle.Text.Background(styles.Background)
201	ti.CharLimit = -1
202	ti.Focus()
203	return &editorCmp{
204		app:      app,
205		textarea: ti,
206	}
207}