readline/readline.go

76 lines
1.1 KiB
Go
Raw Normal View History

2015-09-20 18:14:29 +03:00
package readline
import (
"io"
"os"
)
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
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 {
c.Stdout = os.Stdout
}
if c.Stderr == nil {
c.Stderr = os.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-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 {
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
}