editor.go 5.0 KB

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