2015-09-28 19:26:49 +03:00
|
|
|
package readline
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
2016-02-18 06:25:41 +03:00
|
|
|
"sync"
|
2016-10-06 07:30:22 +03:00
|
|
|
"sync/atomic"
|
2015-09-28 19:26:49 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-09-04 16:02:24 +03:00
|
|
|
Stdin io.ReadCloser = os.Stdin
|
2015-09-29 12:49:58 +03:00
|
|
|
Stdout io.WriteCloser = os.Stdout
|
|
|
|
Stderr io.WriteCloser = os.Stderr
|
2015-09-28 19:26:49 +03:00
|
|
|
)
|
2016-02-18 06:25:41 +03:00
|
|
|
|
|
|
|
var (
|
|
|
|
std *Instance
|
|
|
|
stdOnce sync.Once
|
|
|
|
)
|
|
|
|
|
2016-03-05 05:46:11 +03:00
|
|
|
// global instance will not submit history automatic
|
2016-02-18 06:25:41 +03:00
|
|
|
func getInstance() *Instance {
|
|
|
|
stdOnce.Do(func() {
|
|
|
|
std, _ = NewEx(&Config{
|
|
|
|
DisableAutoSaveHistory: true,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return std
|
|
|
|
}
|
|
|
|
|
2016-03-05 05:46:11 +03:00
|
|
|
// let readline load history from filepath
|
|
|
|
// and try to persist history into disk
|
|
|
|
// set fp to "" to prevent readline persisting history to disk
|
|
|
|
// so the `AddHistory` will return nil error forever.
|
2016-02-18 06:25:41 +03:00
|
|
|
func SetHistoryPath(fp string) {
|
|
|
|
ins := getInstance()
|
|
|
|
cfg := ins.Config.Clone()
|
|
|
|
cfg.HistoryFile = fp
|
|
|
|
ins.SetConfig(cfg)
|
|
|
|
}
|
|
|
|
|
2016-03-05 05:46:11 +03:00
|
|
|
// set auto completer to global instance
|
2016-02-23 07:29:23 +03:00
|
|
|
func SetAutoComplete(completer AutoCompleter) {
|
|
|
|
ins := getInstance()
|
|
|
|
cfg := ins.Config.Clone()
|
|
|
|
cfg.AutoComplete = completer
|
|
|
|
ins.SetConfig(cfg)
|
|
|
|
}
|
|
|
|
|
2016-03-05 05:46:11 +03:00
|
|
|
// add history to global instance manually
|
|
|
|
// raise error only if `SetHistoryPath` is set with a non-empty path
|
2016-02-18 06:25:41 +03:00
|
|
|
func AddHistory(content string) error {
|
|
|
|
ins := getInstance()
|
|
|
|
return ins.SaveHistory(content)
|
|
|
|
}
|
|
|
|
|
2016-04-01 12:54:50 +03:00
|
|
|
func Password(prompt string) ([]byte, error) {
|
|
|
|
ins := getInstance()
|
|
|
|
return ins.ReadPassword(prompt)
|
|
|
|
}
|
|
|
|
|
2016-03-05 05:46:11 +03:00
|
|
|
// readline with global configs
|
2016-02-18 06:25:41 +03:00
|
|
|
func Line(prompt string) (string, error) {
|
|
|
|
ins := getInstance()
|
|
|
|
ins.SetPrompt(prompt)
|
|
|
|
return ins.Readline()
|
|
|
|
}
|
2016-09-01 13:13:06 +03:00
|
|
|
|
|
|
|
type CancelableStdin struct {
|
2016-09-04 16:05:21 +03:00
|
|
|
r io.Reader
|
|
|
|
mutex sync.Mutex
|
|
|
|
stop chan struct{}
|
2016-10-06 07:30:22 +03:00
|
|
|
closed int32
|
2016-09-04 16:05:21 +03:00
|
|
|
notify chan struct{}
|
|
|
|
data []byte
|
|
|
|
read int
|
|
|
|
err error
|
2016-09-01 13:13:06 +03:00
|
|
|
}
|
|
|
|
|
2016-09-04 16:02:24 +03:00
|
|
|
func NewCancelableStdin(r io.Reader) *CancelableStdin {
|
2016-09-01 13:13:06 +03:00
|
|
|
c := &CancelableStdin{
|
2016-09-04 16:02:24 +03:00
|
|
|
r: r,
|
2016-09-01 13:13:06 +03:00
|
|
|
notify: make(chan struct{}),
|
|
|
|
stop: make(chan struct{}),
|
|
|
|
}
|
2016-09-04 16:02:24 +03:00
|
|
|
go c.ioloop()
|
2016-09-01 13:13:06 +03:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CancelableStdin) ioloop() {
|
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-c.notify:
|
2016-09-04 16:02:24 +03:00
|
|
|
c.read, c.err = c.r.Read(c.data)
|
2016-10-06 07:31:47 +03:00
|
|
|
select {
|
|
|
|
case c.notify <- struct{}{}:
|
|
|
|
case <-c.stop:
|
|
|
|
break loop
|
|
|
|
}
|
2016-09-01 13:13:06 +03:00
|
|
|
case <-c.stop:
|
|
|
|
break loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CancelableStdin) Read(b []byte) (n int, err error) {
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
2016-10-06 07:30:22 +03:00
|
|
|
if atomic.LoadInt32(&c.closed) == 1 {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
2016-09-01 13:13:06 +03:00
|
|
|
|
|
|
|
c.data = b
|
|
|
|
c.notify <- struct{}{}
|
|
|
|
select {
|
|
|
|
case <-c.notify:
|
|
|
|
return c.read, c.err
|
|
|
|
case <-c.stop:
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CancelableStdin) Close() error {
|
2016-10-06 07:30:22 +03:00
|
|
|
if atomic.CompareAndSwapInt32(&c.closed, 0, 1) {
|
|
|
|
close(c.stop)
|
|
|
|
}
|
2016-09-01 13:13:06 +03:00
|
|
|
return nil
|
|
|
|
}
|