editor.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package chat
  2. import (
  3. "github.com/charmbracelet/bubbles/key"
  4. "github.com/charmbracelet/bubbles/textarea"
  5. tea "github.com/charmbracelet/bubbletea"
  6. "github.com/charmbracelet/lipgloss"
  7. "github.com/kujtimiihoxha/termai/internal/app"
  8. "github.com/kujtimiihoxha/termai/internal/session"
  9. "github.com/kujtimiihoxha/termai/internal/tui/layout"
  10. "github.com/kujtimiihoxha/termai/internal/tui/styles"
  11. "github.com/kujtimiihoxha/termai/internal/tui/util"
  12. )
  13. type editorCmp struct {
  14. app *app.App
  15. session session.Session
  16. textarea textarea.Model
  17. }
  18. type focusedEditorKeyMaps struct {
  19. Send key.Binding
  20. Blur key.Binding
  21. }
  22. type bluredEditorKeyMaps struct {
  23. Send key.Binding
  24. Focus key.Binding
  25. }
  26. var focusedKeyMaps = focusedEditorKeyMaps{
  27. Send: key.NewBinding(
  28. key.WithKeys("ctrl+s"),
  29. key.WithHelp("ctrl+s", "send message"),
  30. ),
  31. Blur: key.NewBinding(
  32. key.WithKeys("esc"),
  33. key.WithHelp("esc", "focus messages"),
  34. ),
  35. }
  36. var bluredKeyMaps = bluredEditorKeyMaps{
  37. Send: key.NewBinding(
  38. key.WithKeys("ctrl+s", "enter"),
  39. key.WithHelp("ctrl+s/enter", "send message"),
  40. ),
  41. Focus: key.NewBinding(
  42. key.WithKeys("i"),
  43. key.WithHelp("i", "focus editor"),
  44. ),
  45. }
  46. func (m *editorCmp) Init() tea.Cmd {
  47. return textarea.Blink
  48. }
  49. func (m *editorCmp) send() tea.Cmd {
  50. if m.app.CoderAgent.IsSessionBusy(m.session.ID) {
  51. return util.ReportWarn("Agent is working, please wait...")
  52. }
  53. value := m.textarea.Value()
  54. m.textarea.Reset()
  55. m.textarea.Blur()
  56. if value == "" {
  57. return nil
  58. }
  59. return tea.Batch(
  60. util.CmdHandler(SendMsg{
  61. Text: value,
  62. }),
  63. util.CmdHandler(EditorFocusMsg(false)),
  64. )
  65. }
  66. func (m *editorCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  67. var cmd tea.Cmd
  68. switch msg := msg.(type) {
  69. case SessionSelectedMsg:
  70. if msg.ID != m.session.ID {
  71. m.session = msg
  72. }
  73. return m, nil
  74. case tea.KeyMsg:
  75. // if the key does not match any binding, return
  76. if m.textarea.Focused() && key.Matches(msg, focusedKeyMaps.Send) {
  77. return m, m.send()
  78. }
  79. if !m.textarea.Focused() && key.Matches(msg, bluredKeyMaps.Send) {
  80. return m, m.send()
  81. }
  82. if m.textarea.Focused() && key.Matches(msg, focusedKeyMaps.Blur) {
  83. m.textarea.Blur()
  84. return m, util.CmdHandler(EditorFocusMsg(false))
  85. }
  86. if !m.textarea.Focused() && key.Matches(msg, bluredKeyMaps.Focus) {
  87. m.textarea.Focus()
  88. return m, tea.Batch(textarea.Blink, util.CmdHandler(EditorFocusMsg(true)))
  89. }
  90. }
  91. m.textarea, cmd = m.textarea.Update(msg)
  92. return m, cmd
  93. }
  94. func (m *editorCmp) View() string {
  95. style := lipgloss.NewStyle().Padding(0, 0, 0, 1).Bold(true)
  96. return lipgloss.JoinHorizontal(lipgloss.Top, style.Render(">"), m.textarea.View())
  97. }
  98. func (m *editorCmp) SetSize(width, height int) {
  99. m.textarea.SetWidth(width - 3) // account for the prompt and padding right
  100. m.textarea.SetHeight(height)
  101. }
  102. func (m *editorCmp) GetSize() (int, int) {
  103. return m.textarea.Width(), m.textarea.Height()
  104. }
  105. func (m *editorCmp) BindingKeys() []key.Binding {
  106. bindings := layout.KeyMapToSlice(m.textarea.KeyMap)
  107. if m.textarea.Focused() {
  108. bindings = append(bindings, layout.KeyMapToSlice(focusedKeyMaps)...)
  109. } else {
  110. bindings = append(bindings, layout.KeyMapToSlice(bluredKeyMaps)...)
  111. }
  112. return bindings
  113. }
  114. func NewEditorCmp(app *app.App) tea.Model {
  115. ti := textarea.New()
  116. ti.Prompt = " "
  117. ti.ShowLineNumbers = false
  118. ti.BlurredStyle.Base = ti.BlurredStyle.Base.Background(styles.Background)
  119. ti.BlurredStyle.CursorLine = ti.BlurredStyle.CursorLine.Background(styles.Background)
  120. ti.BlurredStyle.Placeholder = ti.BlurredStyle.Placeholder.Background(styles.Background)
  121. ti.BlurredStyle.Text = ti.BlurredStyle.Text.Background(styles.Background)
  122. ti.FocusedStyle.Base = ti.FocusedStyle.Base.Background(styles.Background)
  123. ti.FocusedStyle.CursorLine = ti.FocusedStyle.CursorLine.Background(styles.Background)
  124. ti.FocusedStyle.Placeholder = ti.FocusedStyle.Placeholder.Background(styles.Background)
  125. ti.FocusedStyle.Text = ti.BlurredStyle.Text.Background(styles.Background)
  126. ti.CharLimit = -1
  127. ti.Focus()
  128. return &editorCmp{
  129. app: app,
  130. textarea: ti,
  131. }
  132. }