mirror of https://github.com/chzyer/readline.git
let stdout/stderr io.Writer configurable
This commit is contained in:
parent
0801a4ad00
commit
d4ceb57901
|
@ -1,9 +1,6 @@
|
||||||
package readline
|
package readline
|
||||||
|
|
||||||
import (
|
import "io"
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Operation struct {
|
type Operation struct {
|
||||||
cfg *Config
|
cfg *Config
|
||||||
|
@ -203,11 +200,11 @@ func (o *Operation) ioloop() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *Operation) Stderr() io.Writer {
|
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 {
|
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) {
|
func (o *Operation) String() (string, error) {
|
||||||
|
|
23
readline.go
23
readline.go
|
@ -1,6 +1,9 @@
|
||||||
package readline
|
package readline
|
||||||
|
|
||||||
import "io"
|
import (
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
type Instance struct {
|
type Instance struct {
|
||||||
t *Terminal
|
t *Terminal
|
||||||
|
@ -11,6 +14,24 @@ type Config struct {
|
||||||
Prompt string
|
Prompt string
|
||||||
HistoryFile string
|
HistoryFile string
|
||||||
AutoComplete AutoCompleter
|
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) {
|
func NewEx(cfg *Config) (*Instance, error) {
|
||||||
|
|
|
@ -23,6 +23,9 @@ type Terminal struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTerminal(cfg *Config) (*Terminal, error) {
|
func NewTerminal(cfg *Config) (*Terminal, error) {
|
||||||
|
if err := cfg.Init(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
state, err := MakeRaw(syscall.Stdin)
|
state, err := MakeRaw(syscall.Stdin)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -40,15 +43,15 @@ func NewTerminal(cfg *Config) (*Terminal, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Terminal) Write(b []byte) (int, 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) {
|
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) {
|
func (t *Terminal) PrintRune(r rune) {
|
||||||
fmt.Fprintf(os.Stdout, "%c", r)
|
fmt.Fprintf(t.cfg.Stdout, "%c", r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Terminal) Readline() *Operation {
|
func (t *Terminal) Readline() *Operation {
|
||||||
|
|
Loading…
Reference in New Issue