summaryrefslogtreecommitdiff
path: root/internal/color/color.go
blob: 07361995cf2971696acb0f34c247d7326b7ce9ce (plain)
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 color is used to prettify console output via ANSII terminal colors.
package color

import (
	"fmt"
)

// Color name.
type Color string

// Attribute of a color.
type Attribute string

// The possible color variations.
const (
	escape        = "\x1b"
	reset         = escape + "[0m"
	seq    string = "%s%s%s"

	Gray      Color = escape + "[30m"
	Red       Color = escape + "[31m"
	Green     Color = escape + "[32m"
	Orange    Color = escape + "[33m"
	Blue      Color = escape + "[34m"
	Magenta   Color = escape + "[35m"
	Yellow    Color = escape + "[36m"
	LightGray Color = escape + "[37m"

	BgGray      Color = escape + "[40m"
	BgRed       Color = escape + "[41m"
	BgGreen     Color = escape + "[42m"
	BgOrange    Color = escape + "[43m"
	BgBlue      Color = escape + "[44m"
	BgMagenta   Color = escape + "[45m"
	BgYellow    Color = escape + "[46m"
	BgLightGray Color = escape + "[47m"

	Bold         Attribute = escape + "[1m"
	Italic       Attribute = escape + "[3m"
	Underline    Attribute = escape + "[4m"
	ReverseColor Attribute = escape + "[7m"

	resetBold      = escape + "[22m"
	resetItalic    = escape + "[23m"
	resetUnderline = escape + "[24m"

	Test     Color     = BgYellow
	TestAttr Attribute = Bold
)

// Colored DTail client output enabled.
var Colored bool

// Paint a given string in a given color.
func Paint(c Color, s string) string {
	return fmt.Sprintf(seq, c, s, reset)
}

// Attr adds a given attribute to a given string, such as "bold" or "italic".
func Attr(c Attribute, s string) string {
	switch c {
	case Bold:
		return fmt.Sprintf(seq, Bold, s, resetBold)
	case Italic:
		return fmt.Sprintf(seq, Italic, s, resetItalic)
	case Underline:
		return fmt.Sprintf(seq, Underline, s, resetUnderline)
	}
	panic("Unknown attribute")
}