Basic character deletion
All checks were successful
timw4mail/gilo/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2021-04-02 10:48:51 -04:00
parent 42e50dfebb
commit f4a252a294
3 changed files with 45 additions and 8 deletions

View File

@ -100,6 +100,7 @@ func (e *editor) ProcessKeypress() bool {
case key.Enter: case key.Enter:
case key.Backspace, key.Ctrl('h'): case key.Backspace, key.Ctrl('h'):
e.delChar()
case key.Esc, key.Ctrl('l'): case key.Esc, key.Ctrl('l'):
// Modifier keys that return ANSI escape sequences // Modifier keys that return ANSI escape sequences
@ -116,6 +117,10 @@ func (e *editor) ProcessKeypress() bool {
keyEnd: keyEnd:
e.moveCursor(str) e.moveCursor(str)
case keyDelete:
e.moveCursor(keyRight)
e.delChar()
} }
default: default:
@ -209,6 +214,19 @@ func (e *editor) insertChar(ch rune) {
e.document.dirty = true 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 // Convert the raw ANSI escape sequences to the type of key input
func parseEscapeSequence() string { func parseEscapeSequence() string {
var runes []rune var runes []rune

View File

@ -54,6 +54,25 @@ func (r *row) insertRune(ch rune, at int) {
r.update() 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() { func (r *row) update() {
r.render = r.render[:0] r.render = r.render[:0]
replacement := strings.Repeat(" ", KiloTabStop) replacement := strings.Repeat(" ", KiloTabStop)