editor.go 4.5 KB

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