-
Notifications
You must be signed in to change notification settings - Fork 0
/
wincolog.go
47 lines (38 loc) · 1.06 KB
/
wincolog.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
package wincolog
import (
"io"
"os"
"github.com/mattn/go-colorable"
_ "github.com/mattn/go-isatty" // make cross-compiling easier
)
// WinColorTerminal wraps a colorable Writer
// with a ColorSupporter interface
type WinColorTerminal struct {
output io.Writer
}
// Write implements the io.Writer interface
func (w *WinColorTerminal) Write(p []byte) (n int, err error) {
return w.output.Write(p)
}
// ColorSupported implements the ColorSupporter interface
func (w *WinColorTerminal) ColorSupported() bool {
return true
}
// Stdout returns a WinColorTerminal when stdout is a windows terminal
// returns the built-in os.Stdout otherwise
func Stdout() io.Writer {
stdout := colorable.NewColorableStdout()
if stdout == os.Stdout {
return os.Stdout
}
return &WinColorTerminal{output: stdout}
}
// Stderr returns a WinColorTerminal when stderr is a windows terminal
// returns the built-in os.Stderr otherwise
func Stderr() io.Writer {
stderr := colorable.NewColorableStderr()
if stderr == os.Stderr {
return os.Stderr
}
return &WinColorTerminal{output: stderr}
}