| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- package editor
- import (
- "moose/internal/buffer"
- "github.com/zyedidia/rope"
- "unicode/utf8"
- "strings"
- )
- type Model struct {
- Buffer buffer.Buffer
- CommandBuffer buffer.Buffer
- Actions []Action
- Width int
- Height int
- ShouldQuit bool
- }
- func NewModel() Model {
- initialBuf := buffer.Buffer{
- Rope: rope.New([]byte{}),
- CM: buffer.CursorManager{
- Cursors: []buffer.Cursor{{Offset: 0, Goal: 0}},
- PrimaryIdx: 0,
- },
- }
- //vp := viewport.New()
- //vp.MouseWheelEnabled = false
- model := Model{
- Buffer: initialBuf,
- Actions: DefaultActions(),
- ShouldQuit: false,
- }
- return model
- }
- func (m *Model) Quit() {
- m.ShouldQuit = true
- }
- func (m Model) ToString() string {
- content := []byte(m.Buffer.String())
- cursorMap := make(map[int]bool)
- for _, cur := range m.Buffer.CM.Cursors {
- offset := cur.Offset
- if offset < 0 {
- offset = 0
- }
- if offset > len(content) {
- offset = len(content)
- }
- cursorMap[offset] = true
- }
- var out strings.Builder
- out.Grow(len(content) + len(cursorMap))
- for i := 0; i < len(content); {
- r, size := utf8.DecodeRune(content[i:])
- if r == utf8.RuneError && size == 1 {
- if cursorMap[i] {
- out.WriteRune('█')
- i++
- continue
- }
- out.WriteByte(content[i])
- i++
- continue
- }
- if cursorMap[i] {
- out.WriteRune('█')
- if r == '\n' {
- out.WriteRune('\n')
- }
- i += size
- continue
- }
- out.WriteRune(r)
- i += size
- }
- if cursorMap[len(content)] {
- out.WriteRune('█')
- }
- return out.String()
- }
|