Skip to content

Commit

Permalink
add support for arrow/up/down/left/right
Browse files Browse the repository at this point in the history
  • Loading branch information
chzyer committed Sep 21, 2015
1 parent 7729783 commit e878807
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
1 change: 1 addition & 0 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 21 additions & 10 deletions terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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:
Expand Down
17 changes: 15 additions & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -64,7 +78,6 @@ func getWidth() int {
uintptr(syscall.TIOCGWINSZ),
uintptr(unsafe.Pointer(ws)))

Debug(ws)
if int(retCode) == -1 {
panic(errno)
}
Expand Down

0 comments on commit e878807

Please sign in to comment.