-
Notifications
You must be signed in to change notification settings - Fork 0
/
font.go
142 lines (125 loc) · 3.78 KB
/
font.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
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
package text
import "strings"
const (
AnsiPre = "\u001b["
AnsiReset = AnsiPre + "0m"
AnsiBold = AnsiPre + "1m"
AnsiItalic = AnsiPre + "3m"
AnsiUnderlined = AnsiPre + "4m"
AnsiBlack = AnsiPre + "30m"
AnsiRed = AnsiPre + "31m"
AnsiGreen = AnsiPre + "32m"
AnsiYellow = AnsiPre + "33m"
AnsiBlue = AnsiPre + "34m"
AnsiMagenta = AnsiPre + "35m"
AnsiCyan = AnsiPre + "36m"
AnsiWhite = AnsiPre + "37m"
AnsiGray = AnsiPre + "30;1m"
AnsiBrightRed = AnsiPre + "31;1m"
AnsiBrightGreen = AnsiPre + "32;1m"
AnsiBrightYellow = AnsiPre + "33;1m"
AnsiBrightBlue = AnsiPre + "34;1m"
AnsiBrightMagenta = AnsiPre + "35;1m"
AnsiBrightCyan = AnsiPre + "36;1m"
AnsiBrightWhite = AnsiPre + "37;1m"
)
const (
Pre = "§"
Black = Pre + "0"
Blue = Pre + "1"
Green = Pre + "2"
Cyan = Pre + "3"
Red = Pre + "4"
Magenta = Pre + "5"
Orange = Pre + "6"
BrightGray = Pre + "7"
Gray = Pre + "8"
BrightBlue = Pre + "9"
BrightGreen = Pre + "a"
BrightCyan = Pre + "b"
BrightRed = Pre + "c"
BrightMagenta = Pre + "d"
Yellow = Pre + "e"
White = Pre + "f"
Obfuscated = Pre + "k"
Bold = Pre + "l"
StrikeThrough = Pre + "m"
Underlined = Pre + "n"
Italic = Pre + "o"
Reset = Pre + "r"
)
// ColoredString is a string containing colours.
// ColoredString has functions to manipulate the colours it holds.
type ColoredString string
// colorConvert is used to convert Minecraft colours
// to ANSI colours and the other way around.
var colorConvert = map[string]string{
Black: AnsiBlack,
Blue: AnsiBlue,
Green: AnsiGreen,
Cyan: AnsiCyan,
Red: AnsiRed,
Magenta: AnsiMagenta,
Orange: AnsiYellow,
BrightGray: AnsiWhite,
Gray: AnsiGray,
BrightBlue: AnsiBrightBlue,
BrightGreen: AnsiBrightGreen,
BrightCyan: AnsiBrightCyan,
BrightRed: AnsiBrightRed,
BrightMagenta: AnsiBrightMagenta,
Yellow: AnsiBrightYellow,
White: AnsiBrightWhite,
Bold: AnsiBold,
Underlined: AnsiUnderlined,
Italic: AnsiItalic,
Reset: AnsiReset,
StrikeThrough: AnsiUnderlined,
Obfuscated: AnsiUnderlined,
}
// ToANSI converts all Minecraft colors in a ColoredString to ANSI colors.
// A new string is returned with the colors converted.
func (str ColoredString) ToANSI() string {
text := string(str)
for toConvert, convertValue := range colorConvert {
text = strings.Replace(text, toConvert, convertValue, -1)
}
return text
}
// ToMinecraft converts all ANSI colors in a ColoredString to Minecraft colors.
// A new string is returned with the colors converted.
func (str ColoredString) ToMinecraft() string {
text := string(str)
for convertValue, toConvert := range colorConvert {
text = strings.Replace(text, toConvert, convertValue, -1)
}
return text
}
// StripMinecraft strips all Minecraft colors in a ColoredString.
// A new string is returned with the colors stripped.
func (str ColoredString) StripMinecraft() string {
text := string(str)
for toConvert := range colorConvert {
text = strings.Replace(text, toConvert, "", -1)
}
return text
}
// StripANSI strips all ANSI colors in a ColoredString.
// A new string is returned with the colors stripped.
func (str ColoredString) StripANSI() string {
text := string(str)
for _, toConvert := range colorConvert {
text = strings.Replace(text, toConvert, "", -1)
}
return text
}
// StripAll strips all colors (both ANSI and MCPE) in a ColoredString.
// A new string is returned with the colors stripped.
func (str ColoredString) StripAll() string {
text := string(str)
for mcpeColor, ansiColor := range colorConvert {
text = strings.Replace(text, mcpeColor, "", -1)
text = strings.Replace(text, ansiColor, "", -1)
}
return text
}