Refactor input mapping
All checks were successful
timw4mail/gilo/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2021-04-02 11:57:24 -04:00
parent f4a252a294
commit f2e9cd3075

View File

@ -80,6 +80,13 @@ func (e *editor) SetStatusMessage(template string, a ...interface{}) {
func (e *editor) ProcessKeypress() bool { func (e *editor) ProcessKeypress() bool {
ch, _ := terminal.ReadKey() ch, _ := terminal.ReadKey()
return e.processKeypressChar(ch)
}
/**
* Determine what to do with an individual character of input
*/
func (e *editor) processKeypressChar(ch rune) bool {
switch ch { switch ch {
case key.Ctrl('q'): case key.Ctrl('q'):
if e.document.dirty && e.quitTimes > 0 { if e.document.dirty && e.quitTimes > 0 {
@ -106,7 +113,34 @@ func (e *editor) ProcessKeypress() bool {
// Modifier keys that return ANSI escape sequences // Modifier keys that return ANSI escape sequences
str := parseEscapeSequence() str := parseEscapeSequence()
switch str { // Don't swallow a character after ESC if it doesn't
// start an ANSI escape sequence
if len(str) == 1 {
return e.processKeypressChar(rune(str[0]))
}
e.processKeypressStr(str)
default:
e.insertChar(ch)
}
// Clear the quit message and restart the
// confirmation count if confirmation is not
// completed
if e.quitTimes != KiloQuitTimes {
e.quitTimes = KiloQuitTimes
e.SetStatusMessage("")
}
return true
}
/**
* Determine what do do with a parsed ANSI escape sequence
*/
func (e *editor) processKeypressStr(key string) {
switch key {
case keyUp, case keyUp,
keyDown, keyDown,
keyLeft, keyLeft,
@ -116,22 +150,12 @@ func (e *editor) ProcessKeypress() bool {
keyHome, keyHome,
keyEnd: keyEnd:
e.moveCursor(str) e.moveCursor(key)
case keyDelete: case keyDelete:
e.moveCursor(keyRight) e.moveCursor(keyRight)
e.delChar() e.delChar()
} }
default:
e.insertChar(ch)
}
if e.quitTimes != KiloQuitTimes {
e.quitTimes = KiloQuitTimes
}
return true
} }
func (e *editor) moveCursor(key string) { func (e *editor) moveCursor(key string) {
@ -229,28 +253,51 @@ func (e *editor) delChar() {
// 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 // If we aren't starting an escape sequence,
// return the character
startChar, _ := terminal.ReadKey()
if startChar != '[' && startChar != 'O' {
return string(startChar)
}
for i := 0; i < 3; i++ { // Read one or two characters after
// \e[ or \eO, which is the end of the
// handled escape sequences
runes := [2]rune{'\000', '\000'}
for i := 0; i < 2; i++ {
ch, size := terminal.ReadKey() ch, size := terminal.ReadKey()
if size == 0 { if size == 0 {
return "\x1b" return string(rune(key.Esc))
} }
runes = append(runes, ch) runes[i] = ch
if i == 1 && runes[1] >= 'A' { if i == 0 && runes[0] >= 'A' && runes[0] <= 'Z' {
// \eOH \eOF return escSeqToKey([]rune{startChar, runes[0]})
if runes[0] == 'O' { }
switch runes[1] {
// \e[*~
if i == 1 && startChar == '[' && runes[1] == '~' {
return escSeqToKey([]rune{startChar, runes[0], runes[1]})
}
}
return string(rune(key.Esc))
}
func escSeqToKey (seq []rune) string {
// \eO*
// \e[*
if len(seq) == 2 {
startChar, cmd := seq[0], seq[1]
if startChar == 'O' {
switch cmd {
case 'H': case 'H':
return keyHome return keyHome
case 'F': case 'F':
return keyEnd return keyEnd
} }
} } else if startChar == '[' {
// \e[A switch cmd {
if runes[0] == '[' {
switch runes[1] {
case 'A': case 'A':
return keyUp return keyUp
case 'B': case 'B':
@ -265,11 +312,9 @@ func parseEscapeSequence() string {
return keyEnd return keyEnd
} }
} }
} } else if len(seq) == 3 { // \e[*~
cmd := seq[1]
// \e[1~ switch cmd {
if i == 2 && runes[0] == '[' && runes[2] == '~' {
switch runes[1] {
case '1': case '1':
return keyHome return keyHome
case '3': case '3':
@ -286,7 +331,6 @@ func parseEscapeSequence() string {
return keyEnd return keyEnd
} }
} }
}
return string('\x1b') return string(rune(key.Esc))
} }