# HG changeset patch # User František Kučera # Date 1387741892 -3600 # Node ID ef2fdb55e8ec573a69ad6792edf07f9534c1ab3e # Parent b7ea47b2d4cac23d1bf6b31a804dd6201caf2421 ColorfulPrintWriter: colorz, wow diff -r b7ea47b2d4ca -r ef2fdb55e8ec java/sql-dk/src/info/globalcode/sql/dk/ColorfulPrintWriter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/sql-dk/src/info/globalcode/sql/dk/ColorfulPrintWriter.java Sun Dec 22 20:51:32 2013 +0100 @@ -0,0 +1,225 @@ +/** + * SQL-DK + * Copyright © 2013 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package info.globalcode.sql.dk; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; +import java.io.Writer; +import java.util.EnumSet; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class ColorfulPrintWriter extends PrintWriter { + + public enum TerminalColor { + + Black(30, 40), + Red(31, 41), + Green(32, 42), + Yellow(33, 43), + Blue(34, 44), + Magenta(35, 45), + Cyan(36, 46), + White(37, 47); + private final int foregroundCode; + private final int backgroundCode; + + private TerminalColor(int foregroundCode, int backgroundCode) { + this.foregroundCode = foregroundCode; + this.backgroundCode = backgroundCode; + } + + public int getForegroundCode() { + return foregroundCode; + } + + public int getBackgroundCode() { + return backgroundCode; + } + } + + public enum TerminalStyle { + + Reset(0), + Bright(1), + Dim(2), + Underscore(4), + Blink(5), + Reverse(7), + Hidden(8); + private int code; + + private TerminalStyle(int code) { + this.code = code; + } + + public int getCode() { + return code; + } + } + private boolean colorful = true; + + public void setStyle(EnumSet styles) { + printCodes(getStyleCodes(styles)); + } + + private static int[] getStyleCodes(EnumSet styles) { + int[] array = new int[styles.size()]; + int i = 0; + for (TerminalStyle s : styles) { + array[i++] = s.getCode(); + } + return array; + } + + public void setForegroundColor(TerminalColor color) { + printCodes(color.getForegroundCode()); + } + + public void setBackgroundColor(TerminalColor color) { + printCodes(color.getBackgroundCode()); + } + + public void print(TerminalColor foregroundColor, String string) { + setForegroundColor(foregroundColor); + print(string); + resetAll(); + } + + public void println(TerminalColor foregroundColor, String string) { + print(foregroundColor, string); + println(); + } + + public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, String string) { + setForegroundColor(foregroundColor); + setBackgroundColor(backgroundColor); + print(string); + resetAll(); + } + + public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, String string) { + print(foregroundColor, backgroundColor, string); + println(); + } + + public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, EnumSet styles, String string) { + setForegroundColor(foregroundColor); + setBackgroundColor(backgroundColor); + setStyle(styles); + print(string); + resetAll(); + } + + public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, EnumSet styles, String string) { + print(foregroundColor, backgroundColor, styles, string); + println(); + } + + public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, TerminalStyle style, String string) { + print(foregroundColor, backgroundColor, EnumSet.of(style), string); + } + + public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, TerminalStyle style, String string) { + print(foregroundColor, backgroundColor, style, string); + println(); + } + + public void print(TerminalColor foregroundColor, EnumSet styles, String string) { + setForegroundColor(foregroundColor); + setStyle(styles); + print(string); + resetAll(); + } + + public void println(TerminalColor foregroundColor, EnumSet styles, String string) { + print(foregroundColor, styles, string); + println(); + } + + public void print(TerminalColor foregroundColor, TerminalStyle style, String string) { + print(foregroundColor, EnumSet.of(style), string); + } + + public void println(TerminalColor foregroundColor, TerminalStyle style, String string) { + print(foregroundColor, style, string); + println(); + } + + public void resetAll() { + printCodes(0); + } + + private void printCodes(int... codes) { + if (colorful) { + print("\033["); + for (int i = 0; i < codes.length; i++) { + print(codes[i]); + if (i < codes.length - 1 && codes.length > 1) { + print(";"); + } + } + print("m"); + } + } + + public boolean isColorful() { + return colorful; + } + + public void setColorful(boolean colorful) { + this.colorful = colorful; + } + + public ColorfulPrintWriter(File file) throws FileNotFoundException { + super(file); + } + + public ColorfulPrintWriter(OutputStream out) { + super(out); + } + + public ColorfulPrintWriter(String fileName) throws FileNotFoundException { + super(fileName); + } + + public ColorfulPrintWriter(Writer out) { + super(out); + } + + public ColorfulPrintWriter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException { + super(file, csn); + } + + public ColorfulPrintWriter(OutputStream out, boolean autoFlush) { + super(out, autoFlush); + } + + public ColorfulPrintWriter(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException { + super(fileName, csn); + } + + public ColorfulPrintWriter(Writer out, boolean autoFlush) { + super(out, autoFlush); + } +}