readline/readline.go

86 lines
1.3 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 {
t *Terminal
o *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{
t: t,
o: rl,
}, 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.o.SetPrompt(s)
}
2015-09-24 19:16:49 +03:00
func (i *Instance) Stdout() io.Writer {
return i.o.Stdout()
}
2015-09-21 08:30:10 +03:00
func (i *Instance) Stderr() io.Writer {
return i.o.Stderr()
2015-09-20 18:14:29 +03:00
}
2015-10-01 17:44:43 +03:00
func (i *Instance) SetVimMode(on bool) {
i.o.SetVimMode(on)
}
2015-09-30 09:16:46 +03:00
func (i *Instance) ReadPassword(prompt string) ([]byte, error) {
return i.o.Password(prompt)
}
2015-09-21 08:30:10 +03:00
func (i *Instance) Readline() (string, error) {
return i.o.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.o.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.t.Close(); err != nil {
return err
}
2015-09-22 13:16:24 +03:00
i.o.Close()
return nil
2015-09-20 18:14:29 +03:00
}