model.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package editor
  2. import (
  3. "moose/internal/buffer"
  4. )
  5. type Model struct {
  6. Buffer buffer.Buffer
  7. CommandBuffer buffer.Buffer
  8. Actions []Action
  9. Width int
  10. Height int
  11. ShouldQuit bool
  12. }
  13. func NewModel() Model {
  14. initialBuf := buffer.Buffer{
  15. Rope: nil,
  16. CM: buffer.CursorManager{
  17. Cursors: []buffer.Cursor{{Offset: 0, Goal: 0}},
  18. PrimaryIdx: 0,
  19. },
  20. }
  21. //vp := viewport.New()
  22. //vp.MouseWheelEnabled = false
  23. model := Model{
  24. Buffer: initialBuf,
  25. Actions: DefaultActions(),
  26. ShouldQuit: false,
  27. }
  28. return model
  29. }
  30. func (m *Model) Quit() {
  31. m.ShouldQuit = true
  32. }
  33. /*
  34. func (m Model) Init() tea.Cmd {
  35. return nil
  36. }
  37. func (m Model) View() tea.View {
  38. v := tea.NewView(m.Viewport.View())
  39. v.AltScreen = true
  40. v.MouseMode = tea.MouseModeCellMotion
  41. return v
  42. }
  43. func (m Model) renderedContent() string {
  44. content := []byte(m.Buffer.String())
  45. cursorMap := make(map[int]bool)
  46. for _, cur := range m.Buffer.CM.Cursors {
  47. offset := cur.Offset
  48. if offset < 0 {
  49. offset = 0
  50. }
  51. if offset > len(content) {
  52. offset = len(content)
  53. }
  54. cursorMap[offset] = true
  55. }
  56. var out strings.Builder
  57. out.Grow(len(content) + len(cursorMap))
  58. for i := 0; i < len(content); {
  59. r, size := utf8.DecodeRune(content[i:])
  60. if r == utf8.RuneError && size == 1 {
  61. if cursorMap[i] {
  62. out.WriteRune('█')
  63. i++
  64. continue
  65. }
  66. out.WriteByte(content[i])
  67. i++
  68. continue
  69. }
  70. if cursorMap[i] {
  71. out.WriteRune('█')
  72. if r == '\n' {
  73. out.WriteRune('\n')
  74. }
  75. i += size
  76. continue
  77. }
  78. out.WriteRune(r)
  79. i += size
  80. }
  81. if cursorMap[len(content)] {
  82. out.WriteRune('█')
  83. }
  84. return out.String()
  85. }
  86. */