This commit is contained in:
Cheney 2015-09-21 13:30:10 +08:00
parent 6642cc6506
commit c16e43d258
5 changed files with 185 additions and 142 deletions

View File

@ -10,13 +10,12 @@ import (
) )
func main() { func main() {
t, err := readline.NewTerminal() l, err := readline.New("> ")
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer t.Close() defer l.Close()
l := t.NewReadline("> ")
log.SetOutput(l.Stderr()) log.SetOutput(l.Stderr())
for { for {
line, err := l.Readline() line, err := l.Readline()

View File

@ -1,56 +1,56 @@
package readline package readline
func (l *Readline) PrevHistory() []rune { func (o *Operation) PrevHistory() []rune {
if l.current == nil { if o.current == nil {
return nil return nil
} }
current := l.current.Prev() current := o.current.Prev()
if current == nil { if current == nil {
return nil return nil
} }
l.current = current o.current = current
return current.Value.([]rune) return current.Value.([]rune)
} }
func (l *Readline) NextHistory() []rune { func (o *Operation) NextHistory() []rune {
if l.current == nil { if o.current == nil {
return nil return nil
} }
current := l.current.Next() current := o.current.Next()
if current == nil { if current == nil {
return nil return nil
} }
l.current = current o.current = current
return current.Value.([]rune) return current.Value.([]rune)
} }
func (l *Readline) NewHistory(current []rune) { func (o *Operation) NewHistory(current []rune) {
l.UpdateHistory(current) o.UpdateHistory(current)
if l.current != l.history.Back() { if o.current != o.history.Back() {
// move history item to current command // move history item to current command
l.history.Remove(l.current) o.history.Remove(o.current)
use := l.current.Value.([]rune) use := o.current.Value.([]rune)
l.current = l.history.Back() o.current = o.history.Back()
l.UpdateHistory(use) o.UpdateHistory(use)
} }
// push a new one to commit current command // push a new one to commit current command
l.PushHistory(nil) o.PushHistory(nil)
} }
func (l *Readline) UpdateHistory(s []rune) { func (o *Operation) UpdateHistory(s []rune) {
if l.current == nil { if o.current == nil {
l.PushHistory(s) o.PushHistory(s)
return return
} }
r := l.current.Value.([]rune) r := o.current.Value.([]rune)
l.current.Value = append(r[:0], s...) o.current.Value = append(r[:0], s...)
} }
func (l *Readline) PushHistory(s []rune) { func (o *Operation) PushHistory(s []rune) {
// copy // copy
newCopy := make([]rune, len(s)) newCopy := make([]rune, len(s))
copy(newCopy, s) copy(newCopy, s)
elem := l.history.PushBack(newCopy) elem := o.history.PushBack(newCopy)
l.current = elem o.current = elem
} }

131
operation.go Normal file
View File

@ -0,0 +1,131 @@
package readline
import (
"container/list"
"io"
"os"
)
type Operation struct {
r *os.File
t *Terminal
buf *RuneBuffer
outchan chan []rune
history *list.List
current *list.Element
}
const (
CharLineStart = 0x1
CharLineEnd = 0x5
CharNext = 0xe
CharPrev = 0x10
CharBackward = 0x2
CharForward = 0x6
CharEscape = 0x7f
CharEnter = 0xd
CharEnter2 = 0xa
)
type wrapWriter struct {
r *Operation
target io.Writer
}
func (w *wrapWriter) Write(b []byte) (int, error) {
buf := w.r.buf
buf.Clean()
n, err := w.target.Write(b)
w.r.buf.RefreshSet(0, 0)
return n, err
}
func NewOperation(r *os.File, t *Terminal, prompt string) *Operation {
op := &Operation{
r: r,
t: t,
buf: NewRuneBuffer(t, prompt),
outchan: make(chan []rune),
history: list.New(),
}
go op.ioloop()
return op
}
func (l *Operation) ioloop() {
for {
r := l.t.ReadRune()
switch r {
case MetaNext:
l.buf.MoveToNextWord()
case MetaPrev:
l.buf.MoveToPrevWord()
case MetaDelete:
l.buf.DeleteWord()
case CharLineStart:
l.buf.MoveToLineStart()
case CharLineEnd:
l.buf.MoveToLineEnd()
case KeyDelete:
l.buf.Delete()
case CharEscape:
l.buf.BackEscape()
case CharEnter, CharEnter2:
l.buf.WriteRune('\n')
data := l.buf.Reset()
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:
buf := l.PrevHistory()
if buf != nil {
l.buf.Set(buf)
}
case CharNext:
buf := l.NextHistory()
if buf != nil {
l.buf.Set(buf)
}
case KeyInterrupt:
l.buf.WriteString("^C\n")
l.outchan <- nil
default:
l.buf.WriteRune(r)
}
l.UpdateHistory(l.buf.Runes())
}
}
func (l *Operation) Stderr() io.Writer {
return &wrapWriter{target: os.Stderr, r: l}
}
func (l *Operation) String() (string, error) {
r, err := l.Runes()
if err != nil {
return "", err
}
return string(r), nil
}
func (l *Operation) Runes() ([]rune, error) {
l.buf.Refresh(0, 0) // print prompt
r := <-l.outchan
if r == nil {
return nil, io.EOF
}
return r, nil
}
func (l *Operation) Slice() ([]byte, error) {
r, err := l.Runes()
if err != nil {
return nil, err
}
return []byte(string(r)), nil
}

View File

@ -1,123 +1,36 @@
package readline package readline
import ( import "io"
"container/list"
"io"
"os"
)
type Readline struct { type Instance struct {
r *os.File
t *Terminal t *Terminal
buf *RuneBuffer o *Operation
outchan chan []rune
history *list.List
current *list.Element
} }
const ( func New(prompt string) (*Instance, error) {
CharLineStart = 0x1 t, err := NewTerminal()
CharLineEnd = 0x5
CharNext = 0xe
CharPrev = 0x10
CharBackward = 0x2
CharForward = 0x6
CharEscape = 0x7f
CharEnter = 0xd
CharEnter2 = 0xa
)
type wrapWriter struct {
r *Readline
target io.Writer
}
func (w *wrapWriter) Write(b []byte) (int, error) {
buf := w.r.buf
buf.Clean()
n, err := w.target.Write(b)
w.r.buf.RefreshSet(0, 0)
return n, err
}
func newReadline(r *os.File, t *Terminal, prompt string) *Readline {
rl := &Readline{
r: r,
t: t,
buf: NewRuneBuffer(t, prompt),
outchan: make(chan []rune),
history: list.New(),
}
go rl.ioloop()
return rl
}
func (l *Readline) ioloop() {
for {
r := l.t.ReadRune()
switch r {
case MetaNext:
l.buf.MoveToNextWord()
case MetaPrev:
l.buf.MoveToPrevWord()
case MetaDelete:
l.buf.DeleteWord()
case CharLineStart:
l.buf.MoveToLineStart()
case CharLineEnd:
l.buf.MoveToLineEnd()
case KeyDelete:
l.buf.Delete()
case CharEscape:
l.buf.BackEscape()
case CharEnter, CharEnter2:
l.buf.WriteRune('\n')
data := l.buf.Reset()
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:
buf := l.PrevHistory()
if buf != nil {
l.buf.Set(buf)
}
case CharNext:
buf := l.NextHistory()
if buf != nil {
l.buf.Set(buf)
}
case KeyInterrupt:
l.buf.WriteString("^C\n")
l.outchan <- nil
default:
l.buf.WriteRune(r)
}
l.UpdateHistory(l.buf.Runes())
}
}
func (l *Readline) Stderr() io.Writer {
return &wrapWriter{target: os.Stderr, r: l}
}
func (l *Readline) Readline() (string, error) {
r, err := l.ReadlineSlice()
if err != nil { if err != nil {
return "", err return nil, err
} }
return string(r), nil rl := t.Readline(prompt)
return &Instance{
t: t,
o: rl,
}, nil
} }
func (l *Readline) ReadlineSlice() ([]byte, error) { func (i *Instance) Stderr() io.Writer {
l.buf.Refresh(0, 0) return i.o.Stderr()
r := <-l.outchan }
if r == nil {
return nil, io.EOF func (i *Instance) Readline() (string, error) {
} return i.o.String()
return []byte(string(r)), nil }
func (i *Instance) ReadSlice() ([]byte, error) {
return i.o.Slice()
}
func (i *Instance) Close() error {
return i.t.Close()
} }

View File

@ -56,8 +56,8 @@ func (t *Terminal) PrintRune(r rune) {
fmt.Fprintf(os.Stdout, "%c", r) fmt.Fprintf(os.Stdout, "%c", r)
} }
func (t *Terminal) NewReadline(prompt string) *Readline { func (t *Terminal) Readline(prompt string) *Operation {
return newReadline(os.Stdin, t, prompt) return NewOperation(os.Stdin, t, prompt)
} }
func (t *Terminal) ReadRune() rune { func (t *Terminal) ReadRune() rune {