-
Notifications
You must be signed in to change notification settings - Fork 0
/
colors.go
64 lines (53 loc) · 1.2 KB
/
colors.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
package main
import (
"errors"
)
var (
colors = map[string]string{
"red": "\033[31m",
"blue": "\033[34m",
"green": "\033[32m",
"magenta": "\033[95m",
}
backgroundColors = map[string]string{
"red": "\033[41m",
"green": "\033[42m",
"blue": "\033[44m",
"magenta": "\033[105m",
}
resetCode = "\033[0m"
)
const (
typeColor = iota
typeBackgroundColor
)
func generateColor(color_name string, t int) (string, error) {
switch t {
case typeColor:
if color, ok := colors[color_name]; ok {
return color, nil
}
case typeBackgroundColor:
if backgroundColor, ok := backgroundColors[color_name]; ok {
return backgroundColor, nil
}
}
return "", errors.New(COLOR_NOT_FOUND_ERROR)
}
func WrapContentWithColor(content string, color string) (string, error) {
color, err := generateColor(color, typeColor)
if err != nil {
return "", err
}
return color + content, nil
}
func WrapContentWithBackgroundColor(content string, color string) (string, error) {
backgroundColor, err := generateColor(color, typeBackgroundColor)
if err != nil {
return "", err
}
return backgroundColor + content, nil
}
func AddResetCode(content string) string {
return content + resetCode
}