franta-hg@31
|
1 |
/**
|
franta-hg@31
|
2 |
* SQL-DK
|
franta-hg@31
|
3 |
* Copyright © 2013 František Kučera (frantovo.cz)
|
franta-hg@31
|
4 |
*
|
franta-hg@31
|
5 |
* This program is free software: you can redistribute it and/or modify
|
franta-hg@31
|
6 |
* it under the terms of the GNU General Public License as published by
|
franta-hg@31
|
7 |
* the Free Software Foundation, either version 3 of the License, or
|
franta-hg@31
|
8 |
* (at your option) any later version.
|
franta-hg@31
|
9 |
*
|
franta-hg@31
|
10 |
* This program is distributed in the hope that it will be useful,
|
franta-hg@31
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
franta-hg@31
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
franta-hg@31
|
13 |
* GNU General Public License for more details.
|
franta-hg@31
|
14 |
*
|
franta-hg@31
|
15 |
* You should have received a copy of the GNU General Public License
|
franta-hg@31
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
franta-hg@31
|
17 |
*/
|
franta-hg@31
|
18 |
package info.globalcode.sql.dk;
|
franta-hg@31
|
19 |
|
franta-hg@31
|
20 |
import java.io.File;
|
franta-hg@31
|
21 |
import java.io.FileNotFoundException;
|
franta-hg@31
|
22 |
import java.io.OutputStream;
|
franta-hg@31
|
23 |
import java.io.PrintWriter;
|
franta-hg@31
|
24 |
import java.io.UnsupportedEncodingException;
|
franta-hg@31
|
25 |
import java.io.Writer;
|
franta-hg@31
|
26 |
import java.util.EnumSet;
|
franta-hg@31
|
27 |
|
franta-hg@31
|
28 |
/**
|
franta-hg@155
|
29 |
* PrintWriter with convenience methods for printing color and formatted text.
|
franta-hg@155
|
30 |
*
|
franta-hg@155
|
31 |
* Uses ANSI Escape Sequences.
|
franta-hg@155
|
32 |
* See: http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
|
franta-hg@31
|
33 |
*
|
franta-hg@31
|
34 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@31
|
35 |
*/
|
franta-hg@31
|
36 |
public class ColorfulPrintWriter extends PrintWriter {
|
franta-hg@31
|
37 |
|
franta-hg@31
|
38 |
public enum TerminalColor {
|
franta-hg@31
|
39 |
|
franta-hg@31
|
40 |
Black(30, 40),
|
franta-hg@31
|
41 |
Red(31, 41),
|
franta-hg@31
|
42 |
Green(32, 42),
|
franta-hg@31
|
43 |
Yellow(33, 43),
|
franta-hg@31
|
44 |
Blue(34, 44),
|
franta-hg@31
|
45 |
Magenta(35, 45),
|
franta-hg@31
|
46 |
Cyan(36, 46),
|
franta-hg@31
|
47 |
White(37, 47);
|
franta-hg@31
|
48 |
private final int foregroundCode;
|
franta-hg@31
|
49 |
private final int backgroundCode;
|
franta-hg@31
|
50 |
|
franta-hg@31
|
51 |
private TerminalColor(int foregroundCode, int backgroundCode) {
|
franta-hg@31
|
52 |
this.foregroundCode = foregroundCode;
|
franta-hg@31
|
53 |
this.backgroundCode = backgroundCode;
|
franta-hg@31
|
54 |
}
|
franta-hg@31
|
55 |
|
franta-hg@31
|
56 |
public int getForegroundCode() {
|
franta-hg@31
|
57 |
return foregroundCode;
|
franta-hg@31
|
58 |
}
|
franta-hg@31
|
59 |
|
franta-hg@31
|
60 |
public int getBackgroundCode() {
|
franta-hg@31
|
61 |
return backgroundCode;
|
franta-hg@31
|
62 |
}
|
franta-hg@31
|
63 |
}
|
franta-hg@31
|
64 |
|
franta-hg@31
|
65 |
public enum TerminalStyle {
|
franta-hg@31
|
66 |
|
franta-hg@31
|
67 |
Reset(0),
|
franta-hg@31
|
68 |
Bright(1),
|
franta-hg@31
|
69 |
Dim(2),
|
franta-hg@31
|
70 |
Underscore(4),
|
franta-hg@31
|
71 |
Blink(5),
|
franta-hg@31
|
72 |
Reverse(7),
|
franta-hg@31
|
73 |
Hidden(8);
|
franta-hg@31
|
74 |
private int code;
|
franta-hg@31
|
75 |
|
franta-hg@31
|
76 |
private TerminalStyle(int code) {
|
franta-hg@31
|
77 |
this.code = code;
|
franta-hg@31
|
78 |
}
|
franta-hg@31
|
79 |
|
franta-hg@31
|
80 |
public int getCode() {
|
franta-hg@31
|
81 |
return code;
|
franta-hg@31
|
82 |
}
|
franta-hg@31
|
83 |
}
|
franta-hg@126
|
84 |
private final boolean COLOR_ENABLED;
|
franta-hg@31
|
85 |
private boolean colorful = true;
|
franta-hg@31
|
86 |
|
franta-hg@59
|
87 |
public void setStyle(TerminalStyle style) {
|
franta-hg@59
|
88 |
setStyle(EnumSet.of(style));
|
franta-hg@59
|
89 |
}
|
franta-hg@59
|
90 |
|
franta-hg@31
|
91 |
public void setStyle(EnumSet<TerminalStyle> styles) {
|
franta-hg@31
|
92 |
printCodes(getStyleCodes(styles));
|
franta-hg@31
|
93 |
}
|
franta-hg@31
|
94 |
|
franta-hg@31
|
95 |
private static int[] getStyleCodes(EnumSet<TerminalStyle> styles) {
|
franta-hg@31
|
96 |
int[] array = new int[styles.size()];
|
franta-hg@31
|
97 |
int i = 0;
|
franta-hg@31
|
98 |
for (TerminalStyle s : styles) {
|
franta-hg@31
|
99 |
array[i++] = s.getCode();
|
franta-hg@31
|
100 |
}
|
franta-hg@31
|
101 |
return array;
|
franta-hg@31
|
102 |
}
|
franta-hg@31
|
103 |
|
franta-hg@40
|
104 |
/**
|
franta-hg@40
|
105 |
* Print (usually audible) bell code (\007, \a, ^G)
|
franta-hg@40
|
106 |
*/
|
franta-hg@40
|
107 |
public void bell() {
|
franta-hg@40
|
108 |
print("\007");
|
franta-hg@40
|
109 |
}
|
franta-hg@40
|
110 |
|
franta-hg@40
|
111 |
/**
|
franta-hg@40
|
112 |
* Eat the last character
|
franta-hg@40
|
113 |
*/
|
franta-hg@40
|
114 |
public void backspace() {
|
franta-hg@40
|
115 |
print("\b");
|
franta-hg@40
|
116 |
}
|
franta-hg@40
|
117 |
|
franta-hg@40
|
118 |
/**
|
franta-hg@40
|
119 |
* Eat n last characters
|
franta-hg@40
|
120 |
*
|
franta-hg@40
|
121 |
* @param count n
|
franta-hg@40
|
122 |
*/
|
franta-hg@40
|
123 |
public void backspace(int count) {
|
franta-hg@40
|
124 |
for (int i = 0; i < count; i++) {
|
franta-hg@40
|
125 |
backspace();
|
franta-hg@40
|
126 |
}
|
franta-hg@40
|
127 |
}
|
franta-hg@40
|
128 |
|
franta-hg@40
|
129 |
/**
|
franta-hg@40
|
130 |
* With 100 ms delay and all colors.
|
franta-hg@40
|
131 |
*
|
franta-hg@40
|
132 |
* @see #printRainbow(java.lang.String, int,
|
franta-hg@40
|
133 |
* info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor[])
|
franta-hg@40
|
134 |
*/
|
franta-hg@40
|
135 |
public void printRainbow(String string) {
|
franta-hg@40
|
136 |
printRainbow(string, 100);
|
franta-hg@40
|
137 |
}
|
franta-hg@40
|
138 |
|
franta-hg@40
|
139 |
/**
|
franta-hg@40
|
140 |
* With all colors.
|
franta-hg@40
|
141 |
*
|
franta-hg@40
|
142 |
* @see #printRainbow(java.lang.String, int,
|
franta-hg@40
|
143 |
* info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor[])
|
franta-hg@40
|
144 |
*/
|
franta-hg@40
|
145 |
public void printRainbow(String string, int delay) {
|
franta-hg@40
|
146 |
printRainbow(string, delay, TerminalColor.values());
|
franta-hg@40
|
147 |
}
|
franta-hg@40
|
148 |
|
franta-hg@40
|
149 |
/**
|
franta-hg@40
|
150 |
* Prints rainbow text – (re)writes same text subsequently in given colors and then in default
|
franta-hg@40
|
151 |
* color.
|
franta-hg@40
|
152 |
*
|
franta-hg@40
|
153 |
* @param string text to be printed, should not contain \n new line (then rainbow does not work
|
franta-hg@40
|
154 |
* – use println() after printRainbow() instead)
|
franta-hg@40
|
155 |
* @param delay delay between rewrites
|
franta-hg@40
|
156 |
* @param colors list of colors to be used
|
franta-hg@40
|
157 |
*/
|
franta-hg@40
|
158 |
public void printRainbow(String string, int delay, TerminalColor... colors) {
|
franta-hg@40
|
159 |
for (TerminalColor c : colors) {
|
franta-hg@40
|
160 |
print(c, string);
|
franta-hg@40
|
161 |
try {
|
franta-hg@40
|
162 |
Thread.sleep(delay);
|
franta-hg@40
|
163 |
} catch (InterruptedException e) {
|
franta-hg@40
|
164 |
// no time to sleep
|
franta-hg@40
|
165 |
break;
|
franta-hg@40
|
166 |
}
|
franta-hg@40
|
167 |
backspace(string.length());
|
franta-hg@40
|
168 |
flush();
|
franta-hg@40
|
169 |
}
|
franta-hg@40
|
170 |
print(string);
|
franta-hg@40
|
171 |
}
|
franta-hg@40
|
172 |
|
franta-hg@31
|
173 |
public void setForegroundColor(TerminalColor color) {
|
franta-hg@31
|
174 |
printCodes(color.getForegroundCode());
|
franta-hg@31
|
175 |
}
|
franta-hg@31
|
176 |
|
franta-hg@31
|
177 |
public void setBackgroundColor(TerminalColor color) {
|
franta-hg@31
|
178 |
printCodes(color.getBackgroundCode());
|
franta-hg@31
|
179 |
}
|
franta-hg@31
|
180 |
|
franta-hg@31
|
181 |
public void print(TerminalColor foregroundColor, String string) {
|
franta-hg@31
|
182 |
setForegroundColor(foregroundColor);
|
franta-hg@31
|
183 |
print(string);
|
franta-hg@31
|
184 |
resetAll();
|
franta-hg@31
|
185 |
}
|
franta-hg@31
|
186 |
|
franta-hg@31
|
187 |
public void println(TerminalColor foregroundColor, String string) {
|
franta-hg@31
|
188 |
print(foregroundColor, string);
|
franta-hg@31
|
189 |
println();
|
franta-hg@31
|
190 |
}
|
franta-hg@31
|
191 |
|
franta-hg@31
|
192 |
public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, String string) {
|
franta-hg@31
|
193 |
setForegroundColor(foregroundColor);
|
franta-hg@31
|
194 |
setBackgroundColor(backgroundColor);
|
franta-hg@31
|
195 |
print(string);
|
franta-hg@31
|
196 |
resetAll();
|
franta-hg@31
|
197 |
}
|
franta-hg@31
|
198 |
|
franta-hg@31
|
199 |
public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, String string) {
|
franta-hg@31
|
200 |
print(foregroundColor, backgroundColor, string);
|
franta-hg@31
|
201 |
println();
|
franta-hg@31
|
202 |
}
|
franta-hg@31
|
203 |
|
franta-hg@31
|
204 |
public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, EnumSet<TerminalStyle> styles, String string) {
|
franta-hg@31
|
205 |
setForegroundColor(foregroundColor);
|
franta-hg@31
|
206 |
setBackgroundColor(backgroundColor);
|
franta-hg@31
|
207 |
setStyle(styles);
|
franta-hg@31
|
208 |
print(string);
|
franta-hg@31
|
209 |
resetAll();
|
franta-hg@31
|
210 |
}
|
franta-hg@31
|
211 |
|
franta-hg@31
|
212 |
public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, EnumSet<TerminalStyle> styles, String string) {
|
franta-hg@31
|
213 |
print(foregroundColor, backgroundColor, styles, string);
|
franta-hg@31
|
214 |
println();
|
franta-hg@31
|
215 |
}
|
franta-hg@31
|
216 |
|
franta-hg@31
|
217 |
public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, TerminalStyle style, String string) {
|
franta-hg@31
|
218 |
print(foregroundColor, backgroundColor, EnumSet.of(style), string);
|
franta-hg@31
|
219 |
}
|
franta-hg@31
|
220 |
|
franta-hg@31
|
221 |
public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, TerminalStyle style, String string) {
|
franta-hg@31
|
222 |
print(foregroundColor, backgroundColor, style, string);
|
franta-hg@31
|
223 |
println();
|
franta-hg@31
|
224 |
}
|
franta-hg@31
|
225 |
|
franta-hg@31
|
226 |
public void print(TerminalColor foregroundColor, EnumSet<TerminalStyle> styles, String string) {
|
franta-hg@31
|
227 |
setForegroundColor(foregroundColor);
|
franta-hg@31
|
228 |
setStyle(styles);
|
franta-hg@31
|
229 |
print(string);
|
franta-hg@31
|
230 |
resetAll();
|
franta-hg@31
|
231 |
}
|
franta-hg@31
|
232 |
|
franta-hg@31
|
233 |
public void println(TerminalColor foregroundColor, EnumSet<TerminalStyle> styles, String string) {
|
franta-hg@31
|
234 |
print(foregroundColor, styles, string);
|
franta-hg@31
|
235 |
println();
|
franta-hg@31
|
236 |
}
|
franta-hg@31
|
237 |
|
franta-hg@31
|
238 |
public void print(TerminalColor foregroundColor, TerminalStyle style, String string) {
|
franta-hg@31
|
239 |
print(foregroundColor, EnumSet.of(style), string);
|
franta-hg@31
|
240 |
}
|
franta-hg@31
|
241 |
|
franta-hg@31
|
242 |
public void println(TerminalColor foregroundColor, TerminalStyle style, String string) {
|
franta-hg@31
|
243 |
print(foregroundColor, style, string);
|
franta-hg@31
|
244 |
println();
|
franta-hg@31
|
245 |
}
|
franta-hg@31
|
246 |
|
franta-hg@37
|
247 |
public void print(EnumSet<TerminalStyle> styles, String string) {
|
franta-hg@37
|
248 |
setStyle(styles);
|
franta-hg@37
|
249 |
print(string);
|
franta-hg@37
|
250 |
resetAll();
|
franta-hg@37
|
251 |
}
|
franta-hg@37
|
252 |
|
franta-hg@37
|
253 |
public void println(EnumSet<TerminalStyle> styles, String string) {
|
franta-hg@37
|
254 |
print(styles, string);
|
franta-hg@37
|
255 |
println();
|
franta-hg@37
|
256 |
}
|
franta-hg@37
|
257 |
|
franta-hg@37
|
258 |
public void print(TerminalStyle style, String string) {
|
franta-hg@37
|
259 |
print(EnumSet.of(style), string);
|
franta-hg@37
|
260 |
}
|
franta-hg@37
|
261 |
|
franta-hg@37
|
262 |
public void println(TerminalStyle style, String string) {
|
franta-hg@37
|
263 |
print(style, string);
|
franta-hg@37
|
264 |
println();
|
franta-hg@37
|
265 |
}
|
franta-hg@37
|
266 |
|
franta-hg@31
|
267 |
public void resetAll() {
|
franta-hg@55
|
268 |
printCodes(TerminalStyle.Reset.code);
|
franta-hg@31
|
269 |
}
|
franta-hg@31
|
270 |
|
franta-hg@31
|
271 |
private void printCodes(int... codes) {
|
franta-hg@126
|
272 |
if (COLOR_ENABLED && colorful) {
|
franta-hg@31
|
273 |
print("\033[");
|
franta-hg@31
|
274 |
for (int i = 0; i < codes.length; i++) {
|
franta-hg@31
|
275 |
print(codes[i]);
|
franta-hg@31
|
276 |
if (i < codes.length - 1 && codes.length > 1) {
|
franta-hg@31
|
277 |
print(";");
|
franta-hg@31
|
278 |
}
|
franta-hg@31
|
279 |
}
|
franta-hg@31
|
280 |
print("m");
|
franta-hg@31
|
281 |
}
|
franta-hg@31
|
282 |
}
|
franta-hg@31
|
283 |
|
franta-hg@126
|
284 |
/**
|
franta-hg@126
|
285 |
* Colors can be switched on/off during usage of this writer.
|
franta-hg@126
|
286 |
*
|
franta-hg@126
|
287 |
* @return whether colors are currently turned on
|
franta-hg@126
|
288 |
* @see #isColorEnabled()
|
franta-hg@126
|
289 |
*/
|
franta-hg@31
|
290 |
public boolean isColorful() {
|
franta-hg@31
|
291 |
return colorful;
|
franta-hg@31
|
292 |
}
|
franta-hg@31
|
293 |
|
franta-hg@126
|
294 |
/**
|
franta-hg@126
|
295 |
* Collors might be definitively disabled in constructor. If not, they can be turned on/off
|
franta-hg@126
|
296 |
* during usage of this writer by {@linkplain #setColorful(boolean)}
|
franta-hg@126
|
297 |
*
|
franta-hg@126
|
298 |
* @return whether colors are allowed for this instance of this class
|
franta-hg@126
|
299 |
* @see #isColorful()
|
franta-hg@126
|
300 |
*/
|
franta-hg@126
|
301 |
public boolean isColorEnabled() {
|
franta-hg@126
|
302 |
return COLOR_ENABLED;
|
franta-hg@126
|
303 |
}
|
franta-hg@126
|
304 |
|
franta-hg@126
|
305 |
/**
|
franta-hg@126
|
306 |
* @see #isColorful()
|
franta-hg@126
|
307 |
* @see #isColorEnabled()
|
franta-hg@126
|
308 |
*/
|
franta-hg@31
|
309 |
public void setColorful(boolean colorful) {
|
franta-hg@31
|
310 |
this.colorful = colorful;
|
franta-hg@31
|
311 |
}
|
franta-hg@31
|
312 |
|
franta-hg@31
|
313 |
public ColorfulPrintWriter(File file) throws FileNotFoundException {
|
franta-hg@31
|
314 |
super(file);
|
franta-hg@126
|
315 |
COLOR_ENABLED = true;
|
franta-hg@31
|
316 |
}
|
franta-hg@31
|
317 |
|
franta-hg@31
|
318 |
public ColorfulPrintWriter(OutputStream out) {
|
franta-hg@31
|
319 |
super(out);
|
franta-hg@126
|
320 |
COLOR_ENABLED = true;
|
franta-hg@31
|
321 |
}
|
franta-hg@31
|
322 |
|
franta-hg@31
|
323 |
public ColorfulPrintWriter(String fileName) throws FileNotFoundException {
|
franta-hg@31
|
324 |
super(fileName);
|
franta-hg@126
|
325 |
COLOR_ENABLED = true;
|
franta-hg@31
|
326 |
}
|
franta-hg@31
|
327 |
|
franta-hg@31
|
328 |
public ColorfulPrintWriter(Writer out) {
|
franta-hg@31
|
329 |
super(out);
|
franta-hg@126
|
330 |
COLOR_ENABLED = true;
|
franta-hg@31
|
331 |
}
|
franta-hg@31
|
332 |
|
franta-hg@31
|
333 |
public ColorfulPrintWriter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException {
|
franta-hg@31
|
334 |
super(file, csn);
|
franta-hg@126
|
335 |
COLOR_ENABLED = true;
|
franta-hg@31
|
336 |
}
|
franta-hg@31
|
337 |
|
franta-hg@126
|
338 |
/**
|
franta-hg@126
|
339 |
* @param colorEnabled colors might be definitively disabled by this option – this might be more
|
franta-hg@126
|
340 |
* optimalizable than dynamic turning off colors by {@linkplain #setColorful(boolean)} which is
|
franta-hg@126
|
341 |
* not definitive (colors can be turned on during live of this instance). This might be useful
|
franta-hg@126
|
342 |
* if you need an instance of this class but don't need colors at all.
|
franta-hg@126
|
343 |
*/
|
franta-hg@126
|
344 |
public ColorfulPrintWriter(OutputStream out, boolean autoFlush, boolean colorEnabled) {
|
franta-hg@31
|
345 |
super(out, autoFlush);
|
franta-hg@126
|
346 |
COLOR_ENABLED = colorEnabled;
|
franta-hg@31
|
347 |
}
|
franta-hg@31
|
348 |
|
franta-hg@31
|
349 |
public ColorfulPrintWriter(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException {
|
franta-hg@31
|
350 |
super(fileName, csn);
|
franta-hg@126
|
351 |
COLOR_ENABLED = true;
|
franta-hg@31
|
352 |
}
|
franta-hg@31
|
353 |
|
franta-hg@31
|
354 |
public ColorfulPrintWriter(Writer out, boolean autoFlush) {
|
franta-hg@31
|
355 |
super(out, autoFlush);
|
franta-hg@126
|
356 |
COLOR_ENABLED = true;
|
franta-hg@31
|
357 |
}
|
franta-hg@31
|
358 |
}
|