2015-09-21 08:30:10 +03:00
|
|
|
package readline
|
|
|
|
|
2015-09-30 09:16:46 +03:00
|
|
|
import (
|
2015-11-19 06:55:07 +03:00
|
|
|
"errors"
|
2015-09-30 09:16:46 +03:00
|
|
|
"io"
|
|
|
|
)
|
2015-09-21 08:30:10 +03:00
|
|
|
|
2015-11-19 06:55:07 +03:00
|
|
|
var (
|
|
|
|
ErrInterrupt = errors.New("Interrupt")
|
|
|
|
)
|
|
|
|
|
2016-03-25 12:08:13 +03:00
|
|
|
type InterruptError struct {
|
|
|
|
Line []rune
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*InterruptError) Error() string {
|
|
|
|
return "Interrupted"
|
|
|
|
}
|
|
|
|
|
2015-09-21 08:30:10 +03:00
|
|
|
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-11-19 06:55:07 +03:00
|
|
|
errchan chan error
|
2015-09-29 12:49:58 +03:00
|
|
|
w io.Writer
|
2015-09-24 19:16:49 +03:00
|
|
|
|
2016-02-16 11:55:19 +03:00
|
|
|
history *opHistory
|
2015-09-22 18:01:15 +03:00
|
|
|
*opSearch
|
2015-09-25 17:56:00 +03:00
|
|
|
*opCompleter
|
2015-11-20 15:56:42 +03:00
|
|
|
*opPassword
|
2015-10-01 17:44:43 +03:00
|
|
|
*opVim
|
2015-09-21 08:30:10 +03:00
|
|
|
}
|
|
|
|
|
2017-03-14 02:49:21 +03:00
|
|
|
func (o *Operation) SetBuffer(what string) {
|
|
|
|
o.buf.Set([]rune(what))
|
|
|
|
}
|
|
|
|
|
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) {
|
2015-09-28 19:26:49 +03:00
|
|
|
if !w.t.IsReading() {
|
|
|
|
return w.target.Write(b)
|
2015-09-24 19:16:49 +03:00
|
|
|
}
|
2015-09-28 19:26:49 +03:00
|
|
|
|
|
|
|
var (
|
|
|
|
n int
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
w.r.buf.Refresh(func() {
|
|
|
|
n, err = w.target.Write(b)
|
|
|
|
})
|
|
|
|
|
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 {
|
2016-03-13 13:32:48 +03:00
|
|
|
width := cfg.FuncGetWidth()
|
2015-09-21 08:30:10 +03:00
|
|
|
op := &Operation{
|
2015-10-04 10:23:00 +03:00
|
|
|
t: t,
|
2016-03-13 13:32:48 +03:00
|
|
|
buf: NewRuneBuffer(t, cfg.Prompt, cfg, width),
|
2015-10-04 10:23:00 +03:00
|
|
|
outchan: make(chan []rune),
|
2015-11-19 06:55:07 +03:00
|
|
|
errchan: make(chan error),
|
2015-09-21 08:30:10 +03:00
|
|
|
}
|
2015-09-29 12:49:58 +03:00
|
|
|
op.w = op.buf.w
|
2015-11-20 15:56:42 +03:00
|
|
|
op.SetConfig(cfg)
|
|
|
|
op.opVim = newVimMode(op)
|
2016-03-13 13:32:48 +03:00
|
|
|
op.opCompleter = newOpCompleter(op.buf.w, op, width)
|
2015-11-20 15:56:42 +03:00
|
|
|
op.opPassword = newOpPassword(op)
|
2016-03-13 13:32:48 +03:00
|
|
|
op.cfg.FuncOnWidthChanged(func() {
|
|
|
|
newWidth := cfg.FuncGetWidth()
|
|
|
|
op.opCompleter.OnWidthChange(newWidth)
|
|
|
|
op.opSearch.OnWidthChange(newWidth)
|
|
|
|
op.buf.OnWidthChange(newWidth)
|
|
|
|
})
|
2015-09-21 08:30:10 +03:00
|
|
|
go op.ioloop()
|
|
|
|
return op
|
|
|
|
}
|
|
|
|
|
2015-09-27 13:54:26 +03:00
|
|
|
func (o *Operation) SetPrompt(s string) {
|
|
|
|
o.buf.SetPrompt(s)
|
|
|
|
}
|
|
|
|
|
2015-11-20 15:56:42 +03:00
|
|
|
func (o *Operation) SetMaskRune(r rune) {
|
|
|
|
o.buf.SetMask(r)
|
|
|
|
}
|
|
|
|
|
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()
|
2017-02-20 04:51:01 +03:00
|
|
|
if o.cfg.FuncFilterInputRune != nil {
|
|
|
|
var process bool
|
|
|
|
r, process = o.cfg.FuncFilterInputRune(r)
|
|
|
|
if !process {
|
|
|
|
o.buf.Refresh(nil) // to refresh the line
|
|
|
|
continue // ignore this rune
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-17 09:08:55 +03:00
|
|
|
if r == 0 { // io.EOF
|
2016-06-19 18:06:37 +03:00
|
|
|
if o.buf.Len() == 0 {
|
|
|
|
o.buf.Clean()
|
|
|
|
select {
|
|
|
|
case o.errchan <- io.EOF:
|
|
|
|
}
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
// if stdin got io.EOF and there is something left in buffer,
|
|
|
|
// let's flush them by sending CharEnter.
|
|
|
|
// And we will got io.EOF int next loop.
|
|
|
|
r = CharEnter
|
2016-02-17 09:08:55 +03:00
|
|
|
}
|
|
|
|
}
|
2015-11-20 15:56:42 +03:00
|
|
|
isUpdateHistory := true
|
2015-09-25 17:56:00 +03:00
|
|
|
|
|
|
|
if o.IsInCompleteSelectMode() {
|
|
|
|
keepInCompleteMode = o.HandleCompleteSelect(r)
|
|
|
|
if keepInCompleteMode {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-09-27 05:12:15 +03:00
|
|
|
o.buf.Refresh(nil)
|
2015-09-25 17:56:00 +03:00
|
|
|
switch r {
|
2015-09-25 20:01:20 +03:00
|
|
|
case CharEnter, CharCtrlJ:
|
2016-02-16 11:55:19 +03:00
|
|
|
o.history.Update(o.buf.Runes(), false)
|
2015-09-25 20:01:20 +03:00
|
|
|
fallthrough
|
|
|
|
case CharInterrupt:
|
2015-09-25 17:56:00 +03:00
|
|
|
o.t.KickRead()
|
|
|
|
fallthrough
|
2015-10-01 17:44:43 +03:00
|
|
|
case CharBell:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.IsEnableVimMode() {
|
2015-10-02 05:37:21 +03:00
|
|
|
r = o.HandleVim(r, o.t.ReadRune)
|
|
|
|
if r == 0 {
|
2015-09-25 17:56:00 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-21 08:30:10 +03:00
|
|
|
switch r {
|
2015-10-01 17:44:43 +03:00
|
|
|
case CharBell:
|
2015-09-23 08:52:26 +03:00
|
|
|
if o.IsSearchMode() {
|
|
|
|
o.ExitSearchMode(true)
|
2015-09-27 05:12:15 +03:00
|
|
|
o.buf.Refresh(nil)
|
2015-09-22 18:01:15 +03:00
|
|
|
}
|
2015-09-25 17:56:00 +03:00
|
|
|
if o.IsInCompleteMode() {
|
|
|
|
o.ExitCompleteMode(true)
|
2015-09-27 05:12:15 +03:00
|
|
|
o.buf.Refresh(nil)
|
2015-09-25 17:56:00 +03:00
|
|
|
}
|
2015-09-25 07:59:36 +03:00
|
|
|
case CharTab:
|
2015-10-03 09:15:19 +03:00
|
|
|
if o.cfg.AutoComplete == nil {
|
|
|
|
o.t.Bell()
|
2015-09-25 07:59:36 +03:00
|
|
|
break
|
|
|
|
}
|
2016-08-31 18:51:28 +03:00
|
|
|
if o.OnComplete() {
|
|
|
|
keepInCompleteMode = true
|
|
|
|
} else {
|
|
|
|
o.t.Bell()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2015-09-22 18:01:15 +03:00
|
|
|
case CharBckSearch:
|
2016-08-31 18:51:28 +03:00
|
|
|
if !o.SearchMode(S_DIR_BCK) {
|
|
|
|
o.t.Bell()
|
|
|
|
break
|
|
|
|
}
|
2015-09-22 18:01:15 +03:00
|
|
|
keepInSearchMode = true
|
2015-09-28 06:13:39 +03:00
|
|
|
case CharCtrlU:
|
|
|
|
o.buf.KillFront()
|
2015-09-22 18:01:15 +03:00
|
|
|
case CharFwdSearch:
|
2016-08-31 18:51:28 +03:00
|
|
|
if !o.SearchMode(S_DIR_FWD) {
|
|
|
|
o.t.Bell()
|
|
|
|
break
|
|
|
|
}
|
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-10-01 17:44:43 +03:00
|
|
|
case MetaForward:
|
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-10-01 17:44:43 +03:00
|
|
|
case MetaBackward:
|
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 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
|
|
|
|
}
|
|
|
|
|
2015-09-25 20:01:20 +03:00
|
|
|
if o.buf.Len() == 0 {
|
2015-10-01 17:44:43 +03:00
|
|
|
o.t.Bell()
|
2015-09-25 20:01:20 +03:00
|
|
|
break
|
|
|
|
}
|
2015-09-25 17:56:00 +03:00
|
|
|
o.buf.Backspace()
|
|
|
|
if o.IsInCompleteMode() {
|
|
|
|
o.OnComplete()
|
2015-09-22 18:01:15 +03:00
|
|
|
}
|
2016-04-17 16:05:00 +03:00
|
|
|
case CharCtrlZ:
|
|
|
|
o.buf.Clean()
|
|
|
|
o.t.SleepToResume()
|
|
|
|
o.Refresh()
|
2016-05-16 12:59:35 +03:00
|
|
|
case CharCtrlL:
|
|
|
|
ClearScreen(o.w)
|
|
|
|
o.Refresh()
|
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()
|
2016-02-15 05:03:41 +03:00
|
|
|
var data []rune
|
|
|
|
if !o.cfg.UniqueEditLine {
|
|
|
|
o.buf.WriteRune('\n')
|
|
|
|
data = o.buf.Reset()
|
|
|
|
data = data[:len(data)-1] // trim \n
|
|
|
|
} else {
|
|
|
|
o.buf.Clean()
|
|
|
|
data = o.buf.Reset()
|
|
|
|
}
|
2015-09-23 08:52:26 +03:00
|
|
|
o.outchan <- data
|
2016-02-16 11:55:19 +03:00
|
|
|
if !o.cfg.DisableAutoSaveHistory {
|
2016-02-18 03:41:35 +03:00
|
|
|
// ignore IO error
|
|
|
|
_ = o.history.New(data)
|
2016-02-16 11:55:19 +03:00
|
|
|
} else {
|
|
|
|
isUpdateHistory = false
|
|
|
|
}
|
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:
|
2016-02-16 11:55:19 +03:00
|
|
|
buf := o.history.Prev()
|
2015-09-21 08:30:10 +03:00
|
|
|
if buf != nil {
|
2015-09-23 08:52:26 +03:00
|
|
|
o.buf.Set(buf)
|
2015-10-01 17:44:43 +03:00
|
|
|
} else {
|
|
|
|
o.t.Bell()
|
2015-09-21 08:30:10 +03:00
|
|
|
}
|
|
|
|
case CharNext:
|
2016-02-16 11:55:19 +03:00
|
|
|
buf, ok := o.history.Next()
|
2015-09-21 16:00:48 +03:00
|
|
|
if ok {
|
2015-09-23 08:52:26 +03:00
|
|
|
o.buf.Set(buf)
|
2015-10-01 17:44:43 +03:00
|
|
|
} else {
|
|
|
|
o.t.Bell()
|
2015-09-21 08:30:10 +03:00
|
|
|
}
|
2015-11-19 06:55:07 +03:00
|
|
|
case CharDelete:
|
|
|
|
if o.buf.Len() > 0 || !o.IsNormalMode() {
|
|
|
|
o.t.KickRead()
|
|
|
|
if !o.buf.Delete() {
|
|
|
|
o.t.Bell()
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// treat as EOF
|
2016-03-31 05:55:53 +03:00
|
|
|
if !o.cfg.UniqueEditLine {
|
|
|
|
o.buf.WriteString(o.cfg.EOFPrompt + "\n")
|
|
|
|
}
|
2015-11-20 06:25:30 +03:00
|
|
|
o.buf.Reset()
|
2015-11-20 15:56:42 +03:00
|
|
|
isUpdateHistory = false
|
2016-02-16 11:55:19 +03:00
|
|
|
o.history.Revert()
|
2015-11-19 06:55:07 +03:00
|
|
|
o.errchan <- io.EOF
|
2016-03-31 05:55:53 +03:00
|
|
|
if o.cfg.UniqueEditLine {
|
|
|
|
o.buf.Clean()
|
|
|
|
}
|
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)
|
2015-09-27 05:12:15 +03:00
|
|
|
o.buf.Refresh(nil)
|
2015-09-25 17:56:00 +03:00
|
|
|
break
|
|
|
|
}
|
2015-09-23 08:52:26 +03:00
|
|
|
o.buf.MoveToLineEnd()
|
2015-09-27 05:12:15 +03:00
|
|
|
o.buf.Refresh(nil)
|
2016-03-25 12:08:13 +03:00
|
|
|
hint := o.cfg.InterruptPrompt + "\n"
|
2016-03-31 05:55:53 +03:00
|
|
|
if !o.cfg.UniqueEditLine {
|
|
|
|
o.buf.WriteString(hint)
|
|
|
|
}
|
2016-03-25 12:08:13 +03:00
|
|
|
remain := o.buf.Reset()
|
2016-03-31 05:55:53 +03:00
|
|
|
if !o.cfg.UniqueEditLine {
|
|
|
|
remain = remain[:len(remain)-len([]rune(hint))]
|
|
|
|
}
|
2015-11-20 15:56:42 +03:00
|
|
|
isUpdateHistory = false
|
2016-02-16 11:55:19 +03:00
|
|
|
o.history.Revert()
|
2016-03-25 12:08:13 +03:00
|
|
|
o.errchan <- &InterruptError{remain}
|
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-28 06:13:39 +03:00
|
|
|
|
2015-11-20 15:56:42 +03:00
|
|
|
if o.cfg.Listener != nil {
|
|
|
|
newLine, newPos, ok := o.cfg.Listener.OnChange(o.buf.Runes(), o.buf.Pos(), r)
|
|
|
|
if ok {
|
|
|
|
o.buf.SetWithIdx(newPos, newLine)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 08:52:26 +03:00
|
|
|
if !keepInSearchMode && o.IsSearchMode() {
|
|
|
|
o.ExitSearchMode(false)
|
2015-09-27 05:12:15 +03:00
|
|
|
o.buf.Refresh(nil)
|
2015-09-25 17:56:00 +03:00
|
|
|
} else if o.IsInCompleteMode() {
|
|
|
|
if !keepInCompleteMode {
|
|
|
|
o.ExitCompleteMode(false)
|
2015-11-20 15:56:42 +03:00
|
|
|
o.Refresh()
|
2015-09-25 17:56:00 +03:00
|
|
|
} else {
|
2015-09-27 05:12:15 +03:00
|
|
|
o.buf.Refresh(nil)
|
2015-09-25 17:56:00 +03:00
|
|
|
o.CompleteRefresh()
|
|
|
|
}
|
2015-09-21 08:30:10 +03:00
|
|
|
}
|
2015-11-20 15:56:42 +03:00
|
|
|
if isUpdateHistory && !o.IsSearchMode() {
|
2016-02-16 11:55:19 +03:00
|
|
|
// it will cause null history
|
|
|
|
o.history.Update(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-27 04:04:50 +03:00
|
|
|
return &wrapWriter{target: o.cfg.Stderr, r: o, t: o.t}
|
2015-09-24 19:16:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Operation) Stdout() io.Writer {
|
2015-09-27 04:04:50 +03:00
|
|
|
return &wrapWriter{target: o.cfg.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()
|
2016-03-25 12:08:13 +03:00
|
|
|
return string(r), err
|
2015-09-21 08:30:10 +03:00
|
|
|
}
|
|
|
|
|
2015-09-23 08:52:26 +03:00
|
|
|
func (o *Operation) Runes() ([]rune, error) {
|
2015-11-09 06:20:26 +03:00
|
|
|
o.t.EnterRawMode()
|
|
|
|
defer o.t.ExitRawMode()
|
|
|
|
|
2015-11-20 15:56:42 +03:00
|
|
|
if o.cfg.Listener != nil {
|
|
|
|
o.cfg.Listener.OnChange(nil, 0, 0)
|
|
|
|
}
|
2016-08-31 18:51:28 +03:00
|
|
|
|
2015-09-27 05:12:15 +03:00
|
|
|
o.buf.Refresh(nil) // print prompt
|
2015-09-24 19:16:49 +03:00
|
|
|
o.t.KickRead()
|
2015-11-19 06:55:07 +03:00
|
|
|
select {
|
|
|
|
case r := <-o.outchan:
|
|
|
|
return r, nil
|
|
|
|
case err := <-o.errchan:
|
2016-03-25 12:08:13 +03:00
|
|
|
if e, ok := err.(*InterruptError); ok {
|
|
|
|
return e.Line, ErrInterrupt
|
|
|
|
}
|
2015-11-19 06:55:07 +03:00
|
|
|
return nil, err
|
2015-09-21 08:30:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-20 15:56:42 +03:00
|
|
|
func (o *Operation) PasswordEx(prompt string, l Listener) ([]byte, error) {
|
|
|
|
cfg := o.GenPasswordConfig()
|
|
|
|
cfg.Prompt = prompt
|
|
|
|
cfg.Listener = l
|
|
|
|
return o.PasswordWithConfig(cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Operation) GenPasswordConfig() *Config {
|
|
|
|
return o.opPassword.PasswordConfig()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Operation) PasswordWithConfig(cfg *Config) ([]byte, error) {
|
2015-11-20 16:00:05 +03:00
|
|
|
if err := o.opPassword.EnterPasswordMode(cfg); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-20 15:56:42 +03:00
|
|
|
defer o.opPassword.ExitPasswordMode()
|
|
|
|
return o.Slice()
|
|
|
|
}
|
|
|
|
|
2015-09-30 09:16:46 +03:00
|
|
|
func (o *Operation) Password(prompt string) ([]byte, error) {
|
2016-03-05 05:46:11 +03:00
|
|
|
return o.PasswordEx(prompt, nil)
|
2015-09-30 09:16:46 +03:00
|
|
|
}
|
|
|
|
|
2015-09-29 12:49:58 +03:00
|
|
|
func (o *Operation) SetTitle(t string) {
|
|
|
|
o.w.Write([]byte("\033[2;" + t + "\007"))
|
|
|
|
}
|
|
|
|
|
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() {
|
2016-02-16 11:55:19 +03:00
|
|
|
o.history.Close()
|
2015-10-04 10:23:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Operation) SetHistoryPath(path string) {
|
2016-02-16 11:55:19 +03:00
|
|
|
if o.history != nil {
|
|
|
|
o.history.Close()
|
2015-10-04 10:23:00 +03:00
|
|
|
}
|
|
|
|
o.cfg.HistoryFile = path
|
2016-02-16 11:55:19 +03:00
|
|
|
o.history = newOpHistory(o.cfg)
|
2015-09-22 13:16:24 +03:00
|
|
|
}
|
2015-11-19 06:55:07 +03:00
|
|
|
|
|
|
|
func (o *Operation) IsNormalMode() bool {
|
|
|
|
return !o.IsInCompleteMode() && !o.IsSearchMode()
|
|
|
|
}
|
2015-11-20 15:56:42 +03:00
|
|
|
|
|
|
|
func (op *Operation) SetConfig(cfg *Config) (*Config, error) {
|
|
|
|
if op.cfg == cfg {
|
|
|
|
return op.cfg, nil
|
|
|
|
}
|
|
|
|
if err := cfg.Init(); err != nil {
|
|
|
|
return op.cfg, err
|
|
|
|
}
|
|
|
|
old := op.cfg
|
|
|
|
op.cfg = cfg
|
|
|
|
op.SetPrompt(cfg.Prompt)
|
|
|
|
op.SetMaskRune(cfg.MaskRune)
|
2016-02-18 06:25:41 +03:00
|
|
|
op.buf.SetConfig(cfg)
|
2016-03-13 13:32:48 +03:00
|
|
|
width := op.cfg.FuncGetWidth()
|
2015-11-20 15:56:42 +03:00
|
|
|
|
|
|
|
if cfg.opHistory == nil {
|
|
|
|
op.SetHistoryPath(cfg.HistoryFile)
|
2016-02-16 11:55:19 +03:00
|
|
|
cfg.opHistory = op.history
|
2016-03-13 13:32:48 +03:00
|
|
|
cfg.opSearch = newOpSearch(op.buf.w, op.buf, op.history, cfg, width)
|
2015-11-20 15:56:42 +03:00
|
|
|
}
|
2016-02-16 11:55:19 +03:00
|
|
|
op.history = cfg.opHistory
|
2015-11-20 15:56:42 +03:00
|
|
|
|
|
|
|
// SetHistoryPath will close opHistory which already exists
|
|
|
|
// so if we use it next time, we need to reopen it by `InitHistory()`
|
2016-02-16 11:55:19 +03:00
|
|
|
op.history.Init()
|
2015-11-20 15:56:42 +03:00
|
|
|
|
2016-02-23 07:29:23 +03:00
|
|
|
if op.cfg.AutoComplete != nil {
|
2016-03-13 13:32:48 +03:00
|
|
|
op.opCompleter = newOpCompleter(op.buf.w, op, width)
|
2016-02-23 07:29:23 +03:00
|
|
|
}
|
|
|
|
|
2015-11-20 15:56:42 +03:00
|
|
|
op.opSearch = cfg.opSearch
|
|
|
|
return old, nil
|
|
|
|
}
|
|
|
|
|
2016-03-31 05:55:53 +03:00
|
|
|
func (o *Operation) ResetHistory() {
|
|
|
|
o.history.Reset()
|
|
|
|
}
|
|
|
|
|
2016-02-18 03:41:35 +03:00
|
|
|
// if err is not nil, it just mean it fail to write to file
|
|
|
|
// other things goes fine.
|
|
|
|
func (o *Operation) SaveHistory(content string) error {
|
|
|
|
return o.history.New([]rune(content))
|
2016-02-16 11:55:19 +03:00
|
|
|
}
|
|
|
|
|
2015-11-20 15:56:42 +03:00
|
|
|
func (o *Operation) Refresh() {
|
|
|
|
if o.t.IsReading() {
|
|
|
|
o.buf.Refresh(nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-31 05:55:53 +03:00
|
|
|
func (o *Operation) Clean() {
|
|
|
|
o.buf.Clean()
|
|
|
|
}
|
|
|
|
|
2015-11-20 15:56:42 +03:00
|
|
|
func FuncListener(f func(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool)) Listener {
|
|
|
|
return &DumpListener{f: f}
|
|
|
|
}
|
|
|
|
|
|
|
|
type DumpListener struct {
|
|
|
|
f func(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DumpListener) OnChange(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool) {
|
|
|
|
return d.f(line, pos, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Listener interface {
|
|
|
|
OnChange(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool)
|
|
|
|
}
|