Johan Rong il y a 1 mois
Parent
commit
af563a91da
4 fichiers modifiés avec 96 ajouts et 104 suppressions
  1. 20 0
      internal/buffer/buffer.go
  2. 15 11
      internal/editor/action.go
  3. 55 63
      internal/editor/model.go
  4. 6 30
      main.go

+ 20 - 0
internal/buffer/buffer.go

@@ -6,11 +6,31 @@ import (
 	"github.com/zyedidia/rope"
 )
 
+type BufferManager struct {
+	Buffers       []Buffer
+	CurrentIdx    int
+	CommandBuffer Buffer
+}
+
+func (bm *BufferManager) Current() *Buffer {
+	return &bm.Buffers[bm.CurrentIdx]
+}
+
 type Buffer struct {
 	Rope *rope.Node
 	CM   CursorManager
 }
 
+func NewBuffer() Buffer {
+	return Buffer{
+		Rope: rope.New([]byte{}),
+		CM: CursorManager{
+			Cursors:    []Cursor{{Offset: 0, Goal: 0}},
+			PrimaryIdx: 0,
+		},
+	}
+}
+
 func (buf *Buffer) ensureRope() {
 	if buf.Rope == nil {
 		buf.Rope = rope.New([]byte{})

+ 15 - 11
internal/editor/action.go

@@ -14,7 +14,7 @@ func DefaultActions() []Action {
 			Command: "TODO:",
 			HasArgs: false,
 			Callback: func(m *Model, args []string) {
-				m.Buffer.MoveHoriz(-1)	
+				m.Current().MoveHoriz(-1)	
 			},
 		},
 		Action{
@@ -22,7 +22,7 @@ func DefaultActions() []Action {
 			Command: "TODO:",
 			HasArgs: false,
 			Callback: func(m *Model, args []string) {
-				m.Buffer.MoveHoriz(1)
+				m.Current().MoveHoriz(1)
 			},
 		},
 		Action{
@@ -30,7 +30,7 @@ func DefaultActions() []Action {
 			Command: "TODO:",
 			HasArgs: false,
 			Callback: func(m *Model, args []string) {
-				m.Buffer.MoveVert(-1)
+				m.Current().MoveVert(-1)
 			},
 		},
 		Action{
@@ -38,7 +38,7 @@ func DefaultActions() []Action {
 			Command: "TODO:",
 			HasArgs: false,
 			Callback: func(m *Model, args []string) {
-				m.Buffer.MoveVert(1)
+				m.Current().MoveVert(1)
 			},
 		},
 		Action{
@@ -46,7 +46,7 @@ func DefaultActions() []Action {
 			Command: "TODO:",
 			HasArgs: false,
 			Callback: func(m *Model, args []string) {
-				m.Buffer.AddCursorVert(-1)
+				m.Current().AddCursorVert(-1)
 			},
 		},
 		Action{
@@ -54,7 +54,7 @@ func DefaultActions() []Action {
 			Command: "TODO:",
 			HasArgs: false,
 			Callback: func(m *Model, args []string) {
-				m.Buffer.AddCursorVert(1)
+				m.Current().AddCursorVert(1)
 			},
 		},
 		Action{
@@ -62,7 +62,7 @@ func DefaultActions() []Action {
 			Command: "TODO:",
 			HasArgs: false,
 			Callback: func(m *Model, args []string) {
-				m.Buffer.ClearCursors()
+				m.Current().ClearCursors()
 			},
 		},
 		Action{
@@ -70,7 +70,10 @@ func DefaultActions() []Action {
 			Command: "TODO:",
 			HasArgs: false,
 			Callback: func(m *Model, args []string) {
-				//TODO: implement m.Buffer.
+				//TODO: implement m.Current().
+				for _ = range 4 {
+					m.Current().Insert(' ')
+				}
 			},
 		},
 		Action{
@@ -78,7 +81,8 @@ func DefaultActions() []Action {
 			Command: "TODO:",
 			HasArgs: false,
 			Callback: func(m *Model, args []string) {
-				// TODO: implement m.Buffer.
+				// TODO: implement m.Current().
+				
 			},
 		},
 		Action{
@@ -86,7 +90,7 @@ func DefaultActions() []Action {
 			Command: "TODO:",
 			HasArgs: false,
 			Callback: func(m *Model, args []string) {
-				m.Buffer.Delete()
+				m.Current().Delete()
 			},
 		},
 		Action{
@@ -94,7 +98,7 @@ func DefaultActions() []Action {
 			Command: "TODO:",
 			HasArgs: false,
 			Callback: func(m *Model, args []string) {
-				m.Buffer.Insert('\n')
+				m.Current().Insert('\n')
 			},
 		},
 		Action{

+ 55 - 63
internal/editor/model.go

@@ -1,38 +1,39 @@
 package editor
 
 import (
-	"moose/internal/buffer"
-	"github.com/zyedidia/rope"
-
-	"unicode/utf8"
 	"strings"
+	"moose/internal/buffer"
+	"github.com/gdamore/tcell/v3"
 )
 
 type Model struct {
-	Buffer        buffer.Buffer
-	CommandBuffer buffer.Buffer
+	Screen		  tcell.Screen
+	Style		  tcell.Style
+	Mode		  Mode
+	BM			  buffer.BufferManager
 	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
+type Mode int
+const (
+	ModeNormal Mode = iota
+	ModeInsert
+	ModeCommand
+)
 
+func NewModel(screen tcell.Screen, style tcell.Style) Model {
 	model := Model{
-		Buffer:   initialBuf,
-		Actions: DefaultActions(),
-		ShouldQuit: false,
+		Screen:	       screen,
+		Style:		   style,
+		Mode:		   ModeNormal,
+		BM:            buffer.BufferManager{
+			Buffers:       []buffer.Buffer{buffer.NewBuffer()},
+			CurrentIdx:     0,
+			CommandBuffer: buffer.NewBuffer(),
+		},
+		Actions:       DefaultActions(),
+		ShouldQuit:    false,
 	}
 
 	return model
@@ -42,54 +43,45 @@ 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
+func (m *Model) Current() *buffer.Buffer {
+	if m.Mode == ModeCommand {
+		return &m.BM.CommandBuffer
+	} else {
+		return m.BM.Current()
 	}
+}
 
-	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
-			}
+func (m Model) Render() {
+	len := len(m.BM.Buffers)
+	sWidth, _ := m.Screen.Size()
+	width := sWidth / len
 
-			out.WriteByte(content[i])
-			i++
-			continue
+	for i, buf := range m.BM.Buffers {
+		table := strings.SplitAfter(buf.String(), "\n")
+		for j, line := range table {
+			m.Screen.PutStrStyled(width * i, j, line, m.Style)
 		}
 
-		if cursorMap[i] {
-			out.WriteRune('█')
-			if r == '\n' {
-				out.WriteRune('\n')
-			}
-			i += size
-			continue
+		for _, cur := range buf.CM.Cursors {
+			line, col := buffer.LineCol(buf.Rope, cur.Offset)
+			m.Screen.SetContent(width * i + col, line, ' ', nil, m.Style.Reverse(true))
 		}
-
-		out.WriteRune(r)
-		i += size
 	}
+}
 
-	if cursorMap[len(content)] {
-		out.WriteRune('█')
+func (m *Model) HandleKeyInput(ev *tcell.EventKey) {
+	for i := range m.Actions {
+		action := m.Actions[i]
+
+		if strings.ToLower(ev.Name()) == strings.ToLower(action.Binding) {
+			action.Callback(m, []string{})
+			return
+		}
 	}
 
-	return out.String()
-}
+	if ev.Key() == tcell.KeyRune {
+		for _, r := range ev.Str() {
+			m.Current().Insert(r)
+		}
+	}
+}

+ 6 - 30
main.go

@@ -3,15 +3,11 @@ package main
 import (
 	"fmt"
 	"os"
-	"moose/internal/buffer"
 	"moose/internal/editor"
-	"strings"
 	"github.com/gdamore/tcell/v3"
 )
 
 func main() {
-	m := editor.NewModel()
-
 	s, err := tcell.NewScreen()
 	if err != nil {
 		fmt.Println("moose: error: %v", err)
@@ -22,8 +18,10 @@ func main() {
 		os.Exit(1)
 	}
 
-	defStyle := tcell.StyleDefault.Background(tcell.ColorDefault).Foreground(tcell.ColorDefault)
-	s.SetStyle(defStyle)
+	style := tcell.StyleDefault.Background(tcell.ColorDefault).Foreground(tcell.ColorDefault)
+	s.SetStyle(style)
+
+	m := editor.NewModel(s, style)
 
 	s.EnableMouse()
 	s.Clear()
@@ -46,36 +44,14 @@ func main() {
 
 		ev := <-s.EventQ()
 
-		outer:
 		switch ev := ev.(type) {
 		case *tcell.EventResize:
 			s.Sync()
 		case *tcell.EventKey:
-			for i := range m.Actions {
-				action := m.Actions[i]
-
-				if strings.ToLower(ev.Name()) == strings.ToLower(action.Binding) {
-					action.Callback(&m, []string{})
-					break outer
-				}
-			}
-
-			if ev.Key() == tcell.KeyRune {
-				for _, r := range ev.Str() {
-					m.Buffer.Insert(r)
-				}
-			}
+			m.HandleKeyInput(ev)
 		}
 
-		table := strings.SplitAfter(m.Buffer.String(), "\n")
-		for i, line := range table {
-			s.PutStrStyled(0, i, line, defStyle)
-		}
-
-		for _, cur := range m.Buffer.CM.Cursors {
-			line, col := buffer.LineCol(m.Buffer.Rope, cur.Offset)
-			s.SetContent(col, line, ' ', nil, tcell.StyleDefault.Reverse(true))
-		}
+		m.Render()
 
 		s.Show()
 	}