2015-09-20 18:14:29 +03:00
|
|
|
package readline
|
|
|
|
|
2015-09-21 08:30:10 +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-25 07:59:36 +03:00
|
|
|
type AutoCompleter interface {
|
|
|
|
Do(line []rune, pos int) (newLine []rune, newPos int, ok bool)
|
|
|
|
}
|
|
|
|
|
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-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-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-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 {
|
2015-09-24 19:49:55 +03:00
|
|
|
if err := i.t.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-22 13:16:24 +03:00
|
|
|
i.o.Close()
|
2015-09-24 19:49:55 +03:00
|
|
|
return nil
|
2015-09-20 18:14:29 +03:00
|
|
|
}
|