-
-
Notifications
You must be signed in to change notification settings - Fork 91
/
colour.go
158 lines (132 loc) · 3.77 KB
/
colour.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package text
import (
"fmt"
"strings"
)
// FormatFunc represents a function that may be called on a list of values to format the values and apply
// a colour or format on them, such as making the text bold.
// The formatting on the values passed is done using Minecraft formatting codes. To turn it into a string that
// may be printed, use text.ANSI(string).
type FormatFunc func(a ...interface{}) string
// ANSI converts all Minecraft text formatting codes in the values passed to ANSI formatting codes, so that
// it may be displayed properly in the terminal.
func ANSI(a ...interface{}) string {
str := make([]string, len(a))
for i, v := range a {
str[i] = minecraftReplacer.Replace(fmt.Sprint(v))
}
return strings.Join(str, " ")
}
// Minecraft converts all ANSI text formatting codes in the values passed to Minecraft formatting codes, so
// that it may be displayed in-game.
func Minecraft(a ...interface{}) string {
str := make([]string, len(a))
for i, v := range a {
str[i] = ansiReplacer.Replace(fmt.Sprint(v))
}
return strings.Join(str, " ")
}
// Black returns a black formatter.
func Black() FormatFunc {
return formatFunc(black, reset)
}
// DarkBlue returns a dark blue formatter.
func DarkBlue() FormatFunc {
return formatFunc(darkBlue, reset)
}
// DarkGreen returns a dark green formatter.
func DarkGreen() FormatFunc {
return formatFunc(darkGreen, reset)
}
// DarkAqua returns a dark aqua formatter.
func DarkAqua() FormatFunc {
return formatFunc(darkAqua, reset)
}
// DarkRed returns a dark red formatter.
func DarkRed() FormatFunc {
return formatFunc(darkRed, reset)
}
// DarkPurple returns a dark purple formatter.
func DarkPurple() FormatFunc {
return formatFunc(darkPurple, reset)
}
// Gold returns a gold formatter.
func Gold() FormatFunc {
return formatFunc(gold, reset)
}
// Grey returns a grey formatter.
func Grey() FormatFunc {
return formatFunc(grey, reset)
}
// DarkGrey returns a dark grey formatter.
func DarkGrey() FormatFunc {
return formatFunc(darkGrey, reset)
}
// Blue returns a blue formatter.
func Blue() FormatFunc {
return formatFunc(blue, reset)
}
// Green returns a green formatter.
func Green() FormatFunc {
return formatFunc(green, reset)
}
// Aqua returns an aqua formatter.
func Aqua() FormatFunc {
return formatFunc(aqua, reset)
}
// Red returns a red formatter.
func Red() FormatFunc {
return formatFunc(red, reset)
}
// Purple returns a purple formatter.
func Purple() FormatFunc {
return formatFunc(purple, reset)
}
// Yellow returns a yellow formatter.
func Yellow() FormatFunc {
return formatFunc(yellow, reset)
}
// White returns a white formatter.
func White() FormatFunc {
return formatFunc(white, reset)
}
// DarkYellow returns a dark yellow formatter.
func DarkYellow() FormatFunc {
return formatFunc(darkYellow, reset)
}
// Obfuscated returns an obfuscated formatter.
func Obfuscated() FormatFunc {
return formatFunc(obfuscated, reset)
}
// Bold returns a bold formatter.
func Bold() FormatFunc {
return formatFunc(bold, reset)
}
// Strikethrough returns a strikethrough formatter.
func Strikethrough() FormatFunc {
return formatFunc(strikethrough, reset)
}
// Italic returns an italic formatter.
func Italic() FormatFunc {
return formatFunc(italic, reset)
}
// formatFunc produces a FormatFunc for a given format code and reset code passed. It will start each of the
// arguments passed to the FormatFunc with the format code and end with the reset code.
func formatFunc(formatCode, reset string) FormatFunc {
return func(a ...interface{}) string {
str := &strings.Builder{}
str.WriteString(formatCode)
for i, v := range a {
if i != 0 {
str.WriteByte(' ')
}
str.WriteString(fmt.Sprint(v))
if i != len(a)-1 {
str.WriteString(formatCode)
} else {
str.WriteString(reset)
}
}
return str.String()
}
}