add support for arrow/up/down/left/right

This commit is contained in:
Cheney 2015-09-21 21:14:05 +08:00
parent 772978399e
commit e878807b59
3 changed files with 37 additions and 12 deletions

View File

@ -75,6 +75,7 @@ func (l *Operation) ioloop() {
case MetaBackspace:
l.buf.BackEscapeWord()
case CharEnter, CharEnter2:
l.buf.MoveToLineEnd()
l.buf.WriteRune('\n')
data := l.buf.Reset()
data = data[:len(data)-1] // trim \n

View File

@ -18,11 +18,12 @@ const (
)
const (
KeyPrevChar = 0x2
KeyInterrupt = 0x3
KeyNextChar = 0x6
KeyDelete = 0x4
KeyEsc = 0x1b
KeyPrevChar = 2
KeyInterrupt = 3
KeyNextChar = 6
KeyDelete = 4
KeyEsc = 27
KeyEscapeEx = 91
)
type Terminal struct {
@ -67,16 +68,26 @@ func (t *Terminal) ReadRune() rune {
func (t *Terminal) ioloop() {
buf := bufio.NewReader(os.Stdin)
prefix := false
isEscape := false
isEscapeEx := false
for {
r, _, err := buf.ReadRune()
Debug(r, isEscape, isEscapeEx)
if err != nil {
break
}
if prefix {
prefix = false
r = prefixKey(r)
if isEscape {
isEscape = false
if r == KeyEscapeEx {
isEscapeEx = true
continue
}
r = escapeKey(r)
} else if isEscapeEx {
isEscapeEx = false
r = escapeExKey(r)
Debug("r:", r)
}
if IsPrintable(r) || r < 0 {
@ -88,7 +99,7 @@ func (t *Terminal) ioloop() {
t.outchan <- r
goto exit
case KeyEsc:
prefix = true
isEscape = true
case CharEnter, CharEnter2, KeyPrevChar, KeyNextChar, KeyDelete:
fallthrough
case CharLineEnd, CharLineStart, CharNext, CharPrev:

View File

@ -28,7 +28,21 @@ func IsPrintable(key rune) bool {
return key >= 32 && !isInSurrogateArea
}
func prefixKey(r rune) rune {
func escapeExKey(r rune) rune {
switch r {
case 'D':
r = CharBackward
case 'C':
r = CharForward
case 'A':
r = CharPrev
case 'B':
r = CharNext
}
return r
}
func escapeKey(r rune) rune {
switch r {
case 'b':
r = MetaPrev
@ -64,7 +78,6 @@ func getWidth() int {
uintptr(syscall.TIOCGWINSZ),
uintptr(unsafe.Pointer(ws)))
Debug(ws)
if int(retCode) == -1 {
panic(errno)
}