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-24 19:16:49 +03:00
|
|
|
|
2015-09-22 13:16:24 +03:00
|
|
|
*opHistory
|
2015-09-22 18:01:15 +03:00
|
|
|
*opSearch
|
2015-09-25 17:56:00 +03:00
|
|
|
*opCompleter
|
2015-09-21 08:30:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type wrapWriter struct {
|
|
|
|
r *Operation
|
2015-09-24 19:16:49 +03:00
|
|
|
t *Terminal
|
2015-09-21 08:30:10 +03:00
|
|
|
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-24 19:16:49 +03:00
|
|
|
if w.t.IsReading() {
|
|
|
|
w.r.buf.Refresh()
|
|
|
|
}
|
2015-09-23 09:04:30 +03:00
|
|
|
if w.r.IsSearchMode() {
|
|
|
|
w.r.SearchRefresh(-1)
|
|
|
|
}
|
2015-09-25 17:56:00 +03:00
|
|
|
if w.r.IsInCompleteMode() {
|
|
|
|
w.r.CompleteRefresh()
|
|
|
|
}
|
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-25 17:56:00 +03:00
|
|
|
op.opCompleter = newOpCompleter(op.buf.w, op)
|
2015-09-21 08:30:10 +03:00
|
|
|
go op.ioloop()
|
|
|
|
return op
|
|
|
|
}
|
|
|
|
|
2015-09-23 08:52:26 +03:00
|
|
|
func (o *Operation) ioloop() {
|
2015-09-21 08:30:10 +03:00
|
|
|
for {
|
2015-09-22 18:01:15 +03:00
|
|
|
keepInSearchMode := false
|
2015-09-25 17:56:00 +03:00
|
|
|
keepInCompleteMode := false
|
2015-09-23 08:52:26 +03:00
|
|
|
r := o.t.ReadRune()
|
2015-09-25 17:56:00 +03:00
|
|
|
|
|
|
|
if o.IsInCompleteSelectMode() {
|
|
|
|
keepInCompleteMode = o.HandleCompleteSelect(r)
|
|
|
|
if keepInCompleteMode {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
o.buf.Refresh()
|
|
|
|
switch r {
|
|
|
|
case CharInterrupt, CharEnter, CharCtrlJ:
|
|
|
|
o.t.KickRead()
|
|
|
|
fallthrough
|
|
|
|
case CharCancel:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-21 08:30:10 +03:00
|
|
|
switch r {
|
2015-09-25 17:56:00 +03:00
|
|
|
case CharCancel:
|
2015-09-23 08:52:26 +03:00
|
|
|
if o.IsSearchMode() {
|
|
|
|
o.ExitSearchMode(true)
|
|
|
|
o.buf.Refresh()
|
2015-09-22 18:01:15 +03:00
|
|
|
}
|
2015-09-25 17:56:00 +03:00
|
|
|
if o.IsInCompleteMode() {
|
|
|
|
o.ExitCompleteMode(true)
|
|
|
|
o.buf.Refresh()
|
|
|
|
}
|
2015-09-25 07:59:36 +03:00
|
|
|
case CharTab:
|
2015-09-25 17:56:00 +03:00
|
|
|
if o.opCompleter == nil {
|
2015-09-25 07:59:36 +03:00
|
|
|
break
|
|
|
|
}
|
2015-09-25 17:56:00 +03:00
|
|
|
o.OnComplete()
|
|
|
|
keepInCompleteMode = true
|
2015-09-22 18:01:15 +03:00
|
|
|
case CharBckSearch:
|
2015-09-23 08:52:26 +03:00
|
|
|
o.SearchMode(S_DIR_BCK)
|
2015-09-22 18:01:15 +03:00
|
|
|
keepInSearchMode = true
|
|
|
|
case CharFwdSearch:
|
2015-09-23 08:52:26 +03:00
|
|
|
o.SearchMode(S_DIR_FWD)
|
2015-09-22 18:01:15 +03:00
|
|
|
keepInSearchMode = true
|
2015-09-21 17:51:48 +03:00
|
|
|
case CharKill:
|
2015-09-23 08:52:26 +03:00
|
|
|
o.buf.Kill()
|
2015-09-25 17:56:00 +03:00
|
|
|
keepInCompleteMode = true
|
2015-09-21 08:30:10 +03:00
|
|
|
case MetaNext:
|
2015-09-23 08:52:26 +03:00
|
|
|
o.buf.MoveToNextWord()
|
2015-09-23 08:03:13 +03:00
|
|
|
case CharTranspose:
|
2015-09-23 08:52:26 +03:00
|
|
|
o.buf.Transpose()
|
2015-09-21 08:30:10 +03:00
|
|
|
case MetaPrev:
|
2015-09-23 08:52:26 +03:00
|
|
|
o.buf.MoveToPrevWord()
|
2015-09-21 08:30:10 +03:00
|
|
|
case MetaDelete:
|
2015-09-23 08:52:26 +03:00
|
|
|
o.buf.DeleteWord()
|
2015-09-21 08:30:10 +03:00
|
|
|
case CharLineStart:
|
2015-09-23 08:52:26 +03:00
|
|
|
o.buf.MoveToLineStart()
|
2015-09-21 08:30:10 +03:00
|
|
|
case CharLineEnd:
|
2015-09-23 08:52:26 +03:00
|
|
|
o.buf.MoveToLineEnd()
|
2015-09-23 06:46:56 +03:00
|
|
|
case CharDelete:
|
2015-09-23 08:52:26 +03:00
|
|
|
o.buf.Delete()
|
2015-09-23 06:46:56 +03:00
|
|
|
case CharBackspace, CharCtrlH:
|
2015-09-23 08:52:26 +03:00
|
|
|
if o.IsSearchMode() {
|
|
|
|
o.SearchBackspace()
|
2015-09-22 18:01:15 +03:00
|
|
|
keepInSearchMode = true
|
2015-09-25 17:56:00 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
o.buf.Backspace()
|
|
|
|
if o.IsInCompleteMode() {
|
|
|
|
o.OnComplete()
|
2015-09-22 18:01:15 +03:00
|
|
|
}
|
2015-09-23 06:46:56 +03:00
|
|
|
case MetaBackspace, CharCtrlW:
|
2015-09-23 08:52:26 +03:00
|
|
|
o.buf.BackEscapeWord()
|
2015-09-23 06:46:56 +03:00
|
|
|
case CharEnter, CharCtrlJ:
|
2015-09-23 08:52:26 +03:00
|
|
|
if o.IsSearchMode() {
|
|
|
|
o.ExitSearchMode(false)
|
2015-09-23 06:10:36 +03:00
|
|
|
}
|
2015-09-23 08:52:26 +03:00
|
|
|
o.buf.MoveToLineEnd()
|
|
|
|
o.buf.WriteRune('\n')
|
|
|
|
data := o.buf.Reset()
|
2015-09-21 08:30:10 +03:00
|
|
|
data = data[:len(data)-1] // trim \n
|
2015-09-23 08:52:26 +03:00
|
|
|
o.outchan <- data
|
|
|
|
o.NewHistory(data)
|
2015-09-21 08:30:10 +03:00
|
|
|
case CharBackward:
|
2015-09-23 08:52:26 +03:00
|
|
|
o.buf.MoveBackward()
|
2015-09-21 08:30:10 +03:00
|
|
|
case CharForward:
|
2015-09-23 08:52:26 +03:00
|
|
|
o.buf.MoveForward()
|
2015-09-21 08:30:10 +03:00
|
|
|
case CharPrev:
|
2015-09-23 08:52:26 +03:00
|
|
|
buf := o.PrevHistory()
|
2015-09-21 08:30:10 +03:00
|
|
|
if buf != nil {
|
2015-09-23 08:52:26 +03:00
|
|
|
o.buf.Set(buf)
|
2015-09-21 08:30:10 +03:00
|
|
|
}
|
|
|
|
case CharNext:
|
2015-09-23 08:52:26 +03:00
|
|
|
buf, ok := o.NextHistory()
|
2015-09-21 16:00:48 +03:00
|
|
|
if ok {
|
2015-09-23 08:52:26 +03:00
|
|
|
o.buf.Set(buf)
|
2015-09-21 08:30:10 +03:00
|
|
|
}
|
2015-09-23 06:46:56 +03:00
|
|
|
case CharInterrupt:
|
2015-09-23 08:52:26 +03:00
|
|
|
if o.IsSearchMode() {
|
2015-09-24 19:49:55 +03:00
|
|
|
o.t.KickRead()
|
|
|
|
o.ExitSearchMode(true)
|
|
|
|
break
|
2015-09-22 18:01:15 +03:00
|
|
|
}
|
2015-09-25 17:56:00 +03:00
|
|
|
if o.IsInCompleteMode() {
|
|
|
|
o.t.KickRead()
|
|
|
|
o.ExitCompleteMode(true)
|
|
|
|
o.buf.Refresh()
|
|
|
|
break
|
|
|
|
}
|
2015-09-23 08:52:26 +03:00
|
|
|
o.buf.MoveToLineEnd()
|
|
|
|
o.buf.Refresh()
|
|
|
|
o.buf.WriteString("^C\n")
|
|
|
|
o.outchan <- nil
|
2015-09-21 08:30:10 +03:00
|
|
|
default:
|
2015-09-23 08:52:26 +03:00
|
|
|
if o.IsSearchMode() {
|
|
|
|
o.SearchChar(r)
|
2015-09-22 18:01:15 +03:00
|
|
|
keepInSearchMode = true
|
2015-09-25 17:56:00 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
o.buf.WriteRune(r)
|
|
|
|
if o.IsInCompleteMode() {
|
|
|
|
o.OnComplete()
|
|
|
|
keepInCompleteMode = true
|
2015-09-22 18:01:15 +03:00
|
|
|
}
|
|
|
|
}
|
2015-09-23 08:52:26 +03:00
|
|
|
if !keepInSearchMode && o.IsSearchMode() {
|
|
|
|
o.ExitSearchMode(false)
|
|
|
|
o.buf.Refresh()
|
2015-09-25 17:56:00 +03:00
|
|
|
} else if o.IsInCompleteMode() {
|
|
|
|
if !keepInCompleteMode {
|
|
|
|
o.ExitCompleteMode(false)
|
|
|
|
o.buf.Refresh()
|
|
|
|
} else {
|
|
|
|
o.buf.Refresh()
|
|
|
|
o.CompleteRefresh()
|
|
|
|
}
|
2015-09-21 08:30:10 +03:00
|
|
|
}
|
2015-09-23 08:52:26 +03:00
|
|
|
if !o.IsSearchMode() {
|
|
|
|
o.UpdateHistory(o.buf.Runes(), false)
|
2015-09-23 06:10:36 +03:00
|
|
|
}
|
2015-09-21 08:30:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 08:52:26 +03:00
|
|
|
func (o *Operation) Stderr() io.Writer {
|
2015-09-24 19:16:49 +03:00
|
|
|
return &wrapWriter{target: os.Stderr, r: o, t: o.t}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Operation) Stdout() io.Writer {
|
|
|
|
return &wrapWriter{target: os.Stdout, r: o, t: o.t}
|
2015-09-21 08:30:10 +03:00
|
|
|
}
|
|
|
|
|
2015-09-23 08:52:26 +03:00
|
|
|
func (o *Operation) String() (string, error) {
|
|
|
|
r, err := o.Runes()
|
2015-09-21 08:30:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(r), nil
|
|
|
|
}
|
|
|
|
|
2015-09-23 08:52:26 +03:00
|
|
|
func (o *Operation) Runes() ([]rune, error) {
|
|
|
|
o.buf.Refresh() // print prompt
|
2015-09-24 19:16:49 +03:00
|
|
|
o.t.KickRead()
|
2015-09-23 08:52:26 +03:00
|
|
|
r := <-o.outchan
|
2015-09-21 08:30:10 +03:00
|
|
|
if r == nil {
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2015-09-23 08:52:26 +03:00
|
|
|
func (o *Operation) Slice() ([]byte, error) {
|
|
|
|
r, err := o.Runes()
|
2015-09-21 08:30:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return []byte(string(r)), nil
|
|
|
|
}
|
2015-09-22 13:16:24 +03:00
|
|
|
|
2015-09-23 08:52:26 +03:00
|
|
|
func (o *Operation) Close() {
|
|
|
|
o.opHistory.Close()
|
2015-09-22 13:16:24 +03:00
|
|
|
}
|