From e0b348b9416f42289230c6178ff30cca4b99660a Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 30 Mar 2021 14:42:59 -0400 Subject: [PATCH] Make PageUp and PageDown work --- editor/editor.go | 43 +++++++++++++++++++++++++++++-------------- editor/keymap.go | 6 ++++++ 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/editor/editor.go b/editor/editor.go index db14733..7dede1d 100644 --- a/editor/editor.go +++ b/editor/editor.go @@ -31,7 +31,7 @@ func (e *editor) ProcessKeypress() bool { var runes []rune // for ch, size := terminal.ReadKey(); size != 0; ch, size = terminal.ReadKey() { - for i := 0; i < 3; i++ { + for i := 0; i < 4; i++ { ch, size := terminal.ReadKey() if size == 0 { break @@ -45,9 +45,14 @@ func (e *editor) ProcessKeypress() bool { } runes = append(runes, ch) + + // Break early for arrow keys + if i == 2 && ch >= 'A' && ch <= 'D' { + break + } } - terminal.Write("%v", runes) + // terminal.Write("%v", runes) str := string(runes) runes = runes[:0] @@ -56,7 +61,7 @@ func (e *editor) ProcessKeypress() bool { code := strings.TrimPrefix(str, terminal.EscPrefix) switch code { - case KeyArrowUp, KeyArrowDown, KeyArrowLeft, KeyArrowRight: + case KeyArrowUp, KeyArrowDown, KeyArrowLeft, KeyArrowRight, KeyPageUp, KeyPageDown: e.moveCursor(code) runes = runes[:0] return true @@ -69,17 +74,27 @@ func (e *editor) ProcessKeypress() bool { return true } -func (e *editor) moveCursor (rawKey string) { - key := keyMap[rawKey] - +func (e *editor) moveCursor (key string) { switch key { - case keyLeft: - e.cursor.x -= 1 - case keyRight: - e.cursor.x += 1 - case keyUp: - e.cursor.y -= 1 - case keyDown: - e.cursor.y += 1 + case KeyArrowLeft: + if e.cursor.x != 0 { + e.cursor.x -= 1 + } + case KeyArrowRight: + if e.cursor.x != e.screen.Cols-1 { + e.cursor.x += 1 + } + case KeyArrowUp: + if e.cursor.y != 0 { + e.cursor.y -= 1 + } + case KeyArrowDown: + if e.cursor.y != e.screen.Rows-1 { + e.cursor.y += 1 + } + case KeyPageUp: + e.cursor.y = 0 + case KeyPageDown: + e.cursor.y = e.screen.Rows } } \ No newline at end of file diff --git a/editor/keymap.go b/editor/keymap.go index a8676b0..74d7495 100644 --- a/editor/keymap.go +++ b/editor/keymap.go @@ -9,6 +9,8 @@ const( KeyArrowDown = "B" KeyArrowRight = "C" KeyArrowLeft = "D" + KeyPageUp = "5~" + KeyPageDown = "6~" ) // ---------------------------------------------------------------------------- @@ -20,6 +22,8 @@ const ( keyDown = '↓' keyLeft = '←' keyRight = '→' + keyPageUp = '⭱' + keyPageDown = '⭳' ) var keyMap = map[string]rune{ @@ -27,4 +31,6 @@ var keyMap = map[string]rune{ KeyArrowDown: keyDown, KeyArrowLeft: keyLeft, KeyArrowRight: keyRight, + KeyPageUp: keyPageUp, + KeyPageDown: keyPageDown, } \ No newline at end of file