Switch buffer contents to string slice

This commit is contained in:
2026-03-19 11:53:01 +02:00
parent 1b5ce6b7f8
commit 2bf960b085
6 changed files with 514 additions and 541 deletions
+113 -347
View File
@@ -1,13 +1,14 @@
package main
import (
"github.com/gdamore/tcell/v2"
"log"
"slices"
"strconv"
"strings"
"time"
"unicode"
"github.com/gdamore/tcell/v2"
)
type CursorMode uint8
@@ -98,6 +99,9 @@ func (window *Window) Draw() {
// Clear screen
window.screen.Clear()
// Sync buffer offset
window.SyncBufferOffset()
// Draw top menu
if window.ShowTopMenu {
drawTopMenu(window)
@@ -161,25 +165,24 @@ func (window *Window) handleKeyInput(ev *tcell.EventKey) {
if ev.Modifiers()&tcell.ModCtrl != 0 {
// Move cursor to start of word
// Set variable to one character right of current position
endOfWord := pos + 1
if endOfWord >= len(window.CurrentBuffer.Contents) {
endOfWord = len(window.CurrentBuffer.Contents)
}
window.CurrentBuffer.MoveRight(1)
// Skip all spaces
for endOfWord < len(window.CurrentBuffer.Contents) && unicode.IsSpace(rune(window.CurrentBuffer.Contents[endOfWord])) {
endOfWord++
for unicode.IsSpace(window.CurrentBuffer.GetCharAtPosition(window.CurrentBuffer.CursorPos)) {
if !window.CurrentBuffer.MoveRight(1) {
break
}
}
// Find end of word
for endOfWord < len(window.CurrentBuffer.Contents) && !unicode.IsSpace(rune(window.CurrentBuffer.Contents[endOfWord])) {
endOfWord++
for !unicode.IsSpace(window.CurrentBuffer.GetCharAtPosition(window.CurrentBuffer.CursorPos)) {
if !window.CurrentBuffer.MoveRight(1) {
break
}
}
window.SetCursorPos(endOfWord)
} else {
// Move cursor one character backwards
window.SetCursorPos(window.CurrentBuffer.CursorPos + 1)
// Move cursor one character forwards
window.CurrentBuffer.MoveRight(1)
}
// Add to selection
@@ -187,7 +190,7 @@ func (window *Window) handleKeyInput(ev *tcell.EventKey) {
if window.CurrentBuffer.Selection == nil {
// Cancel cursor movement when creating selection without holding ctrl
if ev.Modifiers()&tcell.ModCtrl == 0 {
window.SetCursorPos(pos)
window.CurrentBuffer.CursorPos = pos
}
window.CurrentBuffer.Selection = &Selection{
@@ -197,10 +200,6 @@ func (window *Window) handleKeyInput(ev *tcell.EventKey) {
} else {
window.CurrentBuffer.Selection.selectionEnd = window.CurrentBuffer.CursorPos
}
// Prevent selecting dummy character at the end of the buffer
if window.CurrentBuffer.Selection.selectionEnd >= len(window.CurrentBuffer.Contents) {
window.CurrentBuffer.Selection.selectionEnd = len(window.CurrentBuffer.Contents) - 1
}
} else if window.CurrentBuffer.Selection != nil {
// Unset selection
window.CurrentBuffer.Selection = nil
@@ -214,28 +213,33 @@ func (window *Window) handleKeyInput(ev *tcell.EventKey) {
if ev.Modifiers()&tcell.ModCtrl != 0 {
// Move cursor to start of word
// Set variable to one character left of current position
startOfWord := pos - 1
if startOfWord < 0 {
startOfWord = 0
}
window.CurrentBuffer.MoveLeft(1)
// Skip all spaces
for startOfWord >= 0 && len(window.CurrentBuffer.Contents) != 0 && unicode.IsSpace(rune(window.CurrentBuffer.Contents[startOfWord])) {
startOfWord--
for unicode.IsSpace(window.CurrentBuffer.GetCharAtPosition(window.CurrentBuffer.CursorPos)) {
if !window.CurrentBuffer.MoveLeft(1) {
break
}
}
// Find start of word
for startOfWord >= 0 && len(window.CurrentBuffer.Contents) != 0 && !unicode.IsSpace(rune(window.CurrentBuffer.Contents[startOfWord])) {
startOfWord--
// Find end of word
for {
char := window.CurrentBuffer.GetCharAtPosition(window.CurrentBuffer.CursorPos)
if char == 0 || unicode.IsSpace(char) {
break
}
if !window.CurrentBuffer.MoveLeft(1) {
break
}
}
// Move one character to the right
startOfWord++
window.SetCursorPos(startOfWord)
// Move one character to the right if not selecting
if window.CurrentBuffer.CursorPos.X != 0 {
window.CurrentBuffer.MoveRight(1)
}
} else {
// Move cursor one character backwards
window.SetCursorPos(window.CurrentBuffer.CursorPos - 1)
window.CurrentBuffer.MoveLeft(1)
}
// Add to selection
@@ -243,7 +247,7 @@ func (window *Window) handleKeyInput(ev *tcell.EventKey) {
if window.CurrentBuffer.Selection == nil {
// Cancel cursor movement when creating selection without holding ctrl
if ev.Modifiers()&tcell.ModCtrl == 0 {
window.SetCursorPos(pos)
window.CurrentBuffer.CursorPos = pos
}
window.CurrentBuffer.Selection = &Selection{
@@ -267,11 +271,11 @@ func (window *Window) handleKeyInput(ev *tcell.EventKey) {
if ev.Modifiers()&tcell.ModCtrl != 0 {
// Move cursor to top of buffer
window.SetCursorPos(0)
window.CurrentBuffer.CursorPos.X = 0
window.CurrentBuffer.CursorPos.Y = 0
} else {
// Move cursor one line up
x, y := window.GetCursorPos2D()
window.SetCursorPos2D(x, y-1)
window.CurrentBuffer.MoveUp(1)
}
// Add to selection
@@ -318,11 +322,11 @@ func (window *Window) handleKeyInput(ev *tcell.EventKey) {
if ev.Modifiers()&tcell.ModCtrl != 0 {
// Move cursor to bottom of buffer
window.SetCursorPos(len(window.CurrentBuffer.Contents))
window.CurrentBuffer.CursorPos.Y = len(window.CurrentBuffer.Contents) - 1
window.CurrentBuffer.CursorPos.X = len(window.CurrentBuffer.Contents[window.CurrentBuffer.CursorPos.Y])
} else {
// Move cursor one line down
x, y := window.GetCursorPos2D()
window.SetCursorPos2D(x, y+1)
window.CurrentBuffer.MoveDown(1)
}
// Add to selection
@@ -337,9 +341,9 @@ func (window *Window) handleKeyInput(ev *tcell.EventKey) {
window.CurrentBuffer.Selection.selectionEnd = window.CurrentBuffer.CursorPos
}
// Prevent selecting dummy character at the end of the buffer
if window.CurrentBuffer.Selection.selectionEnd >= len(window.CurrentBuffer.Contents) {
window.CurrentBuffer.Selection.selectionEnd = len(window.CurrentBuffer.Contents) - 1
}
//if window.CurrentBuffer.Selection.selectionEnd >= len(window.CurrentBuffer.Contents) {
// window.CurrentBuffer.Selection.selectionEnd = len(window.CurrentBuffer.Contents) - 1
//}
} else if window.CurrentBuffer.Selection != nil {
// Unset selection
window.CurrentBuffer.Selection = nil
@@ -389,25 +393,12 @@ func (window *Window) handleKeyInput(ev *tcell.EventKey) {
}
// Typing
if ev.Key() == tcell.KeyBackspace2 {
if ev.Key() == tcell.KeyBackspace || ev.Key() == tcell.KeyBackspace2 {
if window.CursorMode == CursorModeBuffer {
str := window.CurrentBuffer.Contents
index := window.CurrentBuffer.CursorPos
if window.CurrentBuffer.Selection != nil {
edge1, edge2 := window.CurrentBuffer.GetSelectionEdges()
if edge2 == len(window.CurrentBuffer.Contents) {
edge2 = len(window.CurrentBuffer.Contents) - 1
}
str = str[:edge1] + str[edge2+1:]
window.CurrentBuffer.Contents = str
window.SetCursorPos(edge1)
window.CurrentBuffer.Selection = nil
} else if index != 0 {
str = str[:index-1] + str[index:]
window.CurrentBuffer.Contents = str
window.SetCursorPos(window.CurrentBuffer.CursorPos - 1)
window.CurrentBuffer.CutText(window)
} else {
window.CurrentBuffer.Delete(1)
}
} else if window.CursorMode == CursorModeInputBar {
str := currentInputRequest.input
@@ -421,57 +412,21 @@ func (window *Window) handleKeyInput(ev *tcell.EventKey) {
}
} else if ev.Key() == tcell.KeyTab {
if window.CursorMode == CursorModeBuffer {
str := window.CurrentBuffer.Contents
// Remove selected text
if window.CurrentBuffer.Selection != nil {
edge1, edge2 := window.CurrentBuffer.GetSelectionEdges()
if edge2 == len(window.CurrentBuffer.Contents) {
edge2 = len(window.CurrentBuffer.Contents) - 1
}
str = str[:edge1] + str[edge2+1:]
window.CurrentBuffer.Contents = str
window.SetCursorPos(edge1)
window.CurrentBuffer.Selection = nil
window.CurrentBuffer.CutText(window)
}
index := window.CurrentBuffer.CursorPos
if index == len(str) {
str += "\t"
} else {
str = str[:index] + "\t" + str[index:]
}
window.CurrentBuffer.Contents = str
window.SetCursorPos(window.CurrentBuffer.CursorPos + 1)
window.CurrentBuffer.WriteRune('\t')
}
} else if ev.Key() == tcell.KeyEnter {
if window.CursorMode == CursorModeBuffer {
str := window.CurrentBuffer.Contents
// Remove selected text
if window.CurrentBuffer.Selection != nil {
edge1, edge2 := window.CurrentBuffer.GetSelectionEdges()
if edge2 == len(window.CurrentBuffer.Contents) {
edge2 = len(window.CurrentBuffer.Contents) - 1
}
str = str[:edge1] + str[edge2+1:]
window.CurrentBuffer.Contents = str
window.SetCursorPos(edge1)
window.CurrentBuffer.Selection = nil
window.CurrentBuffer.CutText(window)
}
index := window.CurrentBuffer.CursorPos
if index == len(str) {
str += "\n"
} else {
str = str[:index] + "\n" + str[index:]
}
window.CurrentBuffer.Contents = str
window.SetCursorPos(window.CurrentBuffer.CursorPos + 1)
window.CurrentBuffer.WriteRune('\n')
} else if window.CursorMode == CursorModeInputBar {
if currentInputRequest.input == "" && slices.Index(inputHistory, currentInputRequest.input) == -1 {
inputHistory = append(inputHistory, currentInputRequest.input)
@@ -485,30 +440,12 @@ func (window *Window) handleKeyInput(ev *tcell.EventKey) {
}
} else if ev.Key() == tcell.KeyRune {
if window.CursorMode == CursorModeBuffer {
str := window.CurrentBuffer.Contents
// Remove selected text
if window.CurrentBuffer.Selection != nil {
edge1, edge2 := window.CurrentBuffer.GetSelectionEdges()
if edge2 == len(window.CurrentBuffer.Contents) {
edge2 = len(window.CurrentBuffer.Contents) - 1
}
str = str[:edge1] + str[edge2+1:]
window.CurrentBuffer.Contents = str
window.SetCursorPos(edge1)
window.CurrentBuffer.Selection = nil
window.CurrentBuffer.CutText(window)
}
index := window.CurrentBuffer.CursorPos
if index == len(str) {
str += string(ev.Rune())
} else {
str = str[:index] + string(ev.Rune()) + str[index:]
}
window.CurrentBuffer.Contents = str
window.SetCursorPos(window.CurrentBuffer.CursorPos + 1)
window.CurrentBuffer.WriteRune(ev.Rune())
} else if window.CursorMode == CursorModeInputBar {
str := currentInputRequest.input
index := currentInputRequest.cursorPos
@@ -535,14 +472,44 @@ func (window *Window) handleMouseInput(ev *tcell.EventMouse) {
// Ensure click was in buffer area
x1, y1, x2, y2 := window.GetTextAreaDimensions()
if mouseX >= x1 && mouseY >= y1 && mouseX <= x2 && mouseY <= y2 {
currentX, currentY := window.GetCursorPos2D()
bufferMouseX, bufferMouseY := window.AbsolutePosToCursorPos2D(mouseX, mouseY)
currentPos := window.CurrentBuffer.CursorPos
mouseBufferPos := Position{mouseX + window.CurrentBuffer.Offset.X - x1, mouseY + window.CurrentBuffer.Offset.Y - y1}
// Keep mouse Y in bounds
if mouseBufferPos.Y >= len(window.CurrentBuffer.Contents) {
mouseBufferPos.Y = len(window.CurrentBuffer.Contents) - 1
}
// Offset mouse X for each tab character in line
posInLine := make([]int, 0)
for i, r := range window.CurrentBuffer.Contents[mouseBufferPos.Y] + " " {
if r == '\t' {
for j := 0; j < Config.TabIndentation; j++ {
posInLine = append(posInLine, i)
}
} else {
posInLine = append(posInLine, i)
}
}
if len(posInLine) == 0 {
mouseBufferPos.X = 0
} else if mouseBufferPos.X >= len(posInLine) {
mouseBufferPos.X = posInLine[len(posInLine)-1]
} else {
mouseBufferPos.X = posInLine[mouseBufferPos.X]
}
// Keep mouse X in bounds
if mouseBufferPos.X > len(window.CurrentBuffer.Contents[mouseBufferPos.Y]) {
mouseBufferPos.X = len(window.CurrentBuffer.Contents[mouseBufferPos.Y])
}
if mouseHeld {
// Add to selection
if window.CurrentBuffer.Selection == nil {
window.CurrentBuffer.Selection = &Selection{
selectionStart: window.CurrentBuffer.CursorPos,
selectionEnd: window.CursorPos2DToCursorPos(bufferMouseX, bufferMouseY),
selectionEnd: mouseBufferPos,
}
// Set last click time
@@ -550,22 +517,19 @@ func (window *Window) handleMouseInput(ev *tcell.EventMouse) {
return
} else {
window.CurrentBuffer.Selection.selectionEnd = window.CursorPos2DToCursorPos(bufferMouseX, bufferMouseY)
window.CurrentBuffer.Selection.selectionEnd = mouseBufferPos
}
// Prevent selecting dummy character at the end of the buffer
if window.CurrentBuffer.Selection.selectionEnd >= len(window.CurrentBuffer.Contents) {
window.CurrentBuffer.Selection.selectionEnd = len(window.CurrentBuffer.Contents) - 1
}
} else if currentX == bufferMouseX && currentY == bufferMouseY && window.CurrentBuffer.CursorPos < len(window.CurrentBuffer.Contents) && time.Since(lastClickTime).Milliseconds() < 300 {
} else if currentPos == mouseBufferPos && time.Since(lastClickTime).Milliseconds() < 300 {
selectedText := window.CurrentBuffer.GetSelectedText()
if window.CurrentBuffer.Selection == nil || strings.HasSuffix(selectedText, "\n") {
// Select word
startOfWord := window.CurrentBuffer.CursorPos
endOfWord := window.CurrentBuffer.CursorPos
cursorPos := window.CurrentBuffer.CursorPos
startOfWord := window.CurrentBuffer.CursorPos.X
endOfWord := window.CurrentBuffer.CursorPos.X
// Find end of word
for i := window.CurrentBuffer.CursorPos + 1; i < len(window.CurrentBuffer.Contents); i++ {
currentRune := rune(window.CurrentBuffer.Contents[i])
for i := cursorPos.X + 1; i < len(window.CurrentBuffer.Contents[cursorPos.Y]); i++ {
currentRune := rune(window.CurrentBuffer.Contents[cursorPos.Y][i])
if unicode.IsLetter(currentRune) || unicode.IsDigit(currentRune) || currentRune == '_' {
endOfWord++
} else {
@@ -574,8 +538,8 @@ func (window *Window) handleMouseInput(ev *tcell.EventMouse) {
}
// Find start of word
for i := window.CurrentBuffer.CursorPos - 1; i >= 0; i-- {
currentRune := rune(window.CurrentBuffer.Contents[i])
for i := cursorPos.X - 1; i >= 0; i-- {
currentRune := rune(window.CurrentBuffer.Contents[cursorPos.Y][i])
if unicode.IsLetter(currentRune) || unicode.IsDigit(currentRune) || currentRune == '_' {
startOfWord--
} else {
@@ -585,38 +549,17 @@ func (window *Window) handleMouseInput(ev *tcell.EventMouse) {
// Add to selection
window.CurrentBuffer.Selection = &Selection{
selectionStart: startOfWord,
selectionEnd: endOfWord,
selectionStart: Position{startOfWord, cursorPos.Y},
selectionEnd: Position{endOfWord, cursorPos.Y},
}
} else {
// Select line
startOfLine := window.CurrentBuffer.CursorPos
endOfLine := window.CurrentBuffer.CursorPos
// Find end of line
for i := window.CurrentBuffer.CursorPos + 1; i < len(window.CurrentBuffer.Contents); i++ {
currentLetter := window.CurrentBuffer.Contents[i]
endOfLine++
if currentLetter == '\n' {
break
}
}
// Find start of line
for i := window.CurrentBuffer.CursorPos - 1; i >= 0; i-- {
currentLetter := window.CurrentBuffer.Contents[i]
if currentLetter != '\n' {
startOfLine--
} else {
break
}
}
cursorPos := window.CurrentBuffer.CursorPos
// Add to selection
window.CurrentBuffer.Selection = &Selection{
selectionStart: startOfLine,
selectionEnd: endOfLine,
selectionStart: Position{0, cursorPos.Y},
selectionEnd: Position{len(window.CurrentBuffer.Contents[cursorPos.Y]), cursorPos.Y},
}
}
@@ -631,7 +574,7 @@ func (window *Window) handleMouseInput(ev *tcell.EventMouse) {
}
}
// Move cursor
window.SetCursorPos2D(bufferMouseX, bufferMouseY)
window.CurrentBuffer.CursorPos = mouseBufferPos
// Set last click time
lastClick = time.Now().UnixMilli()
@@ -667,196 +610,19 @@ func (window *Window) GetTextAreaDimensions() (int, int, int, int) {
return x1, y1, x2 - 1, y2 - 2
}
func (window *Window) CursorPos2DToCursorPos(x, y int) int {
// Ensure x and y are positive
x = max(x, 0)
y = max(y, 0)
// Set cursor position to 0 buffer is empty
if len(window.CurrentBuffer.Contents) == 0 {
return 0
}
// Create line slice from buffer contents
lines := make([]struct {
charIndex int
str string
}, 0)
var str string
for i, char := range window.CurrentBuffer.Contents {
str += string(char)
if char == '\n' || i == len(window.CurrentBuffer.Contents)-1 {
lines = append(lines, struct {
charIndex int
str string
}{charIndex: i - len(str) + 1, str: str})
str = ""
}
}
// Append extra character or line
if window.CurrentBuffer.Contents[len(window.CurrentBuffer.Contents)-1] == '\n' {
lines = append(lines, struct {
charIndex int
str string
}{charIndex: len(window.CurrentBuffer.Contents), str: " "})
} else {
lines[len(lines)-1].str += " "
}
// Limit x and y
y = min(y, len(lines)-1)
x = min(x, len(lines[y].str)-1)
return lines[y].charIndex + x
}
func (window *Window) AbsolutePosToCursorPos2D(x, y int) (int, int) {
x1, y1, _, _ := window.GetTextAreaDimensions()
x -= x1
y -= y1
x += window.CurrentBuffer.OffsetX
y += window.CurrentBuffer.OffsetY
if x < 0 {
x = 0
}
if y < 0 {
y = 0
}
split := strings.SplitAfter(window.CurrentBuffer.Contents+" ", "\n")
if y >= len(split) {
y = len(split) - 1
}
line := split[y]
posInLine := make([]int, 0)
for i, char := range []rune(line) {
if char == '\t' {
for j := 0; j < Config.TabIndentation; j++ {
posInLine = append(posInLine, i)
}
} else {
posInLine = append(posInLine, i)
}
}
if len(posInLine) == 0 {
x = 0
} else if x >= len(posInLine) {
x = posInLine[len(posInLine)-1]
} else {
x = posInLine[x]
}
return x, y
}
func (window *Window) GetAbsoluteCursorPos() (int, int) {
cursorX, cursorY := window.GetCursorPos2D()
x1, y1, _, _ := window.GetTextAreaDimensions()
cursorX += x1
cursorY += y1
return cursorX, cursorY
}
func (window *Window) GetCursorPos2D() (int, int) {
cursorX := 0
cursorY := 0
for i := 0; i < window.CurrentBuffer.CursorPos; i++ {
char := window.CurrentBuffer.Contents[i]
if char == '\n' {
cursorY++
cursorX = 0
} else {
cursorX++
}
}
return cursorX, cursorY
}
func (window *Window) SetCursorPos(position int) {
window.CurrentBuffer.CursorPos = position
if window.CurrentBuffer.CursorPos < 0 {
window.CurrentBuffer.CursorPos = 0
}
if window.CurrentBuffer.CursorPos > len(window.CurrentBuffer.Contents) {
window.CurrentBuffer.CursorPos = len(window.CurrentBuffer.Contents)
}
window.SyncBufferOffset()
}
func (window *Window) SetCursorPos2D(x, y int) {
// Ensure x and y are positive
x = max(x, 0)
y = max(y, 0)
// Set cursor position to 0 buffer is empty
if len(window.CurrentBuffer.Contents) == 0 {
window.SetCursorPos(0)
return
}
// Create line slice from buffer contents
lines := make([]struct {
charIndex int
str string
}, 0)
var str string
for i, char := range window.CurrentBuffer.Contents {
str += string(char)
if char == '\n' || i == len(window.CurrentBuffer.Contents)-1 {
lines = append(lines, struct {
charIndex int
str string
}{charIndex: i - len(str) + 1, str: str})
str = ""
}
}
// Append extra character or line
if window.CurrentBuffer.Contents[len(window.CurrentBuffer.Contents)-1] == '\n' {
lines = append(lines, struct {
charIndex int
str string
}{charIndex: len(window.CurrentBuffer.Contents), str: " "})
} else {
lines[len(lines)-1].str += " "
}
// Limit x and y
y = min(y, len(lines)-1)
x = min(x, len(lines[y].str)-1)
window.SetCursorPos(lines[y].charIndex + x)
}
func (window *Window) SyncBufferOffset() {
x, y := window.GetCursorPos2D()
cursorPos := window.CurrentBuffer.CursorPos
bufferX1, bufferY1, bufferX2, bufferY2 := window.GetTextAreaDimensions()
if y < window.CurrentBuffer.OffsetY {
window.CurrentBuffer.OffsetY = y
} else if y > window.CurrentBuffer.OffsetY+(bufferY2-bufferY1) {
window.CurrentBuffer.OffsetY = y - (bufferY2 - bufferY1)
if cursorPos.Y < window.CurrentBuffer.Offset.Y {
window.CurrentBuffer.Offset.Y = cursorPos.Y
} else if cursorPos.Y > window.CurrentBuffer.Offset.Y+(bufferY2-bufferY1) {
window.CurrentBuffer.Offset.Y = cursorPos.Y - (bufferY2 - bufferY1)
}
if x < window.CurrentBuffer.OffsetX {
window.CurrentBuffer.OffsetX = x
} else if x > window.CurrentBuffer.OffsetX+(bufferX2-bufferX1) {
window.CurrentBuffer.OffsetX = x - (bufferX2 - bufferX1)
if cursorPos.X < window.CurrentBuffer.Offset.X {
window.CurrentBuffer.Offset.X = cursorPos.X
} else if cursorPos.X > window.CurrentBuffer.Offset.X+(bufferX2-bufferX1) {
window.CurrentBuffer.Offset.X = cursorPos.X - (bufferX2 - bufferX1)
}
}