model.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. 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 == ModePalette {
  64. m.Screen.PutStrStyled(0, maxHeight + 1, m.BM.PaletteBuffer.String(), m.Style)
  65. } else if strings.HasPrefix(m.BM.PaletteBuffer.String(), "moose.error:") {
  66. m.Screen.PutStrStyled(0, maxHeight + 1, string([]rune(m.BM.PaletteBuffer.String())[12:]), m.Style.Foreground(tcell.ColorRed))
  67. }
  68. }
  69. func (m *Model) HandleKeyInput(ev *tcell.EventKey) {
  70. for _, action := range append(m.AM.Common, m.CurrentActionSet()...) {
  71. if action.Binding != "" && strings.ToLower(ev.Name()) == strings.ToLower(action.Binding) {
  72. if action.Command != "" && action.HasArgs == true {
  73. m.Mode = ModePalette
  74. m.BM.PaletteBuffer.Paste(action.Command)
  75. return
  76. }
  77. action.Callback(m, []string{})
  78. return
  79. }
  80. }
  81. if m.Mode == ModeInsert {
  82. if ev.Key() == tcell.KeyRune {
  83. for _, r := range ev.Str() {
  84. m.BM.Current().Insert(r)
  85. }
  86. }
  87. }
  88. if m.Mode == ModePalette {
  89. if ev.Key() == tcell.KeyRune {
  90. for _, r := range ev.Str() {
  91. m.BM.PaletteBuffer.Insert(r)
  92. }
  93. }
  94. }
  95. }