diff --git a/operation.go b/operation.go index 8d2622d..e02d0e6 100644 --- a/operation.go +++ b/operation.go @@ -1,9 +1,6 @@ package readline -import ( - "io" - "os" -) +import "io" type Operation struct { cfg *Config @@ -203,11 +200,11 @@ func (o *Operation) ioloop() { } func (o *Operation) Stderr() io.Writer { - return &wrapWriter{target: os.Stderr, r: o, t: o.t} + return &wrapWriter{target: o.cfg.Stderr, r: o, t: o.t} } func (o *Operation) Stdout() io.Writer { - return &wrapWriter{target: os.Stdout, r: o, t: o.t} + return &wrapWriter{target: o.cfg.Stdout, r: o, t: o.t} } func (o *Operation) String() (string, error) { diff --git a/readline.go b/readline.go index 864cfab..76a25d4 100644 --- a/readline.go +++ b/readline.go @@ -1,6 +1,9 @@ package readline -import "io" +import ( + "io" + "os" +) type Instance struct { t *Terminal @@ -11,6 +14,24 @@ type Config struct { 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 } func NewEx(cfg *Config) (*Instance, error) { diff --git a/terminal.go b/terminal.go index 9165221..2dd6bbf 100644 --- a/terminal.go +++ b/terminal.go @@ -23,6 +23,9 @@ type Terminal struct { } func NewTerminal(cfg *Config) (*Terminal, error) { + if err := cfg.Init(); err != nil { + return nil, err + } state, err := MakeRaw(syscall.Stdin) if err != nil { return nil, err @@ -40,15 +43,15 @@ func NewTerminal(cfg *Config) (*Terminal, error) { } func (t *Terminal) Write(b []byte) (int, error) { - return os.Stdout.Write(b) + return t.cfg.Stdout.Write(b) } func (t *Terminal) Print(s string) { - fmt.Fprintf(os.Stdout, "%s", s) + fmt.Fprintf(t.cfg.Stdout, "%s", s) } func (t *Terminal) PrintRune(r rune) { - fmt.Fprintf(os.Stdout, "%c", r) + fmt.Fprintf(t.cfg.Stdout, "%c", r) } func (t *Terminal) Readline() *Operation {