-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.go
70 lines (57 loc) · 1.15 KB
/
color.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
58
59
60
61
62
63
64
65
66
67
68
69
70
package zen
import (
"bytes"
"fmt"
"github.com/mattn/go-colorable"
"github.com/mattn/go-isatty"
"io"
"os"
_ "github.com/mattn/go-isatty"
)
type (
inner func(interface{}, []string, *Color) string
)
const Rd = "31"
var (
red = outer(Rd)
)
func outer(n string) inner {
return func(msg interface{}, styles []string, c *Color) string {
if c.disabled {
return fmt.Sprintf("%v", msg)
}
b := new(bytes.Buffer)
b.WriteString("\x1b[")
b.WriteString(n)
for _, s := range styles {
b.WriteString(";")
b.WriteString(s)
}
b.WriteString("m")
return fmt.Sprintf("%s%v\x1b[0m", b.String(), msg)
}
}
type Color struct {
op io.Writer
disabled bool
}
func NewColor() (c *Color) {
c = new(Color)
c.setOutput(colorable.NewColorableStdout())
return
}
func (c *Color) output() io.Writer {
return c.op
}
func (c *Color) setOutput(w io.Writer) {
c.op = w
if w, ok := w.(*os.File); !ok || !isatty.IsTerminal(w.Fd()) {
c.disabled = true
}
}
func (c *Color) printF(format string, args ...interface{}) {
_, _ = fmt.Fprintf(c.op, format, args...)
}
func (c *Color) red(msg interface{}, styles ...string) string {
return red(msg, styles, c)
}