Break everything again

This commit is contained in:
Timothy Warren 2021-03-30 15:45:13 -04:00
parent e0b348b941
commit b0c6d1b369
2 changed files with 98 additions and 59 deletions

View File

@ -1,7 +1,6 @@
package editor package editor
import ( import (
"strings"
"timshome.page/gilo/fn" "timshome.page/gilo/fn"
"timshome.page/gilo/terminal" "timshome.page/gilo/terminal"
) )
@ -28,15 +27,9 @@ func New() *editor {
} }
func (e *editor) ProcessKeypress() bool { func (e *editor) ProcessKeypress() bool {
var runes []rune var str string
// for ch, size := terminal.ReadKey(); size != 0; ch, size = terminal.ReadKey() {
for i := 0; i < 4; i++ {
ch, size := terminal.ReadKey()
if size == 0 {
break
}
ch, _ := terminal.ReadKey()
if ch == fn.Ctrl('q') { if ch == fn.Ctrl('q') {
// Clean up on exit // Clean up on exit
terminal.Write(terminal.ClearScreen + terminal.ResetCursor) terminal.Write(terminal.ClearScreen + terminal.ResetCursor)
@ -44,57 +37,109 @@ func (e *editor) ProcessKeypress() bool {
return false return false
} }
runes = append(runes, ch) if ch == '\x1b' {
str = string('\x1b') + parseEscapeSequence()
// Break early for arrow keys } else {
if i == 2 && ch >= 'A' && ch <= 'D' { str = string(ch)
break
}
} }
// terminal.Write("%v", runes) switch str {
str := string(runes) case keyUp, keyDown, keyLeft, keyRight, keyPageUp, keyPageDown, keyHome, keyEnd:
runes = runes[:0] e.moveCursor(str)
// Escape sequences can be less fun...
if strings.Contains(str, terminal.EscPrefix) {
code := strings.TrimPrefix(str, terminal.EscPrefix)
switch code {
case KeyArrowUp, KeyArrowDown, KeyArrowLeft, KeyArrowRight, KeyPageUp, KeyPageDown:
e.moveCursor(code)
runes = runes[:0]
return true return true
default: default:
// Do something later // Do something later
terminal.Write("Code: %v", str) terminal.Write("Code: %v", str)
} }
}
return true return true
} }
func (e *editor) moveCursor (key string) { func (e *editor) moveCursor (key string) {
switch key { switch key {
case KeyArrowLeft: case keyLeft:
if e.cursor.x != 0 { if e.cursor.x != 0 {
e.cursor.x -= 1 e.cursor.x -= 1
} }
case KeyArrowRight: case keyRight:
if e.cursor.x != e.screen.Cols-1 { if e.cursor.x != e.screen.Cols-1 {
e.cursor.x += 1 e.cursor.x += 1
} }
case KeyArrowUp: case keyUp:
if e.cursor.y != 0 { if e.cursor.y != 0 {
e.cursor.y -= 1 e.cursor.y -= 1
} }
case KeyArrowDown: case keyDown:
if e.cursor.y != e.screen.Rows-1 { if e.cursor.y != e.screen.Rows-1 {
e.cursor.y += 1 e.cursor.y += 1
} }
case KeyPageUp: case keyPageUp:
e.cursor.y = 0 e.cursor.y = 0
case KeyPageDown: case keyPageDown:
e.cursor.y = e.screen.Rows e.cursor.y = e.screen.Rows
case keyHome:
e.cursor.x = 0
case keyEnd:
e.cursor.x = e.screen.Cols
} }
} }
func parseEscapeSequence () string {
var runes []rune
for i := 0; i < 3; i++ {
ch, size := terminal.ReadKey()
if size == 0 {
return "\x1b"
}
runes = append(runes, ch)
if i == 1 && runes[1] >= 'A' {
if runes[0] == 'O' {
switch runes[1] {
case 'H':
return keyHome
case 'F':
return keyEnd
}
}
if runes[0] == '[' {
switch runes[1] {
case 'A':
return keyUp
case 'B':
return keyDown
case 'C':
return keyRight
case 'D':
return keyLeft
case 'H':
return keyHome
case 'F':
return keyEnd
}
}
}
if i == 2 && runes[0] == '[' && runes[2] == '~' {
switch runes[1] {
case '1':
return keyHome
case '3':
return keyDelete
case '4':
return keyEnd
case '5':
return keyPageUp
case '6':
return keyPageDown
case '7':
return keyHome
case '8':
return keyEnd
}
}
}
return string('\x1b')
}

View File

@ -18,19 +18,13 @@ const(
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
const ( const (
keyUp = '↑' keyUp = "ARROW_UP"
keyDown = '↓' keyDown = "ARROW_DOWN"
keyLeft = '←' keyLeft = "ARROW_LEFT"
keyRight = '→' keyRight = "ARROW_RIGHT"
keyPageUp = '⭱' keyPageUp = "PAGE_UP"
keyPageDown = '⭳' keyPageDown = "PAGE_DOWN"
keyHome = "HOME"
keyEnd = "END"
keyDelete = "DELETE"
) )
var keyMap = map[string]rune{
KeyArrowUp: keyUp,
KeyArrowDown: keyDown,
KeyArrowLeft: keyLeft,
KeyArrowRight: keyRight,
KeyPageUp: keyPageUp,
KeyPageDown: keyPageDown,
}