readline/readline.go

51 lines
755 B
Go
Raw Normal View History

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-22 13:16:24 +03:00
type Config struct {
Prompt string
HistoryFile string
}
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-22 13:16:24 +03:00
i.o.Close()
2015-09-21 08:30:10 +03:00
return i.t.Close()
2015-09-20 18:14:29 +03:00
}