Enable to use stderr as output with option

This commit is contained in:
rhysd 2018-10-18 20:38:31 +09:00
parent 15a5ff0630
commit f2a63bd1c1
5 changed files with 26 additions and 0 deletions

View File

@ -225,6 +225,14 @@ func OptionAddASCIICodeBind(b ...ASCIICodeBind) Option {
}
}
// OptionUseStderr to set stderr as output
func OptionUseStderr() Option {
return func(p *Prompt) error {
p.renderer.out.UseStderr()
return nil
}
}
// New returns a Prompt with powerful auto-completion.
func New(executor Executor, completer Completer, opts ...Option) *Prompt {
pt := &Prompt{

View File

@ -145,4 +145,7 @@ type ConsoleWriter interface {
// SetColor sets text and background colors. and specify whether text is bold.
SetColor(fg, bg Color, bold bool)
// UseStderr sets stderr as output
UseStderr()
}

View File

@ -40,6 +40,11 @@ func (w *PosixWriter) Flush() error {
return nil
}
// UseStderr to set stderr as output
func (w *PosixWriter) UseStderr() {
w.fd = syscall.Stderr
}
var _ ConsoleWriter = &PosixWriter{}
// NewStandardOutputWriter returns ConsoleWriter object to write to stdout.

View File

@ -270,6 +270,11 @@ func (w *VT100Writer) SetDisplayAttributes(fg, bg Color, attrs ...DisplayAttribu
return
}
// UseStderr to set stderr as output
func (w *VT100Writer) UseStderr() {
// Do nothing
}
var displayAttributeParameters = map[DisplayAttribute][]byte{
DisplayReset: {'0'},
DisplayBold: {'1'},

View File

@ -25,6 +25,11 @@ func (w *WindowsWriter) Flush() error {
return nil
}
// UseStderr to set stderr as output
func (w *WindowsWriter) UseStderr() {
w.out = colorable.NewColorableStderr()
}
var _ ConsoleWriter = &WindowsWriter{}
// NewStandardOutputWriter returns ConsoleWriter object to write to stdout.