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(EnumSet<TerminalStyle> styles) {
83 printCodes(getStyleCodes(styles));
86 private static int[] getStyleCodes(EnumSet<TerminalStyle> styles) {
87 int[] array = new int[styles.size()];
89 for (TerminalStyle s : styles) {
90 array[i++] = s.getCode();
96 * Print (usually audible) bell code (\007, \a, ^G)
103 * Eat the last character
105 public void backspace() {
110 * Eat n last characters
114 public void backspace(int count) {
115 for (int i = 0; i < count; i++) {
121 * With 100 ms delay and all colors.
123 * @see #printRainbow(java.lang.String, int,
124 * info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor[])
126 public void printRainbow(String string) {
127 printRainbow(string, 100);
133 * @see #printRainbow(java.lang.String, int,
134 * info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor[])
136 public void printRainbow(String string, int delay) {
137 printRainbow(string, delay, TerminalColor.values());
141 * Prints rainbow text – (re)writes same text subsequently in given colors and then in default
144 * @param string text to be printed, should not contain \n new line (then rainbow does not work
145 * – use println() after printRainbow() instead)
146 * @param delay delay between rewrites
147 * @param colors list of colors to be used
149 public void printRainbow(String string, int delay, TerminalColor... colors) {
150 for (TerminalColor c : colors) {
154 } catch (InterruptedException e) {
158 backspace(string.length());
164 public void setForegroundColor(TerminalColor color) {
165 printCodes(color.getForegroundCode());
168 public void setBackgroundColor(TerminalColor color) {
169 printCodes(color.getBackgroundCode());
172 public void print(TerminalColor foregroundColor, String string) {
173 setForegroundColor(foregroundColor);
178 public void println(TerminalColor foregroundColor, String string) {
179 print(foregroundColor, string);
183 public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, String string) {
184 setForegroundColor(foregroundColor);
185 setBackgroundColor(backgroundColor);
190 public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, String string) {
191 print(foregroundColor, backgroundColor, string);
195 public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, EnumSet<TerminalStyle> styles, String string) {
196 setForegroundColor(foregroundColor);
197 setBackgroundColor(backgroundColor);
203 public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, EnumSet<TerminalStyle> styles, String string) {
204 print(foregroundColor, backgroundColor, styles, string);
208 public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, TerminalStyle style, String string) {
209 print(foregroundColor, backgroundColor, EnumSet.of(style), string);
212 public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, TerminalStyle style, String string) {
213 print(foregroundColor, backgroundColor, style, string);
217 public void print(TerminalColor foregroundColor, EnumSet<TerminalStyle> styles, String string) {
218 setForegroundColor(foregroundColor);
224 public void println(TerminalColor foregroundColor, EnumSet<TerminalStyle> styles, String string) {
225 print(foregroundColor, styles, string);
229 public void print(TerminalColor foregroundColor, TerminalStyle style, String string) {
230 print(foregroundColor, EnumSet.of(style), string);
233 public void println(TerminalColor foregroundColor, TerminalStyle style, String string) {
234 print(foregroundColor, style, string);
238 public void print(EnumSet<TerminalStyle> styles, String string) {
244 public void println(EnumSet<TerminalStyle> styles, String string) {
245 print(styles, string);
249 public void print(TerminalStyle style, String string) {
250 print(EnumSet.of(style), string);
253 public void println(TerminalStyle style, String string) {
254 print(style, string);
258 public void resetAll() {
259 printCodes(TerminalStyle.Reset.code);
262 private void printCodes(int... codes) {
265 for (int i = 0; i < codes.length; i++) {
267 if (i < codes.length - 1 && codes.length > 1) {
275 public boolean isColorful() {
279 public void setColorful(boolean colorful) {
280 this.colorful = colorful;
283 public ColorfulPrintWriter(File file) throws FileNotFoundException {
287 public ColorfulPrintWriter(OutputStream out) {
291 public ColorfulPrintWriter(String fileName) throws FileNotFoundException {
295 public ColorfulPrintWriter(Writer out) {
299 public ColorfulPrintWriter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException {
303 public ColorfulPrintWriter(OutputStream out, boolean autoFlush) {
304 super(out, autoFlush);
307 public ColorfulPrintWriter(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException {
308 super(fileName, csn);
311 public ColorfulPrintWriter(Writer out, boolean autoFlush) {
312 super(out, autoFlush);