From b52729646ccd34fc69e2c154c8fa731d44858d7d Mon Sep 17 00:00:00 2001 From: EnumDev Date: Tue, 3 Jun 2025 18:20:07 +0300 Subject: [PATCH] Add basic typing functionality --- src/buffer.go | 2 +- src/line_index.go | 2 +- src/window.go | 63 ++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 59 insertions(+), 8 deletions(-) diff --git a/src/buffer.go b/src/buffer.go index fe35e0d..7f8d155 100644 --- a/src/buffer.go +++ b/src/buffer.go @@ -76,7 +76,7 @@ func CreateBuffer(bufferName string) *Buffer { buffer := Buffer{ Id: LastBufferId + 1, Name: bufferName, - Contents: "\n", + Contents: "", LoadFunc: func(buffer *Buffer) error { return nil }, SaveFunc: func(buffer *Buffer) error { return nil }, } diff --git a/src/line_index.go b/src/line_index.go index 741f1cb..bb5a81d 100644 --- a/src/line_index.go +++ b/src/line_index.go @@ -19,7 +19,7 @@ func drawLineIndex(window *Window) { y = 1 } - for lineIndex := 1; lineIndex <= strings.Count(buffer.Contents, "\n") && lineIndex < sizeY; lineIndex++ { + for lineIndex := 1; lineIndex <= strings.Count(buffer.Contents, "\n")+1 && lineIndex < sizeY; lineIndex++ { drawText(screen, 0, y, 3, y, lineIndexStyle, strconv.Itoa(lineIndex)+". ") y++ } diff --git a/src/window.go b/src/window.go index 273d842..f83a905 100644 --- a/src/window.go +++ b/src/window.go @@ -140,6 +140,51 @@ func (window *Window) Draw() { if ev.Key() == tcell.KeyCtrlC { window.Close() } + + // Typing + if ev.Key() == tcell.KeyBackspace2 { + str := window.textArea.CurrentBuffer.Contents + index := window.textArea.CursorPos + + if index != 0 { + str = str[:index-1] + str[index:] + window.textArea.CursorPos-- + window.textArea.CurrentBuffer.Contents = str + } + } else if ev.Key() == tcell.KeyTab { + str := window.textArea.CurrentBuffer.Contents + index := window.textArea.CursorPos + + if index == len(str) { + str += "\t" + } else { + str = str[:index] + "\t" + str[index:] + } + window.textArea.CursorPos++ + window.textArea.CurrentBuffer.Contents = str + } else if ev.Key() == tcell.KeyEnter { + str := window.textArea.CurrentBuffer.Contents + index := window.textArea.CursorPos + + if index == len(str) { + str += "\n" + } else { + str = str[:index] + "\n" + str[index:] + } + window.textArea.CursorPos++ + window.textArea.CurrentBuffer.Contents = str + } else if ev.Key() == tcell.KeyRune { + str := window.textArea.CurrentBuffer.Contents + index := window.textArea.CursorPos + + if index == len(str) { + str += string(ev.Rune()) + } else { + str = str[:index] + string(ev.Rune()) + str[index:] + } + window.textArea.CursorPos++ + window.textArea.CurrentBuffer.Contents = str + } } } @@ -197,8 +242,8 @@ func (window *Window) SetCursorPos(position int) { window.textArea.CursorPos = 0 } - if window.textArea.CursorPos > len(window.textArea.CurrentBuffer.Contents)-1 { - window.textArea.CursorPos = len(window.textArea.CurrentBuffer.Contents) - 1 + if window.textArea.CursorPos > len(window.textArea.CurrentBuffer.Contents) { + window.textArea.CursorPos = len(window.textArea.CurrentBuffer.Contents) } } @@ -214,7 +259,7 @@ func (window *Window) SetCursorPos2D(x, y int) { var str string for i, char := range window.textArea.CurrentBuffer.Contents { str += string(char) - if char == '\n' { + if char == '\n' || i == len(window.textArea.CurrentBuffer.Contents) { lines = append(lines, struct { charIndex int str string @@ -223,8 +268,14 @@ func (window *Window) SetCursorPos2D(x, y int) { } } - y = min(y, len(lines)-1) - x = min(x, len(lines[y].str)-1) + y = min(y, len(lines)) + + if y == len(lines) { + x = 0 + window.SetCursorPos(lines[y-1].charIndex + len(lines[y-1].str) + 1) + } else { + x = min(x, len(lines[y].str)-1) + window.SetCursorPos(lines[y].charIndex + x) + } - window.SetCursorPos(lines[y].charIndex + x) }