3 * Copyright © 2013 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package info.globalcode.sql.dk;
20 import java.io.FileNotFoundException;
21 import java.io.OutputStream;
22 import java.io.PrintWriter;
23 import java.io.UnsupportedEncodingException;
24 import java.io.Writer;
25 import java.util.EnumSet;
28 * PrintWriter with convenience methods for printing color and formatted text.
30 * Uses ANSI Escape Sequences.
31 * See: http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
33 * @author Ing. František Kučera (frantovo.cz)
35 public class ColorfulPrintWriter extends PrintWriter {
37 public enum TerminalColor {
47 private final int foregroundCode;
48 private final int backgroundCode;
50 private TerminalColor(int foregroundCode, int backgroundCode) {
51 this.foregroundCode = foregroundCode;
52 this.backgroundCode = backgroundCode;
55 public int getForegroundCode() {
56 return foregroundCode;
59 public int getBackgroundCode() {
60 return backgroundCode;
64 public enum TerminalStyle {
75 private TerminalStyle(int code) {
79 public int getCode() {
83 private final boolean COLOR_ENABLED;
84 private boolean colorful = true;
86 public void setStyle(TerminalStyle style) {
87 setStyle(EnumSet.of(style));
90 public void setStyle(EnumSet<TerminalStyle> styles) {
91 printCodes(getStyleCodes(styles));
94 private static int[] getStyleCodes(EnumSet<TerminalStyle> styles) {
95 int[] array = new int[styles.size()];
97 for (TerminalStyle s : styles) {
98 array[i++] = s.getCode();
104 * Print (usually audible) bell code (\007, \a, ^G)
111 * Eat the last character
113 public void backspace() {
118 * Eat n last characters
122 public void backspace(int count) {
123 for (int i = 0; i < count; i++) {
129 * With 100 ms delay and all colors.
131 * @see #printRainbow(java.lang.String, int,
132 * info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor[])
134 public void printRainbow(String string) {
135 printRainbow(string, 100);
141 * @see #printRainbow(java.lang.String, int,
142 * info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor[])
144 public void printRainbow(String string, int delay) {
145 printRainbow(string, delay, TerminalColor.values());
149 * Prints rainbow text – (re)writes same text subsequently in given colors and then in default
152 * @param string text to be printed, should not contain \n new line (then rainbow does not work
153 * – use println() after printRainbow() instead)
154 * @param delay delay between rewrites
155 * @param colors list of colors to be used
157 public void printRainbow(String string, int delay, TerminalColor... colors) {
158 for (TerminalColor c : colors) {
162 } catch (InterruptedException e) {
166 backspace(string.length());
172 public void setForegroundColor(TerminalColor color) {
173 printCodes(color.getForegroundCode());
176 public void setBackgroundColor(TerminalColor color) {
177 printCodes(color.getBackgroundCode());
180 public void print(TerminalColor foregroundColor, String string) {
181 setForegroundColor(foregroundColor);
186 public void println(TerminalColor foregroundColor, String string) {
187 print(foregroundColor, string);
191 public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, String string) {
192 setForegroundColor(foregroundColor);
193 setBackgroundColor(backgroundColor);
198 public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, String string) {
199 print(foregroundColor, backgroundColor, string);
203 public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, EnumSet<TerminalStyle> styles, String string) {
204 setForegroundColor(foregroundColor);
205 setBackgroundColor(backgroundColor);
211 public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, EnumSet<TerminalStyle> styles, String string) {
212 print(foregroundColor, backgroundColor, styles, string);
216 public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, TerminalStyle style, String string) {
217 print(foregroundColor, backgroundColor, EnumSet.of(style), string);
220 public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, TerminalStyle style, String string) {
221 print(foregroundColor, backgroundColor, style, string);
225 public void print(TerminalColor foregroundColor, EnumSet<TerminalStyle> styles, String string) {
226 setForegroundColor(foregroundColor);
232 public void println(TerminalColor foregroundColor, EnumSet<TerminalStyle> styles, String string) {
233 print(foregroundColor, styles, string);
237 public void print(TerminalColor foregroundColor, TerminalStyle style, String string) {
238 print(foregroundColor, EnumSet.of(style), string);
241 public void println(TerminalColor foregroundColor, TerminalStyle style, String string) {
242 print(foregroundColor, style, string);
246 public void print(EnumSet<TerminalStyle> styles, String string) {
252 public void println(EnumSet<TerminalStyle> styles, String string) {
253 print(styles, string);
257 public void print(TerminalStyle style, String string) {
258 print(EnumSet.of(style), string);
261 public void println(TerminalStyle style, String string) {
262 print(style, string);
266 public void resetAll() {
267 printCodes(TerminalStyle.Reset.code);
270 private void printCodes(int... codes) {
271 if (COLOR_ENABLED && colorful) {
273 for (int i = 0; i < codes.length; i++) {
275 if (i < codes.length - 1 && codes.length > 1) {
284 * Colors can be switched on/off during usage of this writer.
286 * @return whether colors are currently turned on
287 * @see #isColorEnabled()
289 public boolean isColorful() {
294 * Collors might be definitively disabled in constructor. If not, they can be turned on/off
295 * during usage of this writer by {@linkplain #setColorful(boolean)}
297 * @return whether colors are allowed for this instance of this class
300 public boolean isColorEnabled() {
301 return COLOR_ENABLED;
306 * @see #isColorEnabled()
308 public void setColorful(boolean colorful) {
309 this.colorful = colorful;
312 public ColorfulPrintWriter(File file) throws FileNotFoundException {
314 COLOR_ENABLED = true;
317 public ColorfulPrintWriter(OutputStream out) {
319 COLOR_ENABLED = true;
322 public ColorfulPrintWriter(String fileName) throws FileNotFoundException {
324 COLOR_ENABLED = true;
327 public ColorfulPrintWriter(Writer out) {
329 COLOR_ENABLED = true;
332 public ColorfulPrintWriter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException {
334 COLOR_ENABLED = true;
338 * @param colorEnabled colors might be definitively disabled by this option – this might be more
339 * optimalizable than dynamic turning off colors by {@linkplain #setColorful(boolean)} which is
340 * not definitive (colors can be turned on during live of this instance). This might be useful
341 * if you need an instance of this class but don't need colors at all.
343 public ColorfulPrintWriter(OutputStream out, boolean autoFlush, boolean colorEnabled) {
344 super(out, autoFlush);
345 COLOR_ENABLED = colorEnabled;
348 public ColorfulPrintWriter(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException {
349 super(fileName, csn);
350 COLOR_ENABLED = true;
353 public ColorfulPrintWriter(Writer out, boolean autoFlush) {
354 super(out, autoFlush);
355 COLOR_ENABLED = true;