1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/ColorfulPrintWriter.java Sun Dec 22 20:51:32 2013 +0100
1.3 @@ -0,0 +1,225 @@
1.4 +/**
1.5 + * SQL-DK
1.6 + * Copyright © 2013 František Kučera (frantovo.cz)
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, either version 3 of the License, or
1.11 + * (at your option) any later version.
1.12 + *
1.13 + * This program is distributed in the hope that it will be useful,
1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.16 + * GNU General Public License for more details.
1.17 + *
1.18 + * You should have received a copy of the GNU General Public License
1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.20 + */
1.21 +package info.globalcode.sql.dk;
1.22 +
1.23 +import java.io.File;
1.24 +import java.io.FileNotFoundException;
1.25 +import java.io.OutputStream;
1.26 +import java.io.PrintWriter;
1.27 +import java.io.UnsupportedEncodingException;
1.28 +import java.io.Writer;
1.29 +import java.util.EnumSet;
1.30 +
1.31 +/**
1.32 + *
1.33 + * @author Ing. František Kučera (frantovo.cz)
1.34 + */
1.35 +public class ColorfulPrintWriter extends PrintWriter {
1.36 +
1.37 + public enum TerminalColor {
1.38 +
1.39 + Black(30, 40),
1.40 + Red(31, 41),
1.41 + Green(32, 42),
1.42 + Yellow(33, 43),
1.43 + Blue(34, 44),
1.44 + Magenta(35, 45),
1.45 + Cyan(36, 46),
1.46 + White(37, 47);
1.47 + private final int foregroundCode;
1.48 + private final int backgroundCode;
1.49 +
1.50 + private TerminalColor(int foregroundCode, int backgroundCode) {
1.51 + this.foregroundCode = foregroundCode;
1.52 + this.backgroundCode = backgroundCode;
1.53 + }
1.54 +
1.55 + public int getForegroundCode() {
1.56 + return foregroundCode;
1.57 + }
1.58 +
1.59 + public int getBackgroundCode() {
1.60 + return backgroundCode;
1.61 + }
1.62 + }
1.63 +
1.64 + public enum TerminalStyle {
1.65 +
1.66 + Reset(0),
1.67 + Bright(1),
1.68 + Dim(2),
1.69 + Underscore(4),
1.70 + Blink(5),
1.71 + Reverse(7),
1.72 + Hidden(8);
1.73 + private int code;
1.74 +
1.75 + private TerminalStyle(int code) {
1.76 + this.code = code;
1.77 + }
1.78 +
1.79 + public int getCode() {
1.80 + return code;
1.81 + }
1.82 + }
1.83 + private boolean colorful = true;
1.84 +
1.85 + public void setStyle(EnumSet<TerminalStyle> styles) {
1.86 + printCodes(getStyleCodes(styles));
1.87 + }
1.88 +
1.89 + private static int[] getStyleCodes(EnumSet<TerminalStyle> styles) {
1.90 + int[] array = new int[styles.size()];
1.91 + int i = 0;
1.92 + for (TerminalStyle s : styles) {
1.93 + array[i++] = s.getCode();
1.94 + }
1.95 + return array;
1.96 + }
1.97 +
1.98 + public void setForegroundColor(TerminalColor color) {
1.99 + printCodes(color.getForegroundCode());
1.100 + }
1.101 +
1.102 + public void setBackgroundColor(TerminalColor color) {
1.103 + printCodes(color.getBackgroundCode());
1.104 + }
1.105 +
1.106 + public void print(TerminalColor foregroundColor, String string) {
1.107 + setForegroundColor(foregroundColor);
1.108 + print(string);
1.109 + resetAll();
1.110 + }
1.111 +
1.112 + public void println(TerminalColor foregroundColor, String string) {
1.113 + print(foregroundColor, string);
1.114 + println();
1.115 + }
1.116 +
1.117 + public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, String string) {
1.118 + setForegroundColor(foregroundColor);
1.119 + setBackgroundColor(backgroundColor);
1.120 + print(string);
1.121 + resetAll();
1.122 + }
1.123 +
1.124 + public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, String string) {
1.125 + print(foregroundColor, backgroundColor, string);
1.126 + println();
1.127 + }
1.128 +
1.129 + public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, EnumSet<TerminalStyle> styles, String string) {
1.130 + setForegroundColor(foregroundColor);
1.131 + setBackgroundColor(backgroundColor);
1.132 + setStyle(styles);
1.133 + print(string);
1.134 + resetAll();
1.135 + }
1.136 +
1.137 + public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, EnumSet<TerminalStyle> styles, String string) {
1.138 + print(foregroundColor, backgroundColor, styles, string);
1.139 + println();
1.140 + }
1.141 +
1.142 + public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, TerminalStyle style, String string) {
1.143 + print(foregroundColor, backgroundColor, EnumSet.of(style), string);
1.144 + }
1.145 +
1.146 + public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, TerminalStyle style, String string) {
1.147 + print(foregroundColor, backgroundColor, style, string);
1.148 + println();
1.149 + }
1.150 +
1.151 + public void print(TerminalColor foregroundColor, EnumSet<TerminalStyle> styles, String string) {
1.152 + setForegroundColor(foregroundColor);
1.153 + setStyle(styles);
1.154 + print(string);
1.155 + resetAll();
1.156 + }
1.157 +
1.158 + public void println(TerminalColor foregroundColor, EnumSet<TerminalStyle> styles, String string) {
1.159 + print(foregroundColor, styles, string);
1.160 + println();
1.161 + }
1.162 +
1.163 + public void print(TerminalColor foregroundColor, TerminalStyle style, String string) {
1.164 + print(foregroundColor, EnumSet.of(style), string);
1.165 + }
1.166 +
1.167 + public void println(TerminalColor foregroundColor, TerminalStyle style, String string) {
1.168 + print(foregroundColor, style, string);
1.169 + println();
1.170 + }
1.171 +
1.172 + public void resetAll() {
1.173 + printCodes(0);
1.174 + }
1.175 +
1.176 + private void printCodes(int... codes) {
1.177 + if (colorful) {
1.178 + print("\033[");
1.179 + for (int i = 0; i < codes.length; i++) {
1.180 + print(codes[i]);
1.181 + if (i < codes.length - 1 && codes.length > 1) {
1.182 + print(";");
1.183 + }
1.184 + }
1.185 + print("m");
1.186 + }
1.187 + }
1.188 +
1.189 + public boolean isColorful() {
1.190 + return colorful;
1.191 + }
1.192 +
1.193 + public void setColorful(boolean colorful) {
1.194 + this.colorful = colorful;
1.195 + }
1.196 +
1.197 + public ColorfulPrintWriter(File file) throws FileNotFoundException {
1.198 + super(file);
1.199 + }
1.200 +
1.201 + public ColorfulPrintWriter(OutputStream out) {
1.202 + super(out);
1.203 + }
1.204 +
1.205 + public ColorfulPrintWriter(String fileName) throws FileNotFoundException {
1.206 + super(fileName);
1.207 + }
1.208 +
1.209 + public ColorfulPrintWriter(Writer out) {
1.210 + super(out);
1.211 + }
1.212 +
1.213 + public ColorfulPrintWriter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException {
1.214 + super(file, csn);
1.215 + }
1.216 +
1.217 + public ColorfulPrintWriter(OutputStream out, boolean autoFlush) {
1.218 + super(out, autoFlush);
1.219 + }
1.220 +
1.221 + public ColorfulPrintWriter(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException {
1.222 + super(fileName, csn);
1.223 + }
1.224 +
1.225 + public ColorfulPrintWriter(Writer out, boolean autoFlush) {
1.226 + super(out, autoFlush);
1.227 + }
1.228 +}