editor.go 3.2 KB

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