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;
30 * @author Ing. František Kučera (frantovo.cz)
32 public class ColorfulPrintWriter extends PrintWriter {
34 public enum TerminalColor {
44 private final int foregroundCode;
45 private final int backgroundCode;
47 private TerminalColor(int foregroundCode, int backgroundCode) {
48 this.foregroundCode = foregroundCode;
49 this.backgroundCode = backgroundCode;
52 public int getForegroundCode() {
53 return foregroundCode;
56 public int getBackgroundCode() {
57 return backgroundCode;
61 public enum TerminalStyle {
72 private TerminalStyle(int code) {
76 public int getCode() {
80 private final boolean COLOR_ENABLED;
81 private boolean colorful = true;
83 public void setStyle(TerminalStyle style) {
84 setStyle(EnumSet.of(style));
87 public void setStyle(EnumSet<TerminalStyle> styles) {
88 printCodes(getStyleCodes(styles));
91 private static int[] getStyleCodes(EnumSet<TerminalStyle> styles) {
92 int[] array = new int[styles.size()];
94 for (TerminalStyle s : styles) {
95 array[i++] = s.getCode();
101 * Print (usually audible) bell code (\007, \a, ^G)
108 * Eat the last character
110 public void backspace() {
115 * Eat n last characters
119 public void backspace(int count) {
120 for (int i = 0; i < count; i++) {
126 * With 100 ms delay and all colors.
128 * @see #printRainbow(java.lang.String, int,
129 * info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor[])
131 public void printRainbow(String string) {
132 printRainbow(string, 100);
138 * @see #printRainbow(java.lang.String, int,
139 * info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor[])
141 public void printRainbow(String string, int delay) {
142 printRainbow(string, delay, TerminalColor.values());
146 * Prints rainbow text – (re)writes same text subsequently in given colors and then in default
149 * @param string text to be printed, should not contain \n new line (then rainbow does not work
150 * – use println() after printRainbow() instead)
151 * @param delay delay between rewrites
152 * @param colors list of colors to be used
154 public void printRainbow(String string, int delay, TerminalColor... colors) {
155 for (TerminalColor c : colors) {
159 } catch (InterruptedException e) {
163 backspace(string.length());
169 public void setForegroundColor(TerminalColor color) {
170 printCodes(color.getForegroundCode());
173 public void setBackgroundColor(TerminalColor color) {
174 printCodes(color.getBackgroundCode());
177 public void print(TerminalColor foregroundColor, String string) {
178 setForegroundColor(foregroundColor);
183 public void println(TerminalColor foregroundColor, String string) {
184 print(foregroundColor, string);
188 public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, String string) {
189 setForegroundColor(foregroundColor);
190 setBackgroundColor(backgroundColor);
195 public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, String string) {
196 print(foregroundColor, backgroundColor, string);
200 public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, EnumSet<TerminalStyle> styles, String string) {
201 setForegroundColor(foregroundColor);
202 setBackgroundColor(backgroundColor);
208 public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, EnumSet<TerminalStyle> styles, String string) {
209 print(foregroundColor, backgroundColor, styles, string);
213 public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, TerminalStyle style, String string) {
214 print(foregroundColor, backgroundColor, EnumSet.of(style), string);
217 public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, TerminalStyle style, String string) {
218 print(foregroundColor, backgroundColor, style, string);
222 public void print(TerminalColor foregroundColor, EnumSet<TerminalStyle> styles, String string) {
223 setForegroundColor(foregroundColor);
229 public void println(TerminalColor foregroundColor, EnumSet<TerminalStyle> styles, String string) {
230 print(foregroundColor, styles, string);
234 public void print(TerminalColor foregroundColor, TerminalStyle style, String string) {
235 print(foregroundColor, EnumSet.of(style), string);
238 public void println(TerminalColor foregroundColor, TerminalStyle style, String string) {
239 print(foregroundColor, style, string);
243 public void print(EnumSet<TerminalStyle> styles, String string) {
249 public void println(EnumSet<TerminalStyle> styles, String string) {
250 print(styles, string);
254 public void print(TerminalStyle style, String string) {
255 print(EnumSet.of(style), string);
258 public void println(TerminalStyle style, String string) {
259 print(style, string);
263 public void resetAll() {
264 printCodes(TerminalStyle.Reset.code);
267 private void printCodes(int... codes) {
268 if (COLOR_ENABLED && colorful) {
270 for (int i = 0; i < codes.length; i++) {
272 if (i < codes.length - 1 && codes.length > 1) {
281 * Colors can be switched on/off during usage of this writer.
283 * @return whether colors are currently turned on
284 * @see #isColorEnabled()
286 public boolean isColorful() {
291 * Collors might be definitively disabled in constructor. If not, they can be turned on/off
292 * during usage of this writer by {@linkplain #setColorful(boolean)}
294 * @return whether colors are allowed for this instance of this class
297 public boolean isColorEnabled() {
298 return COLOR_ENABLED;
303 * @see #isColorEnabled()
305 public void setColorful(boolean colorful) {
306 this.colorful = colorful;
309 public ColorfulPrintWriter(File file) throws FileNotFoundException {
311 COLOR_ENABLED = true;
314 public ColorfulPrintWriter(OutputStream out) {
316 COLOR_ENABLED = true;
319 public ColorfulPrintWriter(String fileName) throws FileNotFoundException {
321 COLOR_ENABLED = true;
324 public ColorfulPrintWriter(Writer out) {
326 COLOR_ENABLED = true;
329 public ColorfulPrintWriter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException {
331 COLOR_ENABLED = true;
335 * @param colorEnabled colors might be definitively disabled by this option – this might be more
336 * optimalizable than dynamic turning off colors by {@linkplain #setColorful(boolean)} which is
337 * not definitive (colors can be turned on during live of this instance). This might be useful
338 * if you need an instance of this class but don't need colors at all.
340 public ColorfulPrintWriter(OutputStream out, boolean autoFlush, boolean colorEnabled) {
341 super(out, autoFlush);
342 COLOR_ENABLED = colorEnabled;
345 public ColorfulPrintWriter(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException {
346 super(fileName, csn);
347 COLOR_ENABLED = true;
350 public ColorfulPrintWriter(Writer out, boolean autoFlush) {
351 super(out, autoFlush);
352 COLOR_ENABLED = true;