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