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, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package info.globalcode.sql.dk;
21 import java.io.FileNotFoundException;
22 import java.io.OutputStream;
23 import java.io.PrintWriter;
24 import java.io.UnsupportedEncodingException;
25 import java.io.Writer;
26 import java.util.EnumSet;
29 * PrintWriter with convenience methods for printing color and formatted text.
31 * Uses ANSI Escape Sequences.
32 * See: http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
34 * @author Ing. František Kučera (frantovo.cz)
36 public class ColorfulPrintWriter extends PrintWriter {
38 public enum TerminalColor {
48 private final int foregroundCode;
49 private final int backgroundCode;
51 private TerminalColor(int foregroundCode, int backgroundCode) {
52 this.foregroundCode = foregroundCode;
53 this.backgroundCode = backgroundCode;
56 public int getForegroundCode() {
57 return foregroundCode;
60 public int getBackgroundCode() {
61 return backgroundCode;
65 public enum TerminalStyle {
76 private TerminalStyle(int code) {
80 public int getCode() {
84 private final boolean COLOR_ENABLED;
85 private boolean colorful = true;
87 public void setStyle(TerminalStyle style) {
88 setStyle(EnumSet.of(style));
91 public void setStyle(EnumSet<TerminalStyle> styles) {
92 printCodes(getStyleCodes(styles));
95 private static int[] getStyleCodes(EnumSet<TerminalStyle> styles) {
96 int[] array = new int[styles.size()];
98 for (TerminalStyle s : styles) {
99 array[i++] = s.getCode();
105 * Print (usually audible) bell code (\007, \a, ^G)
112 * Eat the last character
114 public void backspace() {
119 * Eat n last characters
123 public void backspace(int count) {
124 for (int i = 0; i < count; i++) {
130 * With 100 ms delay and all colors.
132 * @see #printRainbow(java.lang.String, int,
133 * info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor[])
135 public void printRainbow(String string) {
136 printRainbow(string, 100);
142 * @see #printRainbow(java.lang.String, int,
143 * info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor[])
145 public void printRainbow(String string, int delay) {
146 printRainbow(string, delay, TerminalColor.values());
150 * Prints rainbow text – (re)writes same text subsequently in given colors and then in default
153 * @param string text to be printed, should not contain \n new line (then rainbow does not work
154 * – use println() after printRainbow() instead)
155 * @param delay delay between rewrites
156 * @param colors list of colors to be used
158 public void printRainbow(String string, int delay, TerminalColor... colors) {
159 for (TerminalColor c : colors) {
163 } catch (InterruptedException e) {
167 backspace(string.length());
173 public void setForegroundColor(TerminalColor color) {
174 printCodes(color.getForegroundCode());
177 public void setBackgroundColor(TerminalColor color) {
178 printCodes(color.getBackgroundCode());
181 public void print(TerminalColor foregroundColor, String string) {
182 setForegroundColor(foregroundColor);
187 public void println(TerminalColor foregroundColor, String string) {
188 print(foregroundColor, string);
192 public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, String string) {
193 setForegroundColor(foregroundColor);
194 setBackgroundColor(backgroundColor);
199 public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, String string) {
200 print(foregroundColor, backgroundColor, string);
204 public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, EnumSet<TerminalStyle> styles, String string) {
205 setForegroundColor(foregroundColor);
206 setBackgroundColor(backgroundColor);
212 public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, EnumSet<TerminalStyle> styles, String string) {
213 print(foregroundColor, backgroundColor, styles, string);
217 public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, TerminalStyle style, String string) {
218 print(foregroundColor, backgroundColor, EnumSet.of(style), string);
221 public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, TerminalStyle style, String string) {
222 print(foregroundColor, backgroundColor, style, string);
226 public void print(TerminalColor foregroundColor, EnumSet<TerminalStyle> styles, String string) {
227 setForegroundColor(foregroundColor);
233 public void println(TerminalColor foregroundColor, EnumSet<TerminalStyle> styles, String string) {
234 print(foregroundColor, styles, string);
238 public void print(TerminalColor foregroundColor, TerminalStyle style, String string) {
239 print(foregroundColor, EnumSet.of(style), string);
242 public void println(TerminalColor foregroundColor, TerminalStyle style, String string) {
243 print(foregroundColor, style, string);
247 public void print(EnumSet<TerminalStyle> styles, String string) {
253 public void println(EnumSet<TerminalStyle> styles, String string) {
254 print(styles, string);
258 public void print(TerminalStyle style, String string) {
259 print(EnumSet.of(style), string);
262 public void println(TerminalStyle style, String string) {
263 print(style, string);
267 public void resetAll() {
268 printCodes(TerminalStyle.Reset.code);
271 private void printCodes(int... codes) {
272 if (COLOR_ENABLED && colorful) {
274 for (int i = 0; i < codes.length; i++) {
276 if (i < codes.length - 1 && codes.length > 1) {
285 * Colors can be switched on/off during usage of this writer.
287 * @return whether colors are currently turned on
288 * @see #isColorEnabled()
290 public boolean isColorful() {
295 * Collors might be definitively disabled in constructor. If not, they can be turned on/off
296 * during usage of this writer by {@linkplain #setColorful(boolean)}
298 * @return whether colors are allowed for this instance of this class
301 public boolean isColorEnabled() {
302 return COLOR_ENABLED;
307 * @see #isColorEnabled()
309 public void setColorful(boolean colorful) {
310 this.colorful = colorful;
313 public ColorfulPrintWriter(File file) throws FileNotFoundException {
315 COLOR_ENABLED = true;
318 public ColorfulPrintWriter(OutputStream out) {
320 COLOR_ENABLED = true;
323 public ColorfulPrintWriter(String fileName) throws FileNotFoundException {
325 COLOR_ENABLED = true;
328 public ColorfulPrintWriter(Writer out) {
330 COLOR_ENABLED = true;
333 public ColorfulPrintWriter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException {
335 COLOR_ENABLED = true;
339 * @param colorEnabled colors might be definitively disabled by this option – this might be more
340 * optimalizable than dynamic turning off colors by {@linkplain #setColorful(boolean)} which is
341 * not definitive (colors can be turned on during live of this instance). This might be useful
342 * if you need an instance of this class but don't need colors at all.
344 public ColorfulPrintWriter(OutputStream out, boolean autoFlush, boolean colorEnabled) {
345 super(out, autoFlush);
346 COLOR_ENABLED = colorEnabled;
349 public ColorfulPrintWriter(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException {
350 super(fileName, csn);
351 COLOR_ENABLED = true;
354 public ColorfulPrintWriter(Writer out, boolean autoFlush) {
355 super(out, autoFlush);
356 COLOR_ENABLED = true;