readline/operation.go

139 lines
2.4 KiB
Go
Raw Normal View History

2015-09-21 08:30:10 +03:00
package readline
import (
"container/list"
"io"
"os"
)
type Operation struct {
r *os.File
t *Terminal
buf *RuneBuffer
outchan chan []rune
2015-09-21 16:00:48 +03:00
history *list.List
historyVer int64
current *list.Element
2015-09-21 08:30:10 +03:00
}
const (
CharLineStart = 0x1
CharLineEnd = 0x5
2015-09-21 17:51:48 +03:00
CharKill = 11
2015-09-21 08:30:10 +03:00
CharNext = 0xe
CharPrev = 0x10
CharBackward = 0x2
CharForward = 0x6
2015-09-21 16:00:48 +03:00
CharBackspace = 0x7f
2015-09-21 08:30:10 +03:00
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)
2015-09-22 12:41:14 +03:00
w.r.buf.Refresh()
2015-09-21 08:30:10 +03:00
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 {
2015-09-21 17:51:48 +03:00
case CharKill:
l.buf.Kill()
2015-09-21 08:30:10 +03:00
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()
2015-09-21 16:00:48 +03:00
case CharBackspace:
l.buf.Backspace()
case MetaBackspace:
l.buf.BackEscapeWord()
2015-09-21 08:30:10 +03:00
case CharEnter, CharEnter2:
l.buf.MoveToLineEnd()
2015-09-21 08:30:10 +03:00
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:
2015-09-21 16:00:48 +03:00
buf, ok := l.NextHistory()
if ok {
2015-09-21 08:30:10 +03:00
l.buf.Set(buf)
}
case KeyInterrupt:
l.buf.WriteString("^C\n")
l.outchan <- nil
default:
l.buf.WriteRune(r)
}
2015-09-21 16:00:48 +03:00
l.UpdateHistory(l.buf.Runes(), false)
2015-09-21 08:30:10 +03:00
}
}
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) {
2015-09-22 12:41:14 +03:00
l.buf.Refresh() // print prompt
2015-09-21 08:30:10 +03:00
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
}