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
// 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
}
}

View File

@ -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,
}