-
Notifications
You must be signed in to change notification settings - Fork 2
/
console.go
107 lines (79 loc) · 2.88 KB
/
console.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
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
package serr
import (
"fmt"
"strings"
)
// elementType type
type elementType int
const (
// elementTypeForeground constant for foreground color type
elementTypeForeground elementType = iota
// elementTypeBackground constant for background color type
elementTypeBackground
)
// consoleColor type
type consoleColor string
const (
// consoleColorDefault constant for default color
consoleColorDefault consoleColor = "39:49"
// consoleColorBlack constant for black color
consoleColorBlack consoleColor = "30:40"
// consoleColorRed constant for red color
consoleColorRed consoleColor = "31:41"
// consoleColorGreen constant for green color
consoleColorGreen consoleColor = "32:42"
// consoleColorYellow constant for yellow color
consoleColorYellow consoleColor = "33:43"
// consoleColorBlue constant for blue color
consoleColorBlue consoleColor = "34:44"
// consoleColorMagenta constant for magenta color
consoleColorMagenta consoleColor = "35:45"
// consoleColorCyan constant for cyan color
consoleColorCyan consoleColor = "36:46"
// consoleColorLightGray constant for light gray color
consoleColorLightGray consoleColor = "37:47"
// consoleColorDarkGray constant for dark gray color
consoleColorDarkGray consoleColor = "90:100"
// consoleColorLightRed constant for light red color
consoleColorLightRed consoleColor = "91:101"
// consoleColorLightGreen constant for light green color
consoleColorLightGreen consoleColor = "92:102"
// consoleColorLightYellow constant for light yellow color
consoleColorLightYellow consoleColor = "93:103"
// consoleColorLightBlue constant for light blue color
consoleColorLightBlue consoleColor = "94:104"
// consoleColorLightMagenta constant for light magenta color
consoleColorLightMagenta consoleColor = "95:105"
// consoleColorLightCyan constant for light cyan color
consoleColorLightCyan consoleColor = "96:106"
// consoleColorWhite constant for white color
consoleColorWhite consoleColor = "97:107"
)
const (
// escChar constant
escChar = "\x1B"
// resetChar constant
resetChar = escChar + "[0m"
)
// extractConsoleColorCode function
func extractConsoleColorCode(color consoleColor, typ elementType) (string, bool) {
splittedColor := strings.Split(string(color), ":")
if len(splittedColor) == 0 {
splittedColor = append(splittedColor, splittedColor[0])
}
return splittedColor[int(typ)], true
}
// applyConsoleForeColor function
func applyConsoleForeColor(msg string, color consoleColor) string {
if color, ok := extractConsoleColorCode(color, elementTypeForeground); ok {
return fmt.Sprintf("%s[%sm%s%s", escChar, color, msg, resetChar)
}
return msg
}
// applyConsoleBackgroundColor function
func applyConsoleBackgroundColor(msg string, color consoleColor) string {
if color, ok := extractConsoleColorCode(color, elementTypeBackground); ok {
return fmt.Sprintf("%s[%sm%s%s", escChar, color, msg, resetChar)
}
return msg
}