diff --git a/operation.go b/operation.go index baf3046..e0ca9b4 100644 --- a/operation.go +++ b/operation.go @@ -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 diff --git a/terminal.go b/terminal.go index f1793bf..e961d47 100644 --- a/terminal.go +++ b/terminal.go @@ -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: diff --git a/utils.go b/utils.go index 6218c0a..4cf9592 100644 --- a/utils.go +++ b/utils.go @@ -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) }