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