Skip to content

Commit

Permalink
support history
Browse files Browse the repository at this point in the history
  • Loading branch information
chzyer committed Sep 21, 2015
1 parent 8f9ae94 commit 6642cc6
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

*.tmp
56 changes: 56 additions & 0 deletions history.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package readline

func (l *Readline) PrevHistory() []rune {
if l.current == nil {
return nil
}
current := l.current.Prev()
if current == nil {
return nil
}
l.current = current
return current.Value.([]rune)
}

func (l *Readline) NextHistory() []rune {
if l.current == nil {
return nil
}
current := l.current.Next()
if current == nil {
return nil
}
l.current = current
return current.Value.([]rune)
}

func (l *Readline) NewHistory(current []rune) {
l.UpdateHistory(current)
if l.current != l.history.Back() {
// move history item to current command
l.history.Remove(l.current)
use := l.current.Value.([]rune)
l.current = l.history.Back()
l.UpdateHistory(use)
}

// push a new one to commit current command
l.PushHistory(nil)
}

func (l *Readline) UpdateHistory(s []rune) {
if l.current == nil {
l.PushHistory(s)
return
}
r := l.current.Value.([]rune)
l.current.Value = append(r[:0], s...)
}

func (l *Readline) PushHistory(s []rune) {
// copy
newCopy := make([]rune, len(s))
copy(newCopy, s)
elem := l.history.PushBack(newCopy)
l.current = elem
}
34 changes: 27 additions & 7 deletions readline.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package readline

import (
"container/list"
"io"
"os"
)
Expand All @@ -10,15 +11,21 @@ type Readline struct {
t *Terminal
buf *RuneBuffer
outchan chan []rune

history *list.List
current *list.Element
}

const (
CharLineStart = 0x1
CharLineEnd = 0x5
CharPrev = 0x2
CharNext = 0x6
CharNext = 0xe
CharPrev = 0x10
CharBackward = 0x2
CharForward = 0x6
CharEscape = 0x7f
CharEnter = 0xd
CharEnter2 = 0xa
)

type wrapWriter struct {
Expand All @@ -40,6 +47,7 @@ func newReadline(r *os.File, t *Terminal, prompt string) *Readline {
t: t,
buf: NewRuneBuffer(t, prompt),
outchan: make(chan []rune),
history: list.New(),
}
go rl.ioloop()
return rl
Expand All @@ -63,21 +71,33 @@ func (l *Readline) ioloop() {
l.buf.Delete()
case CharEscape:
l.buf.BackEscape()
case CharEnter:
case CharEnter, CharEnter2:
l.buf.WriteRune('\n')
data := l.buf.Reset()
l.outchan <- data[:len(data)-1]
data = data[:len(data)-1] // trim \n
l.outchan <- data
l.NewHistory(data)
case CharBackward:
l.buf.MoveBackward()
case CharForward:
l.buf.MoveForward()
case CharPrev:
l.buf.MovePrev()
buf := l.PrevHistory()
if buf != nil {
l.buf.Set(buf)
}
case CharNext:
l.buf.MoveNext()
buf := l.NextHistory()
if buf != nil {
l.buf.Set(buf)
}
case KeyInterrupt:
l.buf.WriteString("^C\n")
l.outchan <- nil
break
default:
l.buf.WriteRune(r)
}
l.UpdateHistory(l.buf.Runes())
}
}

Expand Down
11 changes: 9 additions & 2 deletions runebuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (r *RuneBuffer) MoveToLineStart() {
r.Refresh(-1, r.SetIdx(0))
}

func (r *RuneBuffer) MovePrev() {
func (r *RuneBuffer) MoveBackward() {
if r.idx == 0 {
return
}
Expand All @@ -65,7 +65,7 @@ func (rb *RuneBuffer) WriteRunes(r []rune) {
rb.Refresh(1, 1)
}

func (r *RuneBuffer) MoveNext() {
func (r *RuneBuffer) MoveForward() {
if r.idx == len(r.buf) {
return
}
Expand Down Expand Up @@ -187,3 +187,10 @@ func (r *RuneBuffer) Reset() []rune {
r.Refresh(-len(ret), r.SetIdx(0))
return ret
}

func (r *RuneBuffer) Set(buf []rune) {
length, idx := len(r.buf), r.idx
r.buf = buf
r.idx = len(r.buf)
r.RefreshSet(length, idx)
}
5 changes: 2 additions & 3 deletions terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const (
KeyInterrupt = 0x3
KeyNextChar = 0x6
KeyDelete = 0x4
KeyEnter = 0xd
KeyEsc = 0x1b
)

Expand Down Expand Up @@ -89,9 +88,9 @@ func (t *Terminal) ioloop() {
goto exit
case KeyEsc:
prefix = true
case KeyEnter, KeyPrevChar, KeyNextChar, KeyDelete:
case CharEnter, CharEnter2, KeyPrevChar, KeyNextChar, KeyDelete:
fallthrough
case CharLineEnd, CharLineStart:
case CharLineEnd, CharLineStart, CharNext, CharPrev:
t.outchan <- r
default:
println("np:", r)
Expand Down
2 changes: 2 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func prefixKey(r rune) rune {
r = MetaNext
case 'd':
r = MetaDelete
case KeyEsc:

}
return r
}
Expand Down

0 comments on commit 6642cc6

Please sign in to comment.