readline/readline.go

115 lines
2.2 KiB
Go
Raw Normal View History

2015-09-20 18:14:29 +03:00
package readline
2015-09-28 19:26:49 +03:00
import "io"
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
Terminal *Terminal
Operation *Operation
2015-09-20 18:14:29 +03:00
}
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
// specify the max length of historys, it's 500 by default
HistoryLimit int
// 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
// If VimMode is true, readline will in vim.insert mode by default
VimMode bool
Stdout io.Writer
Stderr io.Writer
inited bool
}
func (c *Config) Init() error {
if c.inited {
return nil
}
c.inited = true
if c.Stdout == nil {
2015-09-28 19:26:49 +03:00
c.Stdout = Stdout
}
if c.Stderr == nil {
2015-09-28 19:26:49 +03:00
c.Stderr = Stderr
}
2015-10-04 10:23:00 +03:00
if c.HistoryLimit < 0 {
c.HistoryLimit = 500
}
return nil
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,
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) {
i.Operation.SetPrompt(s)
2015-09-27 13:54:26 +03:00
}
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 {
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 {
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) {
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 {
return i.Operation.IsEnableVimMode()
2015-10-02 05:37:21 +03:00
}
2015-09-30 09:16:46 +03:00
func (i *Instance) ReadPassword(prompt string) ([]byte, error) {
return i.Operation.Password(prompt)
2015-09-30 09:16:46 +03:00
}
2015-09-21 08:30:10 +03:00
func (i *Instance) Readline() (string, error) {
return i.Operation.String()
2015-09-20 18:14:29 +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) {
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 {
if err := i.Terminal.Close(); err != nil {
return err
}
i.Operation.Close()
return nil
2015-09-20 18:14:29 +03:00
}