2015-09-20 18:14:29 +03:00
|
|
|
package readline
|
|
|
|
|
2015-12-23 08:39:23 +03:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
)
|
2015-09-20 18:14:29 +03:00
|
|
|
|
2015-09-21 08:30:10 +03:00
|
|
|
type Instance struct {
|
2015-10-04 10:23:00 +03:00
|
|
|
Config *Config
|
2015-10-03 09:11:13 +03:00
|
|
|
Terminal *Terminal
|
|
|
|
Operation *Operation
|
2015-09-20 18:14:29 +03:00
|
|
|
}
|
|
|
|
|
2015-12-23 08:39:23 +03:00
|
|
|
type FdReader interface {
|
|
|
|
io.Reader
|
|
|
|
Fd() uintptr
|
|
|
|
}
|
|
|
|
|
2015-09-22 13:16:24 +03:00
|
|
|
type Config struct {
|
2015-10-04 10:23:00 +03:00
|
|
|
// prompt supports ANSI escape sequence, so we can color some characters even in windows
|
|
|
|
Prompt string
|
|
|
|
|
|
|
|
// readline will persist historys to file where HistoryFile specified
|
|
|
|
HistoryFile string
|
2015-11-20 15:56:42 +03:00
|
|
|
// specify the max length of historys, it's 500 by default, set it to -1 to disable history
|
2016-02-16 11:55:19 +03:00
|
|
|
HistoryLimit int
|
|
|
|
DisableAutoSaveHistory bool
|
2015-10-04 10:23:00 +03:00
|
|
|
|
|
|
|
// AutoCompleter will called once user press TAB
|
2015-09-25 07:59:36 +03:00
|
|
|
AutoComplete AutoCompleter
|
2015-10-04 10:23:00 +03:00
|
|
|
|
2015-11-20 15:56:42 +03:00
|
|
|
// Any key press will pass to Listener
|
|
|
|
// NOTE: Listener will be triggered by (nil, 0, 0) immediately
|
|
|
|
Listener Listener
|
|
|
|
|
2015-10-04 10:23:00 +03:00
|
|
|
// If VimMode is true, readline will in vim.insert mode by default
|
|
|
|
VimMode bool
|
|
|
|
|
2015-11-20 06:25:30 +03:00
|
|
|
InterruptPrompt string
|
|
|
|
EOFPrompt string
|
|
|
|
|
2015-12-23 08:39:23 +03:00
|
|
|
Stdin FdReader
|
2015-10-04 10:23:00 +03:00
|
|
|
Stdout io.Writer
|
|
|
|
Stderr io.Writer
|
2015-09-27 04:04:50 +03:00
|
|
|
|
2015-11-20 15:56:42 +03:00
|
|
|
MaskRune rune
|
|
|
|
|
2016-02-15 05:03:41 +03:00
|
|
|
UniqueEditLine bool
|
|
|
|
|
2016-02-18 06:25:41 +03:00
|
|
|
// force use interactive even stdout is not a tty
|
|
|
|
StdoutFd int
|
|
|
|
ForceUseInteractive bool
|
|
|
|
|
2015-11-20 15:56:42 +03:00
|
|
|
// private fields
|
|
|
|
inited bool
|
|
|
|
opHistory *opHistory
|
|
|
|
opSearch *opSearch
|
2015-09-27 04:04:50 +03:00
|
|
|
}
|
|
|
|
|
2016-02-18 06:25:41 +03:00
|
|
|
func (c *Config) useInteractive() bool {
|
|
|
|
return c.ForceUseInteractive || IsTerminal(c.StdoutFd)
|
|
|
|
}
|
|
|
|
|
2015-09-27 04:04:50 +03:00
|
|
|
func (c *Config) Init() error {
|
|
|
|
if c.inited {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
c.inited = true
|
2015-12-23 08:39:23 +03:00
|
|
|
if c.Stdin == nil {
|
|
|
|
c.Stdin = os.Stdin
|
|
|
|
}
|
2015-09-27 04:04:50 +03:00
|
|
|
if c.Stdout == nil {
|
2015-09-28 19:26:49 +03:00
|
|
|
c.Stdout = Stdout
|
2015-09-27 04:04:50 +03:00
|
|
|
}
|
|
|
|
if c.Stderr == nil {
|
2015-09-28 19:26:49 +03:00
|
|
|
c.Stderr = Stderr
|
2015-09-27 04:04:50 +03:00
|
|
|
}
|
2016-02-18 06:25:41 +03:00
|
|
|
if c.StdoutFd == 0 {
|
|
|
|
c.StdoutFd = StdoutFd
|
|
|
|
}
|
2015-11-20 15:56:42 +03:00
|
|
|
if c.HistoryLimit == 0 {
|
2015-10-04 10:23:00 +03:00
|
|
|
c.HistoryLimit = 500
|
|
|
|
}
|
2015-11-20 06:25:30 +03:00
|
|
|
|
|
|
|
if c.InterruptPrompt == "" {
|
|
|
|
c.InterruptPrompt = "^C"
|
|
|
|
} else if c.InterruptPrompt == "\n" {
|
|
|
|
c.InterruptPrompt = ""
|
|
|
|
}
|
|
|
|
if c.EOFPrompt == "" {
|
|
|
|
c.EOFPrompt = "^D"
|
|
|
|
} else if c.EOFPrompt == "\n" {
|
|
|
|
c.EOFPrompt = ""
|
|
|
|
}
|
|
|
|
|
2015-09-27 04:04:50 +03:00
|
|
|
return nil
|
2015-09-22 13:16:24 +03:00
|
|
|
}
|
|
|
|
|
2016-02-18 06:25:41 +03:00
|
|
|
func (c Config) Clone() *Config {
|
|
|
|
c.opHistory = nil
|
|
|
|
c.opSearch = nil
|
|
|
|
return &c
|
|
|
|
}
|
|
|
|
|
2015-11-20 15:56:42 +03:00
|
|
|
func (c *Config) SetListener(f func(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool)) {
|
|
|
|
c.Listener = FuncListener(f)
|
|
|
|
}
|
|
|
|
|
2015-09-22 13:16:24 +03:00
|
|
|
func NewEx(cfg *Config) (*Instance, error) {
|
|
|
|
t, err := NewTerminal(cfg)
|
2015-09-21 08:30:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-09-20 18:14:29 +03:00
|
|
|
}
|
2015-09-22 13:16:24 +03:00
|
|
|
rl := t.Readline()
|
2015-09-21 08:30:10 +03:00
|
|
|
return &Instance{
|
2015-10-04 10:23:00 +03:00
|
|
|
Config: cfg,
|
2015-10-03 09:11:13 +03:00
|
|
|
Terminal: t,
|
|
|
|
Operation: rl,
|
2015-09-21 08:30:10 +03:00
|
|
|
}, nil
|
2015-09-20 18:14:29 +03:00
|
|
|
}
|
|
|
|
|
2015-09-22 13:16:24 +03:00
|
|
|
func New(prompt string) (*Instance, error) {
|
|
|
|
return NewEx(&Config{Prompt: prompt})
|
|
|
|
}
|
|
|
|
|
2015-09-27 13:54:26 +03:00
|
|
|
func (i *Instance) SetPrompt(s string) {
|
2015-10-03 09:11:13 +03:00
|
|
|
i.Operation.SetPrompt(s)
|
2015-09-27 13:54:26 +03:00
|
|
|
}
|
|
|
|
|
2015-11-20 15:56:42 +03:00
|
|
|
func (i *Instance) SetMaskRune(r rune) {
|
|
|
|
i.Operation.SetMaskRune(r)
|
|
|
|
}
|
|
|
|
|
2015-10-04 10:23:00 +03:00
|
|
|
// change hisotry persistence in runtime
|
|
|
|
func (i *Instance) SetHistoryPath(p string) {
|
|
|
|
i.Operation.SetHistoryPath(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// readline will refresh automatic when write through Stdout()
|
2015-09-24 19:16:49 +03:00
|
|
|
func (i *Instance) Stdout() io.Writer {
|
2015-10-03 09:11:13 +03:00
|
|
|
return i.Operation.Stdout()
|
2015-09-24 19:16:49 +03:00
|
|
|
}
|
|
|
|
|
2015-10-04 10:23:00 +03:00
|
|
|
// readline will refresh automatic when write through Stdout()
|
2015-09-21 08:30:10 +03:00
|
|
|
func (i *Instance) Stderr() io.Writer {
|
2015-10-03 09:11:13 +03:00
|
|
|
return i.Operation.Stderr()
|
2015-09-20 18:14:29 +03:00
|
|
|
}
|
|
|
|
|
2015-10-04 10:23:00 +03:00
|
|
|
// switch VimMode in runtime
|
2015-10-01 17:44:43 +03:00
|
|
|
func (i *Instance) SetVimMode(on bool) {
|
2015-10-03 09:11:13 +03:00
|
|
|
i.Operation.SetVimMode(on)
|
2015-10-01 17:44:43 +03:00
|
|
|
}
|
|
|
|
|
2015-10-02 05:37:21 +03:00
|
|
|
func (i *Instance) IsVimMode() bool {
|
2015-10-03 09:11:13 +03:00
|
|
|
return i.Operation.IsEnableVimMode()
|
2015-10-02 05:37:21 +03:00
|
|
|
}
|
|
|
|
|
2015-11-20 15:56:42 +03:00
|
|
|
func (i *Instance) GenPasswordConfig() *Config {
|
|
|
|
return i.Operation.GenPasswordConfig()
|
|
|
|
}
|
|
|
|
|
|
|
|
// we can generate a config by `i.GenPasswordConfig()`
|
|
|
|
func (i *Instance) ReadPasswordWithConfig(cfg *Config) ([]byte, error) {
|
|
|
|
return i.Operation.PasswordWithConfig(cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Instance) ReadPasswordEx(prompt string, l Listener) ([]byte, error) {
|
|
|
|
return i.Operation.PasswordEx(prompt, l)
|
|
|
|
}
|
|
|
|
|
2015-09-30 09:16:46 +03:00
|
|
|
func (i *Instance) ReadPassword(prompt string) ([]byte, error) {
|
2015-10-03 09:11:13 +03:00
|
|
|
return i.Operation.Password(prompt)
|
2015-09-30 09:16:46 +03:00
|
|
|
}
|
|
|
|
|
2016-02-16 11:55:19 +03:00
|
|
|
// err is one of (nil, io.EOF, readline.ErrInterrupt)
|
2015-09-21 08:30:10 +03:00
|
|
|
func (i *Instance) Readline() (string, error) {
|
2015-10-03 09:11:13 +03:00
|
|
|
return i.Operation.String()
|
2015-09-20 18:14:29 +03:00
|
|
|
}
|
|
|
|
|
2016-02-18 06:25:41 +03:00
|
|
|
func (i *Instance) SaveHistory(content string) error {
|
|
|
|
return i.Operation.SaveHistory(content)
|
2016-02-16 11:55:19 +03:00
|
|
|
}
|
|
|
|
|
2015-10-04 10:23:00 +03:00
|
|
|
// same as readline
|
2015-09-21 08:30:10 +03:00
|
|
|
func (i *Instance) ReadSlice() ([]byte, error) {
|
2015-10-03 09:11:13 +03:00
|
|
|
return i.Operation.Slice()
|
2015-09-20 18:14:29 +03:00
|
|
|
}
|
|
|
|
|
2015-10-04 10:23:00 +03:00
|
|
|
// we must make sure that call Close() before process exit.
|
2015-09-21 08:30:10 +03:00
|
|
|
func (i *Instance) Close() error {
|
2015-10-03 09:11:13 +03:00
|
|
|
if err := i.Terminal.Close(); err != nil {
|
2015-09-24 19:49:55 +03:00
|
|
|
return err
|
|
|
|
}
|
2015-10-03 09:11:13 +03:00
|
|
|
i.Operation.Close()
|
2015-09-24 19:49:55 +03:00
|
|
|
return nil
|
2015-09-20 18:14:29 +03:00
|
|
|
}
|
2015-11-20 15:56:42 +03:00
|
|
|
|
|
|
|
func (i *Instance) SetConfig(cfg *Config) *Config {
|
|
|
|
if i.Config == cfg {
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
old := i.Config
|
|
|
|
i.Config = cfg
|
|
|
|
i.Operation.SetConfig(cfg)
|
|
|
|
i.Terminal.SetConfig(cfg)
|
|
|
|
return old
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Instance) Refresh() {
|
|
|
|
i.Operation.Refresh()
|
|
|
|
}
|