update.go 885 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package editor
  2. import (
  3. //"fmt"
  4. "charm.land/bubbles/v2/key"
  5. tea "charm.land/bubbletea/v2"
  6. )
  7. func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  8. switch msg := msg.(type) {
  9. case tea.WindowSizeMsg:
  10. m.Width = msg.Width
  11. m.Height = msg.Height
  12. m.Viewport.SetWidth(msg.Width)
  13. h := msg.Height - 2
  14. if h < 0 {
  15. h = 0
  16. }
  17. m.Viewport.SetHeight(h)
  18. return m, nil
  19. case tea.MouseWheelMsg:
  20. return m, nil
  21. case tea.KeyPressMsg:
  22. for i := range m.Actions {
  23. action := m.Actions[i]
  24. if key.Matches(msg, action.Binding) {
  25. //fmt.Println(msg, action.Binding)
  26. cmd := action.Callback(&m, []string{})
  27. m.Viewport.SetContent(m.renderedContent())
  28. return m, cmd
  29. }
  30. }
  31. key := msg.Key()
  32. if key.Text != "" {
  33. for _, r := range key.Text {
  34. m.Buffer.Insert(r)
  35. }
  36. }
  37. m.Viewport.SetContent(m.renderedContent())
  38. return m, nil
  39. }
  40. return m, nil
  41. }