forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolor.go
More file actions
61 lines (47 loc) · 1.38 KB
/
color.go
File metadata and controls
61 lines (47 loc) · 1.38 KB
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
package utils
import (
"io"
"os"
"github.com/mattn/go-colorable"
"github.com/mattn/go-isatty"
"github.com/mgutz/ansi"
)
var _isStdoutTerminal = false
var checkedTerminal = false
func isStdoutTerminal() bool {
if !checkedTerminal {
fd := os.Stdout.Fd()
_isStdoutTerminal = isatty.IsTerminal(fd) || isatty.IsCygwinTerminal(fd)
checkedTerminal = true
}
return _isStdoutTerminal
}
// NewColorable returns an output stream that handles ANSI color sequences on Windows
func NewColorable(f *os.File) io.Writer {
return colorable.NewColorable(f)
}
func makeColorFunc(color string) func(string) string {
cf := ansi.ColorFunc(color)
return func(arg string) string {
if isStdoutTerminal() {
return cf(arg)
}
return arg
}
}
// Magenta outputs ANSI color if stdout is a tty
var Magenta = makeColorFunc("magenta")
// Cyan outputs ANSI color if stdout is a tty
var Cyan = makeColorFunc("cyan")
// Red outputs ANSI color if stdout is a tty
var Red = makeColorFunc("red")
// Yellow outputs ANSI color if stdout is a tty
var Yellow = makeColorFunc("yellow")
// Blue outputs ANSI color if stdout is a tty
var Blue = makeColorFunc("blue")
// Green outputs ANSI color if stdout is a tty
var Green = makeColorFunc("green")
// Gray outputs ANSI color if stdout is a tty
var Gray = makeColorFunc("black+h")
// Bold outputs ANSI color if stdout is a tty
var Bold = makeColorFunc("default+b")