Bladeren bron

Split some functiosn into their own files

Johan Rong 1 maand geleden
bovenliggende
commit
f482208e1f
3 gewijzigde bestanden met toevoegingen van 100 en 88 verwijderingen
  1. 37 0
      internal/editor/keyinput.go
  2. 0 88
      internal/editor/model.go
  3. 63 0
      internal/editor/render.go

+ 37 - 0
internal/editor/keyinput.go

@@ -0,0 +1,37 @@
+package editor
+
+import (
+	"strings"
+	"github.com/gdamore/tcell/v3"
+)
+
+func (m *Model) HandleKeyInput(ev *tcell.EventKey) {
+	for _, action := range append(m.CurrentActionSet(), m.AM.Common...) {
+		if action.Binding != "" && strings.ToLower(ev.Name()) == strings.ToLower(action.Binding) {
+			if action.Command != "" && action.NeedsArgs == true {
+				m.Mode = ModePalette
+				m.BM.PaletteBuffer.Clear()
+				m.BM.PaletteBuffer.Paste("/" + action.Command)
+				return
+			}
+
+			action.Callback(m, []string{})
+			return
+		}
+	}
+
+	if m.Mode == ModeInsert {
+		if ev.Key() == tcell.KeyRune {
+			for _, r := range ev.Str() {
+				m.BM.Current().Insert(r)
+			}
+		}
+	}
+	if m.Mode == ModePalette {
+		if ev.Key() == tcell.KeyRune {
+			for _, r := range ev.Str() {
+				m.BM.PaletteBuffer.Insert(r)
+			}
+		}
+	}
+}

+ 0 - 88
internal/editor/model.go

@@ -1,7 +1,6 @@
 package editor
 
 import (
-	"strings"
 	"moose/internal/buffer"
 	"github.com/gdamore/tcell/v3"
 )
@@ -44,91 +43,4 @@ func (m *Model) CurrentActionSet() []Action {
 
 func (m *Model) Quit() {
 	m.ShouldQuit = true
-}
-
-func (m Model) Render() {
-	sWidth, sHeight := m.Screen.Size()
-
-	maxHeight := sHeight - 2 // TODO: saturating sub?
-
-	bLen := len(m.BM.Buffers)
-	bufWidth := sWidth / bLen
-
-	for i, buf := range m.BM.Buffers {
-		table := strings.SplitAfter(buf.String(), "\n")
-		for j, line := range table {
-			if j + 1 > maxHeight { break }
-
-			r := []rune(line)
-			if len(r) + 1 > bufWidth {
-				r = r[:bufWidth]
-			}
-
-			m.Screen.PutStrStyled(bufWidth*i, j, string(r), m.Style)
-		}
-
-		for _, cur := range buf.CM.Cursors {
-			line, col := buffer.LineCol(buf.Rope, cur.Offset)
-			if line + 1 > maxHeight { continue }
-			if col + 1 > bufWidth { continue }
-
-			if i == m.BM.CurrentIdx && m.Mode != ModePalette {
-				m.Screen.SetContent(bufWidth * i + col, line, ' ', nil, m.Style.Reverse(true))
-			} else {
-				m.Screen.SetContent(bufWidth * i + col, line, ' ', nil, m.Style.Reverse(true).Foreground(tcell.ColorGray))				
-			}
-		}
-	}
-
-	for col := 0; col < sWidth; col++ {
-		m.Screen.SetContent(col, maxHeight, ' ', nil, m.Style.Reverse(true))
-	}
-
-	modeStr := m.Mode.String()
-	m.Screen.PutStrStyled(sWidth - (len(modeStr) + 1), maxHeight, strings.ToUpper(modeStr), m.Style.Reverse(true))
-
-	if m.Mode == ModePalette {
-		m.Screen.PutStrStyled(0, maxHeight + 1, m.BM.PaletteBuffer.String(), m.Style)
-
-		for _, cur := range m.BM.PaletteBuffer.CM.Cursors {
-			line, col := buffer.LineCol(m.BM.PaletteBuffer.Rope, cur.Offset)
-			if line + maxHeight != maxHeight { continue }
-			if col + 1 > sWidth { continue }
-
-			m.Screen.SetContent(col, maxHeight + 1, ' ', nil, m.Style.Reverse(true))
-		}
-	} else if strings.HasPrefix(m.BM.PaletteBuffer.String(), "moose.error:") {
-		m.Screen.PutStrStyled(0, maxHeight + 1, string([]rune(m.BM.PaletteBuffer.String())[12:]), m.Style.Foreground(tcell.ColorRed))
-	}
-}
-
-func (m *Model) HandleKeyInput(ev *tcell.EventKey) {
-	for _, action := range append(m.CurrentActionSet(), m.AM.Common...) {
-		if action.Binding != "" && strings.ToLower(ev.Name()) == strings.ToLower(action.Binding) {
-			if action.Command != "" && action.NeedsArgs == true {
-				m.Mode = ModePalette
-				m.BM.PaletteBuffer.Clear()
-				m.BM.PaletteBuffer.Paste("/" + action.Command)
-				return
-			}
-
-			action.Callback(m, []string{})
-			return
-		}
-	}
-
-	if m.Mode == ModeInsert {
-		if ev.Key() == tcell.KeyRune {
-			for _, r := range ev.Str() {
-				m.BM.Current().Insert(r)
-			}
-		}
-	}
-	if m.Mode == ModePalette {
-		if ev.Key() == tcell.KeyRune {
-			for _, r := range ev.Str() {
-				m.BM.PaletteBuffer.Insert(r)
-			}
-		}
-	}
 }

+ 63 - 0
internal/editor/render.go

@@ -0,0 +1,63 @@
+package editor
+
+import (
+	"strings"
+	"moose/internal/buffer"
+	"github.com/gdamore/tcell/v3"
+)
+
+func (m Model) Render() {
+	sWidth, sHeight := m.Screen.Size()
+
+	maxHeight := sHeight - 2 // TODO: saturating sub?
+
+	bLen := len(m.BM.Buffers)
+	bufWidth := sWidth / bLen
+
+	for i, buf := range m.BM.Buffers {
+		table := strings.SplitAfter(buf.String(), "\n")
+		for j, line := range table {
+			if j + 1 > maxHeight { break }
+
+			r := []rune(line)
+			if len(r) + 1 > bufWidth {
+				r = r[:bufWidth]
+			}
+
+			m.Screen.PutStrStyled(bufWidth*i, j, string(r), m.Style)
+		}
+
+		for _, cur := range buf.CM.Cursors {
+			line, col := buffer.LineCol(buf.Rope, cur.Offset)
+			if line + 1 > maxHeight { continue }
+			if col + 1 > bufWidth { continue }
+
+			if i == m.BM.CurrentIdx && m.Mode != ModePalette {
+				m.Screen.SetContent(bufWidth * i + col, line, ' ', nil, m.Style.Reverse(true))
+			} else {
+				m.Screen.SetContent(bufWidth * i + col, line, ' ', nil, m.Style.Reverse(true).Foreground(tcell.ColorGray))				
+			}
+		}
+	}
+
+	for col := 0; col < sWidth; col++ {
+		m.Screen.SetContent(col, maxHeight, ' ', nil, m.Style.Reverse(true))
+	}
+
+	modeStr := m.Mode.String()
+	m.Screen.PutStrStyled(sWidth - (len(modeStr) + 1), maxHeight, strings.ToUpper(modeStr), m.Style.Reverse(true))
+
+	if m.Mode == ModePalette {
+		m.Screen.PutStrStyled(0, maxHeight + 1, m.BM.PaletteBuffer.String(), m.Style)
+
+		for _, cur := range m.BM.PaletteBuffer.CM.Cursors {
+			line, col := buffer.LineCol(m.BM.PaletteBuffer.Rope, cur.Offset)
+			if line + maxHeight != maxHeight { continue }
+			if col + 1 > sWidth { continue }
+
+			m.Screen.SetContent(col, maxHeight + 1, ' ', nil, m.Style.Reverse(true))
+		}
+	} else if strings.HasPrefix(m.BM.PaletteBuffer.String(), "moose.error:") {
+		m.Screen.PutStrStyled(0, maxHeight + 1, string([]rune(m.BM.PaletteBuffer.String())[12:]), m.Style.Foreground(tcell.ColorRed))
+	}
+}