1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.pattern;
18
19 import java.util.Arrays;
20 import java.util.HashMap;
21 import java.util.Locale;
22 import java.util.Map;
23
24 import org.apache.logging.log4j.core.util.Patterns;
25 import org.apache.logging.log4j.util.EnglishEnums;
26
27
28
29
30
31
32
33
34 public enum AnsiEscape {
35
36
37
38
39 PREFIX("\u001b["),
40
41
42
43 SUFFIX("m"),
44
45
46
47
48 SEPARATOR(";"),
49
50
51
52
53 NORMAL("0"),
54
55
56
57
58 BRIGHT("1"),
59
60
61
62
63 DIM("2"),
64
65
66
67
68 UNDERLINE("3"),
69
70
71
72
73 BLINK("5"),
74
75
76
77
78 REVERSE("7"),
79
80
81
82
83 HIDDEN("8"),
84
85
86
87
88 BLACK("30"),
89
90
91
92
93 FG_BLACK("30"),
94
95
96
97
98 RED("31"),
99
100
101
102
103 FG_RED("31"),
104
105
106
107
108 GREEN("32"),
109
110
111
112
113 FG_GREEN("32"),
114
115
116
117
118 YELLOW("33"),
119
120
121
122
123 FG_YELLOW("33"),
124
125
126
127
128 BLUE("34"),
129
130
131
132
133 FG_BLUE("34"),
134
135
136
137
138 MAGENTA("35"),
139
140
141
142
143 FG_MAGENTA("35"),
144
145
146
147
148 CYAN("36"),
149
150
151
152
153 FG_CYAN("36"),
154
155
156
157
158 WHITE("37"),
159
160
161
162
163 FG_WHITE("37"),
164
165
166
167
168 DEFAULT("39"),
169
170
171
172
173 FG_DEFAULT("39"),
174
175
176
177
178 BG_BLACK("40"),
179
180
181
182
183 BG_RED("41"),
184
185
186
187
188 BG_GREEN("42"),
189
190
191
192
193 BG_YELLOW("43"),
194
195
196
197
198 BG_BLUE("44"),
199
200
201
202
203 BG_MAGENTA("45"),
204
205
206
207
208 BG_CYAN("46"),
209
210
211
212
213 BG_WHITE("47");
214
215 private final String code;
216
217 private AnsiEscape(final String code) {
218 this.code = code;
219 }
220
221
222
223
224
225
226 public static String getDefaultStyle() {
227 return PREFIX.getCode() + SUFFIX.getCode();
228 }
229
230
231
232
233
234
235 public String getCode() {
236 return code;
237 }
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259 public static Map<String, String> createMap(final String values, final String[] dontEscapeKeys) {
260 return createMap(values.split(Patterns.COMMA_SEPARATOR), dontEscapeKeys);
261 }
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285 public static Map<String, String> createMap(final String[] values, final String[] dontEscapeKeys) {
286 final String[] sortedIgnoreKeys = dontEscapeKeys != null ? dontEscapeKeys.clone() : new String[0];
287 Arrays.sort(sortedIgnoreKeys);
288 final Map<String, String> map = new HashMap<String, String>();
289 for (final String string : values) {
290 final String[] keyValue = string.split(Patterns.toWhitespaceSeparator("="));
291 if (keyValue.length > 1) {
292 final String key = keyValue[0].toUpperCase(Locale.ENGLISH);
293 final String value = keyValue[1];
294 final boolean escape = Arrays.binarySearch(sortedIgnoreKeys, key) < 0;
295 map.put(key, escape ? createSequence(value.split("\\s")) : value);
296 }
297 }
298 return map;
299 }
300
301
302
303
304
305
306
307
308 public static String createSequence(final String... names) {
309 if (names == null) {
310 return getDefaultStyle();
311 }
312 final StringBuilder sb = new StringBuilder(AnsiEscape.PREFIX.getCode());
313 boolean first = true;
314 for (final String name : names) {
315 try {
316 final AnsiEscape escape = EnglishEnums.valueOf(AnsiEscape.class, name.trim());
317 if (!first) {
318 sb.append(AnsiEscape.SEPARATOR.getCode());
319 }
320 first = false;
321 sb.append(escape.getCode());
322 } catch (final Exception ex) {
323
324 }
325 }
326 sb.append(AnsiEscape.SUFFIX.getCode());
327 return sb.toString();
328 }
329
330 }