editor.go 5.3 KB

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