-
Notifications
You must be signed in to change notification settings - Fork 2
/
iomanip.go
57 lines (46 loc) · 1.1 KB
/
iomanip.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package iomanip
import (
"bytes"
"io"
"io/ioutil"
"os"
"github.com/profclems/tfa/utils/color"
"github.com/profclems/tfa/utils/terminal"
)
type IO struct {
In io.ReadCloser
StdOut io.Writer
StdErr io.Writer
IsaTTY bool // stdout is a tty
IsErrTTY bool // stderr is a tty
IsInTTY bool // stdin is a tty
}
func InitIO() *IO {
stdoutIsTTY := terminal.IsTerminal(os.Stdout)
stderrIsTTY := terminal.IsTerminal(os.Stderr)
ioStream := &IO{
In: os.Stdin,
StdOut: color.NewColorable(os.Stdout),
StdErr: color.NewColorable(os.Stderr),
IsaTTY: stdoutIsTTY,
IsErrTTY: stderrIsTTY,
}
if stdin, ok := ioStream.In.(*os.File); ok {
ioStream.IsInTTY = terminal.IsTerminal(stdin)
}
color.IsColorEnabled = color.IsEnabled() && stdoutIsTTY && stderrIsTTY
return ioStream
}
func (s *IO) TerminalWidth() int {
return terminal.Width(s.StdOut)
}
func TestIO() (*IO, *bytes.Buffer, *bytes.Buffer, *bytes.Buffer) {
in := &bytes.Buffer{}
out := &bytes.Buffer{}
errOut := &bytes.Buffer{}
return &IO{
In: ioutil.NopCloser(in),
StdOut: out,
StdErr: errOut,
}, in, out, errOut
}