editor.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. if m.app.CoderAgent.IsSessionBusy(m.session.ID) {
  122. return m, util.ReportWarn("Agent is working, please wait...")
  123. }
  124. return m, openEditor()
  125. }
  126. // if the key does not match any binding, return
  127. if m.textarea.Focused() && key.Matches(msg, focusedKeyMaps.Send) {
  128. return m, m.send()
  129. }
  130. if !m.textarea.Focused() && key.Matches(msg, bluredKeyMaps.Send) {
  131. return m, m.send()
  132. }
  133. if m.textarea.Focused() && key.Matches(msg, focusedKeyMaps.Blur) {
  134. m.textarea.Blur()
  135. return m, util.CmdHandler(EditorFocusMsg(false))
  136. }
  137. if !m.textarea.Focused() && key.Matches(msg, bluredKeyMaps.Focus) {
  138. m.textarea.Focus()
  139. return m, tea.Batch(textarea.Blink, util.CmdHandler(EditorFocusMsg(true)))
  140. }
  141. }
  142. m.textarea, cmd = m.textarea.Update(msg)
  143. return m, cmd
  144. }
  145. func (m *editorCmp) View() string {
  146. style := lipgloss.NewStyle().Padding(0, 0, 0, 1).Bold(true)
  147. return lipgloss.JoinHorizontal(lipgloss.Top, style.Render(">"), m.textarea.View())
  148. }
  149. func (m *editorCmp) SetSize(width, height int) tea.Cmd {
  150. m.textarea.SetWidth(width - 3) // account for the prompt and padding right
  151. m.textarea.SetHeight(height)
  152. return nil
  153. }
  154. func (m *editorCmp) GetSize() (int, int) {
  155. return m.textarea.Width(), m.textarea.Height()
  156. }
  157. func (m *editorCmp) BindingKeys() []key.Binding {
  158. bindings := []key.Binding{}
  159. if m.textarea.Focused() {
  160. bindings = append(bindings, layout.KeyMapToSlice(focusedKeyMaps)...)
  161. } else {
  162. bindings = append(bindings, layout.KeyMapToSlice(bluredKeyMaps)...)
  163. }
  164. bindings = append(bindings, layout.KeyMapToSlice(m.textarea.KeyMap)...)
  165. return bindings
  166. }
  167. func NewEditorCmp(app *app.App) tea.Model {
  168. ti := textarea.New()
  169. ti.Prompt = " "
  170. ti.ShowLineNumbers = false
  171. ti.BlurredStyle.Base = ti.BlurredStyle.Base.Background(styles.Background)
  172. ti.BlurredStyle.CursorLine = ti.BlurredStyle.CursorLine.Background(styles.Background)
  173. ti.BlurredStyle.Placeholder = ti.BlurredStyle.Placeholder.Background(styles.Background)
  174. ti.BlurredStyle.Text = ti.BlurredStyle.Text.Background(styles.Background)
  175. ti.FocusedStyle.Base = ti.FocusedStyle.Base.Background(styles.Background)
  176. ti.FocusedStyle.CursorLine = ti.FocusedStyle.CursorLine.Background(styles.Background)
  177. ti.FocusedStyle.Placeholder = ti.FocusedStyle.Placeholder.Background(styles.Background)
  178. ti.FocusedStyle.Text = ti.BlurredStyle.Text.Background(styles.Background)
  179. ti.CharLimit = -1
  180. ti.Focus()
  181. return &editorCmp{
  182. app: app,
  183. textarea: ti,
  184. }
  185. }