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: case MetaBackspace:
l.buf.BackEscapeWord() l.buf.BackEscapeWord()
case CharEnter, CharEnter2: case CharEnter, CharEnter2:
l.buf.MoveToLineEnd()
l.buf.WriteRune('\n') l.buf.WriteRune('\n')
data := l.buf.Reset() data := l.buf.Reset()
data = data[:len(data)-1] // trim \n data = data[:len(data)-1] // trim \n

View File

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

View File

@ -28,7 +28,21 @@ func IsPrintable(key rune) bool {
return key >= 32 && !isInSurrogateArea 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 { switch r {
case 'b': case 'b':
r = MetaPrev r = MetaPrev
@ -64,7 +78,6 @@ func getWidth() int {
uintptr(syscall.TIOCGWINSZ), uintptr(syscall.TIOCGWINSZ),
uintptr(unsafe.Pointer(ws))) uintptr(unsafe.Pointer(ws)))
Debug(ws)
if int(retCode) == -1 { if int(retCode) == -1 {
panic(errno) panic(errno)
} }