forked from mirror/readline
let stdout/stderr io.Writer configurable
This commit is contained in:
parent
0801a4ad00
commit
d4ceb57901
|
@ -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) {
|
||||
|
|
23
readline.go
23
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) {
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue