editor.go 3.8 KB

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