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 return m, openEditor()
136 }
137 // if the key does not match any binding, return
138 if m.textarea.Focused() && key.Matches(msg, focusedKeyMaps.Send) {
139 return m, m.send()
140 }
141 if !m.textarea.Focused() && key.Matches(msg, bluredKeyMaps.Send) {
142 return m, m.send()
143 }
144 if m.textarea.Focused() && key.Matches(msg, focusedKeyMaps.Blur) {
145 m.textarea.Blur()
146 return m, util.CmdHandler(EditorFocusMsg(false))
147 }
148 if !m.textarea.Focused() && key.Matches(msg, bluredKeyMaps.Focus) {
149 m.textarea.Focus()
150 return m, tea.Batch(textarea.Blink, util.CmdHandler(EditorFocusMsg(true)))
151 }
152 }
153 m.textarea, cmd = m.textarea.Update(msg)
154 return m, cmd
155}
156
157func (m *editorCmp) View() string {
158 style := lipgloss.NewStyle().Padding(0, 0, 0, 1).Bold(true)
159
160 return lipgloss.JoinHorizontal(lipgloss.Top, style.Render(">"), m.textarea.View())
161}
162
163func (m *editorCmp) SetSize(width, height int) tea.Cmd {
164 m.textarea.SetWidth(width - 3) // account for the prompt and padding right
165 m.textarea.SetHeight(height)
166 return nil
167}
168
169func (m *editorCmp) GetSize() (int, int) {
170 return m.textarea.Width(), m.textarea.Height()
171}
172
173func (m *editorCmp) BindingKeys() []key.Binding {
174 bindings := []key.Binding{}
175 if m.textarea.Focused() {
176 bindings = append(bindings, layout.KeyMapToSlice(focusedKeyMaps)...)
177 } else {
178 bindings = append(bindings, layout.KeyMapToSlice(bluredKeyMaps)...)
179 }
180
181 bindings = append(bindings, layout.KeyMapToSlice(m.textarea.KeyMap)...)
182 return bindings
183}
184
185func NewEditorCmp(app *app.App) tea.Model {
186 ti := textarea.New()
187 ti.Prompt = " "
188 ti.ShowLineNumbers = false
189 ti.BlurredStyle.Base = ti.BlurredStyle.Base.Background(styles.Background)
190 ti.BlurredStyle.CursorLine = ti.BlurredStyle.CursorLine.Background(styles.Background)
191 ti.BlurredStyle.Placeholder = ti.BlurredStyle.Placeholder.Background(styles.Background)
192 ti.BlurredStyle.Text = ti.BlurredStyle.Text.Background(styles.Background)
193
194 ti.FocusedStyle.Base = ti.FocusedStyle.Base.Background(styles.Background)
195 ti.FocusedStyle.CursorLine = ti.FocusedStyle.CursorLine.Background(styles.Background)
196 ti.FocusedStyle.Placeholder = ti.FocusedStyle.Placeholder.Background(styles.Background)
197 ti.FocusedStyle.Text = ti.BlurredStyle.Text.Background(styles.Background)
198 ti.CharLimit = -1
199 ti.Focus()
200 return &editorCmp{
201 app: app,
202 textarea: ti,
203 }
204}