Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
chzyer committed Sep 23, 2015
1 parent 2a02950 commit 4ab9a96
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 52 deletions.
32 changes: 32 additions & 0 deletions char.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package readline

const (
CharLineStart = 1
CharBackward = 2
CharInterrupt = 3
CharDelete = 4
CharLineEnd = 5
CharForward = 6
CharCannel = 7
CharCtrlH = 8
CharCtrlJ = 10
CharKill = 11
CharEnter = 13
CharNext = 14
CharPrev = 16
CharBckSearch = 18
CharFwdSearch = 19
CharTransform = 20
CharCtrlW = 23
CharEsc = 27
CharEscapeEx = 91
CharBackspace = 127
)

const (
MetaPrev = -iota - 1
MetaNext
MetaDelete
MetaBackspace
MetaTransform
)
28 changes: 7 additions & 21 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,6 @@ type Operation struct {
*opSearch
}

const (
CharLineStart = 1
CharLineEnd = 5
CharKill = 11
CharNext = 14
CharPrev = 16
CharBackward = 2
CharForward = 6
CharBackspace = 0x7f
CharEnter = 0xd
CharEnter2 = 0xa
CharBckSearch = 18
CharFwdSearch = 19
CharCannel = 7
)

type wrapWriter struct {
r *Operation
target io.Writer
Expand Down Expand Up @@ -76,6 +60,8 @@ func (l *Operation) ioloop() {
l.buf.Kill()
case MetaNext:
l.buf.MoveToNextWord()
case CharTransform:
l.buf.Transform()
case MetaPrev:
l.buf.MoveToPrevWord()
case MetaDelete:
Expand All @@ -84,18 +70,18 @@ func (l *Operation) ioloop() {
l.buf.MoveToLineStart()
case CharLineEnd:
l.buf.MoveToLineEnd()
case KeyDelete:
case CharDelete:
l.buf.Delete()
case CharBackspace:
case CharBackspace, CharCtrlH:
if l.IsSearchMode() {
l.SearchBackspace()
keepInSearchMode = true
} else {
l.buf.Backspace()
}
case MetaBackspace:
case MetaBackspace, CharCtrlW:
l.buf.BackEscapeWord()
case CharEnter, CharEnter2:
case CharEnter, CharCtrlJ:
if l.IsSearchMode() {
l.ExitSearchMode(false)
}
Expand All @@ -119,7 +105,7 @@ func (l *Operation) ioloop() {
if ok {
l.buf.Set(buf)
}
case KeyInterrupt:
case CharInterrupt:
if l.IsSearchMode() {
l.ExitSearchMode(false)
}
Expand Down
4 changes: 4 additions & 0 deletions runebuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ func (r *RuneBuffer) Kill() {
r.Refresh()
}

func (r *RuneBuffer) Transform() {

}

func (r *RuneBuffer) MoveToNextWord() {
for i := r.idx + 1; i < len(r.buf); i++ {
if r.buf[i] != ' ' && r.buf[i-1] == ' ' {
Expand Down
34 changes: 4 additions & 30 deletions terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,6 @@ import (
"golang.org/x/crypto/ssh/terminal"
)

const (
MetaPrev = -iota - 1
MetaNext
MetaDelete
MetaBackspace
)

const (
KeyPrevChar = 2
KeyInterrupt = 3
KeyNextChar = 6
KeyDelete = 4
KeyEsc = 27
KeyEscapeEx = 91
)

type Terminal struct {
cfg *Config
state *terminal.State
Expand Down Expand Up @@ -80,7 +64,7 @@ func (t *Terminal) ioloop() {

if isEscape {
isEscape = false
if r == KeyEscapeEx {
if r == CharEscapeEx {
isEscapeEx = true
continue
}
Expand All @@ -90,24 +74,14 @@ func (t *Terminal) ioloop() {
r = escapeExKey(r)
}

if IsPrintable(r) || r < 0 {
t.outchan <- r
continue
}
switch r {
case KeyInterrupt:
case CharInterrupt:
t.outchan <- r
goto exit
case KeyEsc:
case CharEsc:
isEscape = true
case CharEnter, CharEnter2, KeyPrevChar, KeyNextChar, KeyDelete:
fallthrough
case CharFwdSearch, CharBckSearch, CharCannel:
fallthrough
case CharLineEnd, CharLineStart, CharNext, CharPrev, CharKill:
t.outchan <- r
default:
println("np:", r)
t.outchan <- r
}
}
exit:
Expand Down
2 changes: 1 addition & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func escapeKey(r rune) rune {
r = MetaDelete
case CharBackspace:
r = MetaBackspace
case KeyEsc:
case CharEsc:

}
return r
Expand Down

0 comments on commit 4ab9a96

Please sign in to comment.