readline/readline.go

90 lines
1.5 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 {
Terminal *Terminal
Operation *Operation
2015-09-20 18:14:29 +03:00
}
2015-09-22 13:16:24 +03:00
type Config struct {
2015-09-25 07:59:36 +03:00
Prompt string
HistoryFile string
AutoComplete AutoCompleter
2015-10-01 17:44:43 +03:00
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
}
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{
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-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-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-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-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-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
}