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 boolean colorful = true;
82 public void setStyle(TerminalStyle style) {
83 setStyle(EnumSet.of(style));
86 public void setStyle(EnumSet<TerminalStyle> styles) {
87 printCodes(getStyleCodes(styles));
90 private static int[] getStyleCodes(EnumSet<TerminalStyle> styles) {
91 int[] array = new int[styles.size()];
93 for (TerminalStyle s : styles) {
94 array[i++] = s.getCode();
100 * Print (usually audible) bell code (\007, \a, ^G)
107 * Eat the last character
109 public void backspace() {
114 * Eat n last characters
118 public void backspace(int count) {
119 for (int i = 0; i < count; i++) {
125 * With 100 ms delay and all colors.
127 * @see #printRainbow(java.lang.String, int,
128 * info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor[])
130 public void printRainbow(String string) {
131 printRainbow(string, 100);
137 * @see #printRainbow(java.lang.String, int,
138 * info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor[])
140 public void printRainbow(String string, int delay) {
141 printRainbow(string, delay, TerminalColor.values());
145 * Prints rainbow text – (re)writes same text subsequently in given colors and then in default
148 * @param string text to be printed, should not contain \n new line (then rainbow does not work
149 * – use println() after printRainbow() instead)
150 * @param delay delay between rewrites
151 * @param colors list of colors to be used
153 public void printRainbow(String string, int delay, TerminalColor... colors) {
154 for (TerminalColor c : colors) {
158 } catch (InterruptedException e) {
162 backspace(string.length());
168 public void setForegroundColor(TerminalColor color) {
169 printCodes(color.getForegroundCode());
172 public void setBackgroundColor(TerminalColor color) {
173 printCodes(color.getBackgroundCode());
176 public void print(TerminalColor foregroundColor, String string) {
177 setForegroundColor(foregroundColor);
182 public void println(TerminalColor foregroundColor, String string) {
183 print(foregroundColor, string);
187 public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, String string) {
188 setForegroundColor(foregroundColor);
189 setBackgroundColor(backgroundColor);
194 public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, String string) {
195 print(foregroundColor, backgroundColor, string);
199 public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, EnumSet<TerminalStyle> styles, String string) {
200 setForegroundColor(foregroundColor);
201 setBackgroundColor(backgroundColor);
207 public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, EnumSet<TerminalStyle> styles, String string) {
208 print(foregroundColor, backgroundColor, styles, string);
212 public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, TerminalStyle style, String string) {
213 print(foregroundColor, backgroundColor, EnumSet.of(style), string);
216 public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, TerminalStyle style, String string) {
217 print(foregroundColor, backgroundColor, style, string);
221 public void print(TerminalColor foregroundColor, EnumSet<TerminalStyle> styles, String string) {
222 setForegroundColor(foregroundColor);
228 public void println(TerminalColor foregroundColor, EnumSet<TerminalStyle> styles, String string) {
229 print(foregroundColor, styles, string);
233 public void print(TerminalColor foregroundColor, TerminalStyle style, String string) {
234 print(foregroundColor, EnumSet.of(style), string);
237 public void println(TerminalColor foregroundColor, TerminalStyle style, String string) {
238 print(foregroundColor, style, string);
242 public void print(EnumSet<TerminalStyle> styles, String string) {
248 public void println(EnumSet<TerminalStyle> styles, String string) {
249 print(styles, string);
253 public void print(TerminalStyle style, String string) {
254 print(EnumSet.of(style), string);
257 public void println(TerminalStyle style, String string) {
258 print(style, string);
262 public void resetAll() {
263 printCodes(TerminalStyle.Reset.code);
266 private void printCodes(int... codes) {
269 for (int i = 0; i < codes.length; i++) {
271 if (i < codes.length - 1 && codes.length > 1) {
279 public boolean isColorful() {
283 public void setColorful(boolean colorful) {
284 this.colorful = colorful;
287 public ColorfulPrintWriter(File file) throws FileNotFoundException {
291 public ColorfulPrintWriter(OutputStream out) {
295 public ColorfulPrintWriter(String fileName) throws FileNotFoundException {
299 public ColorfulPrintWriter(Writer out) {
303 public ColorfulPrintWriter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException {
307 public ColorfulPrintWriter(OutputStream out, boolean autoFlush) {
308 super(out, autoFlush);
311 public ColorfulPrintWriter(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException {
312 super(fileName, csn);
315 public ColorfulPrintWriter(Writer out, boolean autoFlush) {
316 super(out, autoFlush);