model.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package editor
  2. import (
  3. "strings"
  4. "moose/internal/buffer"
  5. "github.com/gdamore/tcell/v3"
  6. )
  7. type Model struct {
  8. Screen tcell.Screen
  9. Style tcell.Style
  10. Mode Mode
  11. BM buffer.BufferManager
  12. AM ActionManager
  13. ShouldQuit bool
  14. }
  15. func NewModel(screen tcell.Screen, style tcell.Style) Model {
  16. model := Model{
  17. Screen: screen,
  18. Style: style,
  19. Mode: ModeNormal,
  20. BM: buffer.BufferManager{
  21. Buffers: []buffer.Buffer{buffer.NewBuffer()},
  22. CurrentIdx: 0,
  23. CommandBuffer: buffer.NewBuffer(),
  24. },
  25. AM: DefaultActionManager(),
  26. ShouldQuit: false,
  27. }
  28. return model
  29. }
  30. func (m *Model) CurrentActionSet() []Action {
  31. switch m.Mode {
  32. case ModeNormal: return m.AM.Normal
  33. case ModeInsert: return m.AM.Insert
  34. case ModeCommand: return m.AM.Command
  35. }
  36. return nil
  37. }
  38. func (m *Model) Quit() {
  39. m.ShouldQuit = true
  40. }
  41. func (m Model) Render() {
  42. sWidth, sHeight := m.Screen.Size()
  43. maxHeight := sHeight - 2 // TODO: saturating sub?
  44. bLen := len(m.BM.Buffers)
  45. bufWidth := sWidth / bLen
  46. for i, buf := range m.BM.Buffers {
  47. table := strings.SplitAfter(buf.String(), "\n")
  48. for j, line := range table {
  49. m.Screen.PutStrStyled(bufWidth * i, j, line, m.Style)
  50. if j + 1 > maxHeight { break }
  51. }
  52. for _, cur := range buf.CM.Cursors {
  53. line, col := buffer.LineCol(buf.Rope, cur.Offset)
  54. if line + 1 > maxHeight { continue }
  55. m.Screen.SetContent(bufWidth * i + col, line, ' ', nil, m.Style.Reverse(true))
  56. }
  57. }
  58. for col := 0; col < sWidth; col++ {
  59. m.Screen.SetContent(col, maxHeight, ' ', nil, m.Style.Reverse(true))
  60. }
  61. modeStr := m.Mode.String()
  62. m.Screen.PutStrStyled(sWidth - (len(modeStr) + 1), maxHeight, strings.ToUpper(modeStr), m.Style.Reverse(true))
  63. if m.Mode == ModeCommand {
  64. m.Screen.PutStrStyled(0, maxHeight + 1, m.BM.CommandBuffer.String(), m.Style)
  65. }
  66. }
  67. func (m *Model) HandleKeyInput(ev *tcell.EventKey) {
  68. for _, action := range append(m.AM.Common, m.CurrentActionSet()...) {
  69. if action.Binding != "" && strings.ToLower(ev.Name()) == strings.ToLower(action.Binding) {
  70. if action.Command != "" && action.HasArgs == true {
  71. m.Mode = ModeCommand
  72. m.BM.CommandBuffer.Paste(action.Command)
  73. return
  74. }
  75. action.Callback(m, []string{})
  76. return
  77. }
  78. }
  79. if m.Mode == ModeInsert {
  80. if ev.Key() == tcell.KeyRune {
  81. for _, r := range ev.Str() {
  82. m.BM.Current().Insert(r)
  83. }
  84. }
  85. }
  86. if m.Mode == ModeCommand {
  87. if ev.Key() == tcell.KeyRune {
  88. for _, r := range ev.Str() {
  89. m.BM.CommandBuffer.Insert(r)
  90. }
  91. }
  92. }
  93. }