forked from mirror/readline
support history
This commit is contained in:
parent
8f9ae9433d
commit
6642cc6506
|
@ -0,0 +1,2 @@
|
|||
|
||||
*.tmp
|
|
@ -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
readline.go
34
readline.go
|
@ -1,6 +1,7 @@
|
|||
package readline
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
11
runebuf.go
11
runebuf.go
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -21,7 +21,6 @@ const (
|
|||
KeyInterrupt = 0x3
|
||||
KeyNextChar = 0x6
|
||||
KeyDelete = 0x4
|
||||
KeyEnter = 0xd
|
||||
KeyEsc = 0x1b
|
||||
)
|
||||
|
||||
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue