/
io.go
41 lines (35 loc) · 813 Bytes
/
io.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package cli
import (
"io"
"os"
"runtime"
colorable "github.com/mattn/go-colorable"
)
// IO contains an input reader, an output writer and an error writer.
type IO interface {
In() io.Reader
Out() io.Writer
Err() io.Writer
}
// IOContainer is a basic implementation of the IO interface.
type IOContainer struct {
InR io.Reader
OutW io.Writer
ErrW io.Writer
}
func (i *IOContainer) In() io.Reader { return i.InR }
func (i *IOContainer) Out() io.Writer { return i.OutW }
func (i *IOContainer) Err() io.Writer { return i.ErrW }
// Stdio returns a standard IO object.
func Stdio() IO {
io := &IOContainer{
InR: os.Stdin,
OutW: os.Stdout,
ErrW: os.Stderr,
}
if runtime.GOOS == "windows" {
io.OutW = colorable.NewColorableStdout()
io.ErrW = colorable.NewColorableStderr()
}
return io
}