let stdout/stderr io.Writer configurable

This commit is contained in:
Cheney 2015-09-27 09:04:50 +08:00
parent 0801a4ad00
commit d4ceb57901
3 changed files with 31 additions and 10 deletions

View File

@ -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) {

View File

@ -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) {

View File

@ -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 {