editor.go 5.1 KB

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