editor.go 4.8 KB

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