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/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() tea.Cmd {
  42. editor := os.Getenv("EDITOR")
  43. if editor == "" {
  44. editor = "nvim"
  45. }
  46. tmpfile, err := os.CreateTemp("", "msg_*.md")
  47. if err != nil {
  48. return util.ReportError(err)
  49. }
  50. tmpfile.Close()
  51. c := exec.Command(editor, tmpfile.Name()) //nolint:gosec
  52. c.Stdin = os.Stdin
  53. c.Stdout = os.Stdout
  54. c.Stderr = os.Stderr
  55. return tea.ExecProcess(c, func(err error) tea.Msg {
  56. if err != nil {
  57. return util.ReportError(err)
  58. }
  59. content, err := os.ReadFile(tmpfile.Name())
  60. if err != nil {
  61. return util.ReportError(err)
  62. }
  63. if len(content) == 0 {
  64. return util.ReportWarn("Message is empty")
  65. }
  66. os.Remove(tmpfile.Name())
  67. return SendMsg{
  68. Text: string(content),
  69. }
  70. })
  71. }
  72. func (m *editorCmp) Init() tea.Cmd {
  73. return textarea.Blink
  74. }
  75. func (m *editorCmp) send() tea.Cmd {
  76. if m.app.CoderAgent.IsSessionBusy(m.session.ID) {
  77. return util.ReportWarn("Agent is working, please wait...")
  78. }
  79. value := m.textarea.Value()
  80. m.textarea.Reset()
  81. if value == "" {
  82. return nil
  83. }
  84. return tea.Batch(
  85. util.CmdHandler(SendMsg{
  86. Text: value,
  87. }),
  88. )
  89. }
  90. func (m *editorCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  91. var cmd tea.Cmd
  92. switch msg := msg.(type) {
  93. case dialog.ThemeChangedMsg:
  94. m.textarea = CreateTextArea(&m.textarea)
  95. return m, nil
  96. case SessionSelectedMsg:
  97. if msg.ID != m.session.ID {
  98. m.session = msg
  99. }
  100. return m, nil
  101. case tea.KeyMsg:
  102. if key.Matches(msg, messageKeys.PageUp) || key.Matches(msg, messageKeys.PageDown) ||
  103. key.Matches(msg, messageKeys.HalfPageUp) || key.Matches(msg, messageKeys.HalfPageDown) {
  104. return m, nil
  105. }
  106. if key.Matches(msg, editorMaps.OpenEditor) {
  107. if m.app.CoderAgent.IsSessionBusy(m.session.ID) {
  108. return m, util.ReportWarn("Agent is working, please wait...")
  109. }
  110. return m, openEditor()
  111. }
  112. // Handle Enter key
  113. if m.textarea.Focused() && key.Matches(msg, editorMaps.Send) {
  114. value := m.textarea.Value()
  115. if len(value) > 0 && value[len(value)-1] == '\\' {
  116. // If the last character is a backslash, remove it and add a newline
  117. m.textarea.SetValue(value[:len(value)-1] + "\n")
  118. return m, nil
  119. } else {
  120. // Otherwise, send the message
  121. return m, m.send()
  122. }
  123. }
  124. }
  125. m.textarea, cmd = m.textarea.Update(msg)
  126. return m, cmd
  127. }
  128. func (m *editorCmp) View() string {
  129. t := theme.CurrentTheme()
  130. // Style the prompt with theme colors
  131. style := lipgloss.NewStyle().
  132. Padding(0, 0, 0, 1).
  133. Bold(true).
  134. Foreground(t.Primary())
  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(editorMaps)...)
  148. return bindings
  149. }
  150. func CreateTextArea(existing *textarea.Model) textarea.Model {
  151. t := theme.CurrentTheme()
  152. bgColor := t.Background()
  153. textColor := t.Text()
  154. textMutedColor := t.TextMuted()
  155. ta := textarea.New()
  156. ta.BlurredStyle.Base = styles.BaseStyle().Background(bgColor).Foreground(textColor)
  157. ta.BlurredStyle.CursorLine = styles.BaseStyle().Background(bgColor)
  158. ta.BlurredStyle.Placeholder = styles.BaseStyle().Background(bgColor).Foreground(textMutedColor)
  159. ta.BlurredStyle.Text = styles.BaseStyle().Background(bgColor).Foreground(textColor)
  160. ta.FocusedStyle.Base = styles.BaseStyle().Background(bgColor).Foreground(textColor)
  161. ta.FocusedStyle.CursorLine = styles.BaseStyle().Background(bgColor)
  162. ta.FocusedStyle.Placeholder = styles.BaseStyle().Background(bgColor).Foreground(textMutedColor)
  163. ta.FocusedStyle.Text = styles.BaseStyle().Background(bgColor).Foreground(textColor)
  164. ta.Prompt = " "
  165. ta.ShowLineNumbers = false
  166. ta.CharLimit = -1
  167. if existing != nil {
  168. ta.SetValue(existing.Value())
  169. ta.SetWidth(existing.Width())
  170. ta.SetHeight(existing.Height())
  171. }
  172. ta.Focus()
  173. return ta
  174. }
  175. func NewEditorCmp(app *app.App) tea.Model {
  176. ta := CreateTextArea(nil)
  177. return &editorCmp{
  178. app: app,
  179. textarea: ta,
  180. }
  181. }