-
Notifications
You must be signed in to change notification settings - Fork 3
/
color.go
69 lines (54 loc) · 1.29 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
package line
import (
"fmt"
"strconv"
)
var (
// BlackColor is a Formatter for black
BlackColor = Color{Code: black}
// RedColor is a Formatter for red
RedColor = Color{Code: red}
// GreenColor is a Formatter for green
GreenColor = Color{Code: green}
// YellowColor is a Formatter for yellow
YellowColor = Color{Code: yellow}
// BlueColor is a Formatter for blue
BlueColor = Color{Code: blue}
// MagentaColor is a Formatter for magenta
MagentaColor = Color{Code: magenta}
// CyanColor is a Formatter for cyan
CyanColor = Color{Code: cyan}
// WhiteColor is a Formatter for white
WhiteColor = Color{Code: white}
)
const escape = "\x1b"
const reset = "0"
// ansi escape sequence color constants
const (
black = 30
red = 31
green = 32
yellow = 33
blue = 34
magenta = 35
cyan = 36
white = 37
)
// Color formats with a given color
type Color struct {
Code int
}
// Sprint wraps fmt.Sprint with ANSI control codes for a color
func (c Color) Sprint(a ...interface{}) string {
str := fmt.Sprint(a...)
return c.enable() + str + c.disable()
}
func (c Color) enable() string {
return fmt.Sprintf("%s[%sm", escape, c.seq())
}
func (c Color) disable() string {
return fmt.Sprintf("%s[%sm", escape, reset)
}
func (c Color) seq() string {
return strconv.Itoa(c.Code)
}