forked from fusesource/jansi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnsiRenderer.java
256 lines (217 loc) · 7.18 KB
/
AnsiRenderer.java
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* Copyright (C) 2009-2021 the original author(s).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fusesource.jansi;
import java.io.IOException;
import java.util.Locale;
import org.fusesource.jansi.Ansi.Attribute;
import org.fusesource.jansi.Ansi.Color;
/**
* Renders ANSI color escape-codes in strings by parsing out some special syntax to pick up the correct fluff to use.
*
* The syntax for embedded ANSI codes is:
*
* <pre>
* @|<em>code</em>(,<em>code</em>)* <em>text</em>|@
* </pre>
*
* Examples:
*
* <pre>
* @|bold Hello|@
* </pre>
*
* <pre>
* @|bold,red Warning!|@
* </pre>
*
* @since 2.2
*/
public class AnsiRenderer {
public static final String BEGIN_TOKEN = "@|";
public static final String END_TOKEN = "|@";
public static final String CODE_TEXT_SEPARATOR = " ";
public static final String CODE_LIST_SEPARATOR = ",";
private static final int BEGIN_TOKEN_LEN = 2;
private static final int END_TOKEN_LEN = 2;
public static String render(final String input) throws IllegalArgumentException {
try {
return render(input, new StringBuilder()).toString();
} catch (IOException e) {
// Cannot happen because StringBuilder does not throw IOException
throw new IllegalArgumentException(e);
}
}
/**
* Renders the given input to the target Appendable.
*
* @param input
* source to render
* @param target
* render onto this target Appendable.
* @return the given Appendable
* @throws IOException
* If an I/O error occurs
*/
public static Appendable render(final String input, Appendable target) throws IOException {
int i = 0;
int j, k;
while (true) {
j = input.indexOf(BEGIN_TOKEN, i);
if (j == -1) {
if (i == 0) {
target.append(input);
return target;
}
target.append(input.substring(i));
return target;
}
target.append(input.substring(i, j));
k = input.indexOf(END_TOKEN, j);
if (k == -1) {
target.append(input);
return target;
}
j += BEGIN_TOKEN_LEN;
String spec = input.substring(j, k);
String[] items = spec.split(CODE_TEXT_SEPARATOR, 2);
if (items.length == 1) {
target.append(input);
return target;
}
String replacement = render(items[1], items[0].split(CODE_LIST_SEPARATOR));
target.append(replacement);
i = k + END_TOKEN_LEN;
}
}
public static String render(final String text, final String... codes) {
return render(Ansi.ansi(), codes)
.a(text).reset().toString();
}
/**
* Renders {@link Code} names as an ANSI escape string.
* @param codes The code names to render
* @return an ANSI escape string.
*/
public static String renderCodes(final String... codes) {
return render(Ansi.ansi(), codes).toString();
}
/**
* Renders {@link Code} names as an ANSI escape string.
* @param codes A space separated list of code names to render
* @return an ANSI escape string.
*/
public static String renderCodes(final String codes) {
return renderCodes(codes.split("\\s"));
}
private static Ansi render(Ansi ansi, String... names) {
for (String name : names) {
Code code = Code.valueOf(name.toUpperCase(Locale.ENGLISH));
if (code.isColor()) {
if (code.isBackground()) {
ansi.bg(code.getColor());
} else {
ansi.fg(code.getColor());
}
} else if (code.isAttribute()) {
ansi.a(code.getAttribute());
}
}
return ansi;
}
public static boolean test(final String text) {
return text != null && text.contains(BEGIN_TOKEN);
}
@SuppressWarnings("unused")
public enum Code {
//
// TODO: Find a better way to keep Code in sync with Color/Attribute/Erase
//
// Colors
BLACK(Color.BLACK),
RED(Color.RED),
GREEN(Color.GREEN),
YELLOW(Color.YELLOW),
BLUE(Color.BLUE),
MAGENTA(Color.MAGENTA),
CYAN(Color.CYAN),
WHITE(Color.WHITE),
DEFAULT(Color.DEFAULT),
// Foreground Colors
FG_BLACK(Color.BLACK, false),
FG_RED(Color.RED, false),
FG_GREEN(Color.GREEN, false),
FG_YELLOW(Color.YELLOW, false),
FG_BLUE(Color.BLUE, false),
FG_MAGENTA(Color.MAGENTA, false),
FG_CYAN(Color.CYAN, false),
FG_WHITE(Color.WHITE, false),
FG_DEFAULT(Color.DEFAULT, false),
// Background Colors
BG_BLACK(Color.BLACK, true),
BG_RED(Color.RED, true),
BG_GREEN(Color.GREEN, true),
BG_YELLOW(Color.YELLOW, true),
BG_BLUE(Color.BLUE, true),
BG_MAGENTA(Color.MAGENTA, true),
BG_CYAN(Color.CYAN, true),
BG_WHITE(Color.WHITE, true),
BG_DEFAULT(Color.DEFAULT, true),
// Attributes
RESET(Attribute.RESET),
INTENSITY_BOLD(Attribute.INTENSITY_BOLD),
INTENSITY_FAINT(Attribute.INTENSITY_FAINT),
ITALIC(Attribute.ITALIC),
UNDERLINE(Attribute.UNDERLINE),
BLINK_SLOW(Attribute.BLINK_SLOW),
BLINK_FAST(Attribute.BLINK_FAST),
BLINK_OFF(Attribute.BLINK_OFF),
NEGATIVE_ON(Attribute.NEGATIVE_ON),
NEGATIVE_OFF(Attribute.NEGATIVE_OFF),
CONCEAL_ON(Attribute.CONCEAL_ON),
CONCEAL_OFF(Attribute.CONCEAL_OFF),
UNDERLINE_DOUBLE(Attribute.UNDERLINE_DOUBLE),
UNDERLINE_OFF(Attribute.UNDERLINE_OFF),
// Aliases
BOLD(Attribute.INTENSITY_BOLD),
FAINT(Attribute.INTENSITY_FAINT),;
private final Enum<?> n;
private final boolean background;
Code(final Enum<?> n, boolean background) {
this.n = n;
this.background = background;
}
Code(final Enum<?> n) {
this(n, false);
}
public boolean isColor() {
return n instanceof Ansi.Color;
}
public Ansi.Color getColor() {
return (Ansi.Color) n;
}
public boolean isAttribute() {
return n instanceof Attribute;
}
public Attribute getAttribute() {
return (Attribute) n;
}
public boolean isBackground() {
return background;
}
}
private AnsiRenderer() {
}
}