summaryrefslogtreecommitdiff
path: root/internal/color/color.go
blob: aebc9e4ee3231139cb9f2cc242ba4c39aca44b9f (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Package color contains all terminal color codes we know of.
package color

import (
	"fmt"
	"strings"
)

// FgColor is the text foreground color.
type FgColor string

// BgColor is the text background color.
type BgColor string

// Attribute of text.
type Attribute string

// The possible color variations.
const (
	escape = "\x1b"

	FgBlack   FgColor = escape + "[30m"
	FgRed     FgColor = escape + "[31m"
	FgGreen   FgColor = escape + "[32m"
	FgYellow  FgColor = escape + "[33m"
	FgBlue    FgColor = escape + "[34m"
	FgMagenta FgColor = escape + "[35m"
	FgCyan    FgColor = escape + "[36m"
	FgWhite   FgColor = escape + "[37m"
	FgDefault FgColor = escape + "[39m"

	BgBlack   BgColor = escape + "[40m"
	BgRed     BgColor = escape + "[41m"
	BgGreen   BgColor = escape + "[42m"
	BgYellow  BgColor = escape + "[43m"
	BgBlue    BgColor = escape + "[44m"
	BgMagenta BgColor = escape + "[45m"
	BgCyan    BgColor = escape + "[46m"
	BgWhite   BgColor = escape + "[47m"
	BgDefault BgColor = escape + "[49m"

	AttrNone       Attribute = ""
	AttrReset      Attribute = escape + "[0m"
	AttrBold       Attribute = escape + "[1m"
	AttrDim        Attribute = escape + "[2m"
	AttrItalic     Attribute = escape + "[3m"
	AttrUnderline  Attribute = escape + "[4m"
	AttrBlink      Attribute = escape + "[5m"
	AttrSlowBlink  Attribute = escape + "[5m"
	AttrRapidBlink Attribute = escape + "[6m"
	AttrReverse    Attribute = escape + "[7m"
	AttrHidden     Attribute = escape + "[8m"
)

// ColorNames is the list of all supported terminal colors.
var ColorNames = []string{
	"Black", "Red", "Green", "Yellow", "Blue", "Magenta", "Cyan", "White", "Default",
}

// AttributeNames is the list of all supported terminal text attributes.
var AttributeNames = []string{
	"Bold", "Dim", "Italic", "Underline", "Blink", "SlowBlink", "RapidBlink",
	"Reverse", "Hidden", "None",
}

// ToFgColor converts a given string (e.g. from a config file) into a foreground
// color code.
func ToFgColor(s string) (FgColor, error) {
	switch strings.ToLower(s) {
	case "black":
		return FgBlack, nil
	case "red":
		return FgRed, nil
	case "green":
		return FgGreen, nil
	case "yellow":
		return FgYellow, nil
	case "blue":
		return FgBlue, nil
	case "magenta":
		return FgMagenta, nil
	case "cyan":
		return FgCyan, nil
	case "white":
		return FgWhite, nil
	case "default":
		return FgDefault, nil
	default:
		return FgDefault, fmt.Errorf("unknown foreground text color '%s'", s)
	}
}

// ToBgColor converts a given string (e.g. from a config file) into a background
// color code.
func ToBgColor(s string) (BgColor, error) {
	switch strings.ToLower(s) {
	case "black":
		return BgBlack, nil
	case "red":
		return BgRed, nil
	case "green":
		return BgGreen, nil
	case "yellow":
		return BgYellow, nil
	case "blue":
		return BgBlue, nil
	case "magenta":
		return BgMagenta, nil
	case "cyan":
		return BgCyan, nil
	case "white":
		return BgWhite, nil
	case "default":
		return BgDefault, nil
	default:
		return BgDefault, fmt.Errorf("unknown background text color '%s'", s)
	}
}

// ToAttribute converts a given string (e.g. from a config file) into a text attribute.
func ToAttribute(s string) (Attribute, error) {
	switch strings.ToLower(s) {
	case "bold":
		return AttrBold, nil
	case "dim":
		return AttrDim, nil
	case "italic":
		return AttrItalic, nil
	case "underline":
		return AttrUnderline, nil
	case "blink":
		return AttrBlink, nil
	case "slowblink":
		return AttrSlowBlink, nil
	case "rapidblink":
		return AttrRapidBlink, nil
	case "reverse":
		return AttrReverse, nil
	case "hidden":
		return AttrHidden, nil
	case "none":
		fallthrough
	case "":
		return AttrNone, nil
	default:
		return AttrNone, fmt.Errorf("unknown text attribute '%s'", s)
	}
}