2015-09-21 08:30:10 +03:00
|
|
|
package readline
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Operation struct {
|
2015-09-22 13:16:24 +03:00
|
|
|
cfg *Config
|
2015-09-21 08:30:10 +03:00
|
|
|
t *Terminal
|
|
|
|
buf *RuneBuffer
|
|
|
|
outchan chan []rune
|
2015-09-22 13:16:24 +03:00
|
|
|
*opHistory
|
2015-09-22 18:01:15 +03:00
|
|
|
*opSearch
|
2015-09-21 08:30:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2015-09-22 18:01:15 +03:00
|
|
|
CharLineStart = 1
|
|
|
|
CharLineEnd = 5
|
2015-09-21 17:51:48 +03:00
|
|
|
CharKill = 11
|
2015-09-22 18:01:15 +03:00
|
|
|
CharNext = 14
|
|
|
|
CharPrev = 16
|
|
|
|
CharBackward = 2
|
|
|
|
CharForward = 6
|
2015-09-21 16:00:48 +03:00
|
|
|
CharBackspace = 0x7f
|
2015-09-21 08:30:10 +03:00
|
|
|
CharEnter = 0xd
|
|
|
|
CharEnter2 = 0xa
|
2015-09-22 18:01:15 +03:00
|
|
|
CharBckSearch = 18
|
|
|
|
CharFwdSearch = 19
|
|
|
|
CharCannel = 7
|
2015-09-21 08:30:10 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-09-22 13:16:24 +03:00
|
|
|
func NewOperation(t *Terminal, cfg *Config) *Operation {
|
2015-09-21 08:30:10 +03:00
|
|
|
op := &Operation{
|
2015-09-22 13:16:24 +03:00
|
|
|
cfg: cfg,
|
|
|
|
t: t,
|
|
|
|
buf: NewRuneBuffer(t, cfg.Prompt),
|
|
|
|
outchan: make(chan []rune),
|
|
|
|
opHistory: newOpHistory(cfg.HistoryFile),
|
2015-09-21 08:30:10 +03:00
|
|
|
}
|
2015-09-22 18:01:15 +03:00
|
|
|
op.opSearch = newOpSearch(op.buf.w, op.buf, op.opHistory)
|
2015-09-21 08:30:10 +03:00
|
|
|
go op.ioloop()
|
|
|
|
return op
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Operation) ioloop() {
|
|
|
|
for {
|
2015-09-22 18:01:15 +03:00
|
|
|
keepInSearchMode := false
|
2015-09-21 08:30:10 +03:00
|
|
|
r := l.t.ReadRune()
|
|
|
|
switch r {
|
2015-09-22 18:01:15 +03:00
|
|
|
case CharCannel:
|
|
|
|
if l.IsSearchMode() {
|
|
|
|
l.ExitSearchMode(true)
|
|
|
|
l.buf.Refresh()
|
|
|
|
}
|
|
|
|
case CharBckSearch:
|
|
|
|
l.SearchMode(S_DIR_BCK)
|
|
|
|
keepInSearchMode = true
|
|
|
|
case CharFwdSearch:
|
|
|
|
l.SearchMode(S_DIR_FWD)
|
|
|
|
keepInSearchMode = true
|
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:
|
2015-09-22 18:01:15 +03:00
|
|
|
if l.IsSearchMode() {
|
|
|
|
l.SearchBackspace()
|
|
|
|
keepInSearchMode = true
|
|
|
|
} else {
|
|
|
|
l.buf.Backspace()
|
|
|
|
}
|
2015-09-21 16:00:48 +03:00
|
|
|
case MetaBackspace:
|
|
|
|
l.buf.BackEscapeWord()
|
2015-09-21 08:30:10 +03:00
|
|
|
case CharEnter, CharEnter2:
|
2015-09-23 06:10:36 +03:00
|
|
|
if l.IsSearchMode() {
|
|
|
|
l.ExitSearchMode(false)
|
|
|
|
}
|
2015-09-21 16:14:05 +03:00
|
|
|
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:
|
2015-09-22 18:01:15 +03:00
|
|
|
if l.IsSearchMode() {
|
|
|
|
l.ExitSearchMode(false)
|
|
|
|
}
|
|
|
|
l.buf.MoveToLineEnd()
|
|
|
|
l.buf.Refresh()
|
2015-09-21 08:30:10 +03:00
|
|
|
l.buf.WriteString("^C\n")
|
|
|
|
l.outchan <- nil
|
|
|
|
default:
|
2015-09-22 18:01:15 +03:00
|
|
|
if l.IsSearchMode() {
|
|
|
|
l.SearchChar(r)
|
|
|
|
keepInSearchMode = true
|
|
|
|
} else {
|
|
|
|
l.buf.WriteRune(r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !keepInSearchMode && l.IsSearchMode() {
|
|
|
|
l.ExitSearchMode(false)
|
|
|
|
l.buf.Refresh()
|
2015-09-21 08:30:10 +03:00
|
|
|
}
|
2015-09-23 06:10:36 +03:00
|
|
|
if !l.IsSearchMode() {
|
|
|
|
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
|
|
|
|
}
|
2015-09-22 13:16:24 +03:00
|
|
|
|
|
|
|
func (l *Operation) Close() {
|
|
|
|
l.opHistory.Close()
|
|
|
|
}
|