From f4a252a29474a5c5f75f34d4f44db3454981b30e Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 2 Apr 2021 10:48:51 -0400 Subject: [PATCH] Basic character deletion --- editor/constants.go | 4 ++-- editor/editor.go | 30 ++++++++++++++++++++++++------ editor/row.go | 19 +++++++++++++++++++ 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/editor/constants.go b/editor/constants.go index 0027ff3..73edecf 100644 --- a/editor/constants.go +++ b/editor/constants.go @@ -5,7 +5,7 @@ package editor // ---------------------------------------------------------------------------- const ( - KiloVersion = "0.0.1" - KiloTabStop = 4 + KiloVersion = "0.0.1" + KiloTabStop = 4 KiloQuitTimes = 3 ) diff --git a/editor/editor.go b/editor/editor.go index 20cedc2..cb31ef5 100644 --- a/editor/editor.go +++ b/editor/editor.go @@ -22,13 +22,13 @@ type statusMsg struct { } type editor struct { - screen *terminal.Screen - cursor *point - offset *point - document *document - status *statusMsg + screen *terminal.Screen + cursor *point + offset *point + document *document + status *statusMsg quitTimes uint8 - renderX int + renderX int } func New() *editor { @@ -100,6 +100,7 @@ func (e *editor) ProcessKeypress() bool { case key.Enter: case key.Backspace, key.Ctrl('h'): + e.delChar() case key.Esc, key.Ctrl('l'): // Modifier keys that return ANSI escape sequences @@ -116,6 +117,10 @@ func (e *editor) ProcessKeypress() bool { keyEnd: e.moveCursor(str) + + case keyDelete: + e.moveCursor(keyRight) + e.delChar() } default: @@ -209,6 +214,19 @@ func (e *editor) insertChar(ch rune) { e.document.dirty = true } +func (e *editor) delChar() { + if e.cursor.y == e.document.rowCount() { + return + } + + if e.cursor.x > 0 { + e.document.rows[e.cursor.y].deleteRune(e.cursor.x - 1) + e.cursor.x -= 1 + } + + e.document.dirty = true +} + // Convert the raw ANSI escape sequences to the type of key input func parseEscapeSequence() string { var runes []rune diff --git a/editor/row.go b/editor/row.go index 5a51964..04c99d5 100644 --- a/editor/row.go +++ b/editor/row.go @@ -54,6 +54,25 @@ func (r *row) insertRune(ch rune, at int) { r.update() } +func (r *row) deleteRune(at int) { + if at < 0 || at >= r.size() { + return + } + + var newSlice []rune + + // Split the character array at the insertion point + start := r.chars[0:at] + end := r.chars[at+1 : r.size()] // Skip the index in question + + // Splice it back together + newSlice = append(newSlice, start...) + newSlice = append(newSlice, end...) + + r.chars = newSlice + r.update() +} + func (r *row) update() { r.render = r.render[:0] replacement := strings.Repeat(" ", KiloTabStop)