support history

This commit is contained in:
Cheney 2015-09-21 13:13:30 +08:00
parent 8f9ae9433d
commit 6642cc6506
6 changed files with 98 additions and 12 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.tmp

56
history.go Normal file
View File

@ -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
}

View File

@ -1,6 +1,7 @@
package readline package readline
import ( import (
"container/list"
"io" "io"
"os" "os"
) )
@ -10,15 +11,21 @@ type Readline struct {
t *Terminal t *Terminal
buf *RuneBuffer buf *RuneBuffer
outchan chan []rune outchan chan []rune
history *list.List
current *list.Element
} }
const ( const (
CharLineStart = 0x1 CharLineStart = 0x1
CharLineEnd = 0x5 CharLineEnd = 0x5
CharPrev = 0x2 CharNext = 0xe
CharNext = 0x6 CharPrev = 0x10
CharBackward = 0x2
CharForward = 0x6
CharEscape = 0x7f CharEscape = 0x7f
CharEnter = 0xd CharEnter = 0xd
CharEnter2 = 0xa
) )
type wrapWriter struct { type wrapWriter struct {
@ -40,6 +47,7 @@ func newReadline(r *os.File, t *Terminal, prompt string) *Readline {
t: t, t: t,
buf: NewRuneBuffer(t, prompt), buf: NewRuneBuffer(t, prompt),
outchan: make(chan []rune), outchan: make(chan []rune),
history: list.New(),
} }
go rl.ioloop() go rl.ioloop()
return rl return rl
@ -63,21 +71,33 @@ func (l *Readline) ioloop() {
l.buf.Delete() l.buf.Delete()
case CharEscape: case CharEscape:
l.buf.BackEscape() l.buf.BackEscape()
case CharEnter: case CharEnter, CharEnter2:
l.buf.WriteRune('\n') l.buf.WriteRune('\n')
data := l.buf.Reset() 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: case CharPrev:
l.buf.MovePrev() buf := l.PrevHistory()
if buf != nil {
l.buf.Set(buf)
}
case CharNext: case CharNext:
l.buf.MoveNext() buf := l.NextHistory()
if buf != nil {
l.buf.Set(buf)
}
case KeyInterrupt: case KeyInterrupt:
l.buf.WriteString("^C\n") l.buf.WriteString("^C\n")
l.outchan <- nil l.outchan <- nil
break
default: default:
l.buf.WriteRune(r) l.buf.WriteRune(r)
} }
l.UpdateHistory(l.buf.Runes())
} }
} }

View File

@ -42,7 +42,7 @@ func (r *RuneBuffer) MoveToLineStart() {
r.Refresh(-1, r.SetIdx(0)) r.Refresh(-1, r.SetIdx(0))
} }
func (r *RuneBuffer) MovePrev() { func (r *RuneBuffer) MoveBackward() {
if r.idx == 0 { if r.idx == 0 {
return return
} }
@ -65,7 +65,7 @@ func (rb *RuneBuffer) WriteRunes(r []rune) {
rb.Refresh(1, 1) rb.Refresh(1, 1)
} }
func (r *RuneBuffer) MoveNext() { func (r *RuneBuffer) MoveForward() {
if r.idx == len(r.buf) { if r.idx == len(r.buf) {
return return
} }
@ -187,3 +187,10 @@ func (r *RuneBuffer) Reset() []rune {
r.Refresh(-len(ret), r.SetIdx(0)) r.Refresh(-len(ret), r.SetIdx(0))
return ret 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)
}

View File

@ -21,7 +21,6 @@ const (
KeyInterrupt = 0x3 KeyInterrupt = 0x3
KeyNextChar = 0x6 KeyNextChar = 0x6
KeyDelete = 0x4 KeyDelete = 0x4
KeyEnter = 0xd
KeyEsc = 0x1b KeyEsc = 0x1b
) )
@ -89,9 +88,9 @@ func (t *Terminal) ioloop() {
goto exit goto exit
case KeyEsc: case KeyEsc:
prefix = true prefix = true
case KeyEnter, KeyPrevChar, KeyNextChar, KeyDelete: case CharEnter, CharEnter2, KeyPrevChar, KeyNextChar, KeyDelete:
fallthrough fallthrough
case CharLineEnd, CharLineStart: case CharLineEnd, CharLineStart, CharNext, CharPrev:
t.outchan <- r t.outchan <- r
default: default:
println("np:", r) println("np:", r)

View File

@ -33,6 +33,8 @@ func prefixKey(r rune) rune {
r = MetaNext r = MetaNext
case 'd': case 'd':
r = MetaDelete r = MetaDelete
case KeyEsc:
} }
return r return r
} }