Make PageUp and PageDown work

This commit is contained in:
Timothy Warren 2021-03-30 14:42:59 -04:00
parent 4dfe797467
commit e0b348b941
2 changed files with 35 additions and 14 deletions

View File

@ -31,7 +31,7 @@ func (e *editor) ProcessKeypress() bool {
var runes []rune var runes []rune
// for ch, size := terminal.ReadKey(); size != 0; ch, size = terminal.ReadKey() { // 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() ch, size := terminal.ReadKey()
if size == 0 { if size == 0 {
break break
@ -45,9 +45,14 @@ func (e *editor) ProcessKeypress() bool {
} }
runes = append(runes, ch) 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) str := string(runes)
runes = runes[:0] runes = runes[:0]
@ -56,7 +61,7 @@ func (e *editor) ProcessKeypress() bool {
code := strings.TrimPrefix(str, terminal.EscPrefix) code := strings.TrimPrefix(str, terminal.EscPrefix)
switch code { switch code {
case KeyArrowUp, KeyArrowDown, KeyArrowLeft, KeyArrowRight: case KeyArrowUp, KeyArrowDown, KeyArrowLeft, KeyArrowRight, KeyPageUp, KeyPageDown:
e.moveCursor(code) e.moveCursor(code)
runes = runes[:0] runes = runes[:0]
return true return true
@ -69,17 +74,27 @@ func (e *editor) ProcessKeypress() bool {
return true return true
} }
func (e *editor) moveCursor (rawKey string) { func (e *editor) moveCursor (key string) {
key := keyMap[rawKey]
switch key { switch key {
case keyLeft: case KeyArrowLeft:
if e.cursor.x != 0 {
e.cursor.x -= 1 e.cursor.x -= 1
case keyRight: }
case KeyArrowRight:
if e.cursor.x != e.screen.Cols-1 {
e.cursor.x += 1 e.cursor.x += 1
case keyUp: }
case KeyArrowUp:
if e.cursor.y != 0 {
e.cursor.y -= 1 e.cursor.y -= 1
case keyDown: }
case KeyArrowDown:
if e.cursor.y != e.screen.Rows-1 {
e.cursor.y += 1 e.cursor.y += 1
} }
case KeyPageUp:
e.cursor.y = 0
case KeyPageDown:
e.cursor.y = e.screen.Rows
}
} }

View File

@ -9,6 +9,8 @@ const(
KeyArrowDown = "B" KeyArrowDown = "B"
KeyArrowRight = "C" KeyArrowRight = "C"
KeyArrowLeft = "D" KeyArrowLeft = "D"
KeyPageUp = "5~"
KeyPageDown = "6~"
) )
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -20,6 +22,8 @@ const (
keyDown = '↓' keyDown = '↓'
keyLeft = '←' keyLeft = '←'
keyRight = '→' keyRight = '→'
keyPageUp = '⭱'
keyPageDown = '⭳'
) )
var keyMap = map[string]rune{ var keyMap = map[string]rune{
@ -27,4 +31,6 @@ var keyMap = map[string]rune{
KeyArrowDown: keyDown, KeyArrowDown: keyDown,
KeyArrowLeft: keyLeft, KeyArrowLeft: keyLeft,
KeyArrowRight: keyRight, KeyArrowRight: keyRight,
KeyPageUp: keyPageUp,
KeyPageDown: keyPageDown,
} }