1
0

model.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. PaletteBuffer: 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 ModePalette: return m.AM.Palette
  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. if j + 1 > maxHeight { break }
  50. r := []rune(line)
  51. if len(r) + 1 > bufWidth {
  52. r = r[:bufWidth]
  53. }
  54. m.Screen.PutStrStyled(bufWidth*i, j, string(r), m.Style)
  55. }
  56. for _, cur := range buf.CM.Cursors {
  57. line, col := buffer.LineCol(buf.Rope, cur.Offset)
  58. if line + 1 > maxHeight { continue }
  59. if col + 1 > bufWidth { continue }
  60. m.Screen.SetContent(bufWidth * i + col, line, ' ', nil, m.Style.Reverse(true))
  61. }
  62. }
  63. for col := 0; col < sWidth; col++ {
  64. m.Screen.SetContent(col, maxHeight, ' ', nil, m.Style.Reverse(true))
  65. }
  66. modeStr := m.Mode.String()
  67. m.Screen.PutStrStyled(sWidth - (len(modeStr) + 1), maxHeight, strings.ToUpper(modeStr), m.Style.Reverse(true))
  68. if m.Mode == ModePalette {
  69. m.Screen.PutStrStyled(0, maxHeight + 1, m.BM.PaletteBuffer.String(), m.Style)
  70. } else if strings.HasPrefix(m.BM.PaletteBuffer.String(), "moose.error:") {
  71. m.Screen.PutStrStyled(0, maxHeight + 1, string([]rune(m.BM.PaletteBuffer.String())[12:]), m.Style.Foreground(tcell.ColorRed))
  72. }
  73. }
  74. func (m *Model) HandleKeyInput(ev *tcell.EventKey) {
  75. for _, action := range append(m.AM.Common, m.CurrentActionSet()...) {
  76. if action.Binding != "" && strings.ToLower(ev.Name()) == strings.ToLower(action.Binding) {
  77. if action.Command != "" && action.NeedsArgs == true {
  78. m.Mode = ModePalette
  79. m.BM.PaletteBuffer.Clear()
  80. m.BM.PaletteBuffer.Paste("/" + action.Command)
  81. return
  82. }
  83. action.Callback(m, []string{})
  84. return
  85. }
  86. }
  87. if m.Mode == ModeInsert {
  88. if ev.Key() == tcell.KeyRune {
  89. for _, r := range ev.Str() {
  90. m.BM.Current().Insert(r)
  91. }
  92. }
  93. }
  94. if m.Mode == ModePalette {
  95. if ev.Key() == tcell.KeyRune {
  96. for _, r := range ev.Str() {
  97. m.BM.PaletteBuffer.Insert(r)
  98. }
  99. }
  100. }
  101. }