1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/java/sql-dk/src/main/java/info/globalcode/sql/dk/ColorfulPrintWriter.java Mon Mar 04 20:15:24 2019 +0100
1.3 @@ -0,0 +1,358 @@
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 + * PrintWriter with convenience methods for printing color and formatted text.
1.33 + *
1.34 + * Uses ANSI Escape Sequences.
1.35 + * See: http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
1.36 + *
1.37 + * @author Ing. František Kučera (frantovo.cz)
1.38 + */
1.39 +public class ColorfulPrintWriter extends PrintWriter {
1.40 +
1.41 + public enum TerminalColor {
1.42 +
1.43 + Black(30, 40),
1.44 + Red(31, 41),
1.45 + Green(32, 42),
1.46 + Yellow(33, 43),
1.47 + Blue(34, 44),
1.48 + Magenta(35, 45),
1.49 + Cyan(36, 46),
1.50 + White(37, 47);
1.51 + private final int foregroundCode;
1.52 + private final int backgroundCode;
1.53 +
1.54 + private TerminalColor(int foregroundCode, int backgroundCode) {
1.55 + this.foregroundCode = foregroundCode;
1.56 + this.backgroundCode = backgroundCode;
1.57 + }
1.58 +
1.59 + public int getForegroundCode() {
1.60 + return foregroundCode;
1.61 + }
1.62 +
1.63 + public int getBackgroundCode() {
1.64 + return backgroundCode;
1.65 + }
1.66 + }
1.67 +
1.68 + public enum TerminalStyle {
1.69 +
1.70 + Reset(0),
1.71 + Bright(1),
1.72 + Dim(2),
1.73 + Underscore(4),
1.74 + Blink(5),
1.75 + Reverse(7),
1.76 + Hidden(8);
1.77 + private int code;
1.78 +
1.79 + private TerminalStyle(int code) {
1.80 + this.code = code;
1.81 + }
1.82 +
1.83 + public int getCode() {
1.84 + return code;
1.85 + }
1.86 + }
1.87 + private final boolean COLOR_ENABLED;
1.88 + private boolean colorful = true;
1.89 +
1.90 + public void setStyle(TerminalStyle style) {
1.91 + setStyle(EnumSet.of(style));
1.92 + }
1.93 +
1.94 + public void setStyle(EnumSet<TerminalStyle> styles) {
1.95 + printCodes(getStyleCodes(styles));
1.96 + }
1.97 +
1.98 + private static int[] getStyleCodes(EnumSet<TerminalStyle> styles) {
1.99 + int[] array = new int[styles.size()];
1.100 + int i = 0;
1.101 + for (TerminalStyle s : styles) {
1.102 + array[i++] = s.getCode();
1.103 + }
1.104 + return array;
1.105 + }
1.106 +
1.107 + /**
1.108 + * Print (usually audible) bell code (\007, \a, ^G)
1.109 + */
1.110 + public void bell() {
1.111 + print("\007");
1.112 + }
1.113 +
1.114 + /**
1.115 + * Eat the last character
1.116 + */
1.117 + public void backspace() {
1.118 + print("\b");
1.119 + }
1.120 +
1.121 + /**
1.122 + * Eat n last characters
1.123 + *
1.124 + * @param count n
1.125 + */
1.126 + public void backspace(int count) {
1.127 + for (int i = 0; i < count; i++) {
1.128 + backspace();
1.129 + }
1.130 + }
1.131 +
1.132 + /**
1.133 + * With 100 ms delay and all colors.
1.134 + *
1.135 + * @see #printRainbow(java.lang.String, int,
1.136 + * info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor[])
1.137 + */
1.138 + public void printRainbow(String string) {
1.139 + printRainbow(string, 100);
1.140 + }
1.141 +
1.142 + /**
1.143 + * With all colors.
1.144 + *
1.145 + * @see #printRainbow(java.lang.String, int,
1.146 + * info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor[])
1.147 + */
1.148 + public void printRainbow(String string, int delay) {
1.149 + printRainbow(string, delay, TerminalColor.values());
1.150 + }
1.151 +
1.152 + /**
1.153 + * Prints rainbow text – (re)writes same text subsequently in given colors and then in default
1.154 + * color.
1.155 + *
1.156 + * @param string text to be printed, should not contain \n new line (then rainbow does not work
1.157 + * – use println() after printRainbow() instead)
1.158 + * @param delay delay between rewrites
1.159 + * @param colors list of colors to be used
1.160 + */
1.161 + public void printRainbow(String string, int delay, TerminalColor... colors) {
1.162 + for (TerminalColor c : colors) {
1.163 + print(c, string);
1.164 + try {
1.165 + Thread.sleep(delay);
1.166 + } catch (InterruptedException e) {
1.167 + // no time to sleep
1.168 + break;
1.169 + }
1.170 + backspace(string.length());
1.171 + flush();
1.172 + }
1.173 + print(string);
1.174 + }
1.175 +
1.176 + public void setForegroundColor(TerminalColor color) {
1.177 + printCodes(color.getForegroundCode());
1.178 + }
1.179 +
1.180 + public void setBackgroundColor(TerminalColor color) {
1.181 + printCodes(color.getBackgroundCode());
1.182 + }
1.183 +
1.184 + public void print(TerminalColor foregroundColor, String string) {
1.185 + setForegroundColor(foregroundColor);
1.186 + print(string);
1.187 + resetAll();
1.188 + }
1.189 +
1.190 + public void println(TerminalColor foregroundColor, String string) {
1.191 + print(foregroundColor, string);
1.192 + println();
1.193 + }
1.194 +
1.195 + public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, String string) {
1.196 + setForegroundColor(foregroundColor);
1.197 + setBackgroundColor(backgroundColor);
1.198 + print(string);
1.199 + resetAll();
1.200 + }
1.201 +
1.202 + public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, String string) {
1.203 + print(foregroundColor, backgroundColor, string);
1.204 + println();
1.205 + }
1.206 +
1.207 + public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, EnumSet<TerminalStyle> styles, String string) {
1.208 + setForegroundColor(foregroundColor);
1.209 + setBackgroundColor(backgroundColor);
1.210 + setStyle(styles);
1.211 + print(string);
1.212 + resetAll();
1.213 + }
1.214 +
1.215 + public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, EnumSet<TerminalStyle> styles, String string) {
1.216 + print(foregroundColor, backgroundColor, styles, string);
1.217 + println();
1.218 + }
1.219 +
1.220 + public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, TerminalStyle style, String string) {
1.221 + print(foregroundColor, backgroundColor, EnumSet.of(style), string);
1.222 + }
1.223 +
1.224 + public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, TerminalStyle style, String string) {
1.225 + print(foregroundColor, backgroundColor, style, string);
1.226 + println();
1.227 + }
1.228 +
1.229 + public void print(TerminalColor foregroundColor, EnumSet<TerminalStyle> styles, String string) {
1.230 + setForegroundColor(foregroundColor);
1.231 + setStyle(styles);
1.232 + print(string);
1.233 + resetAll();
1.234 + }
1.235 +
1.236 + public void println(TerminalColor foregroundColor, EnumSet<TerminalStyle> styles, String string) {
1.237 + print(foregroundColor, styles, string);
1.238 + println();
1.239 + }
1.240 +
1.241 + public void print(TerminalColor foregroundColor, TerminalStyle style, String string) {
1.242 + print(foregroundColor, EnumSet.of(style), string);
1.243 + }
1.244 +
1.245 + public void println(TerminalColor foregroundColor, TerminalStyle style, String string) {
1.246 + print(foregroundColor, style, string);
1.247 + println();
1.248 + }
1.249 +
1.250 + public void print(EnumSet<TerminalStyle> styles, String string) {
1.251 + setStyle(styles);
1.252 + print(string);
1.253 + resetAll();
1.254 + }
1.255 +
1.256 + public void println(EnumSet<TerminalStyle> styles, String string) {
1.257 + print(styles, string);
1.258 + println();
1.259 + }
1.260 +
1.261 + public void print(TerminalStyle style, String string) {
1.262 + print(EnumSet.of(style), string);
1.263 + }
1.264 +
1.265 + public void println(TerminalStyle style, String string) {
1.266 + print(style, string);
1.267 + println();
1.268 + }
1.269 +
1.270 + public void resetAll() {
1.271 + printCodes(TerminalStyle.Reset.code);
1.272 + }
1.273 +
1.274 + private void printCodes(int... codes) {
1.275 + if (COLOR_ENABLED && colorful) {
1.276 + print("\033[");
1.277 + for (int i = 0; i < codes.length; i++) {
1.278 + print(codes[i]);
1.279 + if (i < codes.length - 1 && codes.length > 1) {
1.280 + print(";");
1.281 + }
1.282 + }
1.283 + print("m");
1.284 + }
1.285 + }
1.286 +
1.287 + /**
1.288 + * Colors can be switched on/off during usage of this writer.
1.289 + *
1.290 + * @return whether colors are currently turned on
1.291 + * @see #isColorEnabled()
1.292 + */
1.293 + public boolean isColorful() {
1.294 + return colorful;
1.295 + }
1.296 +
1.297 + /**
1.298 + * Collors might be definitively disabled in constructor. If not, they can be turned on/off
1.299 + * during usage of this writer by {@linkplain #setColorful(boolean)}
1.300 + *
1.301 + * @return whether colors are allowed for this instance of this class
1.302 + * @see #isColorful()
1.303 + */
1.304 + public boolean isColorEnabled() {
1.305 + return COLOR_ENABLED;
1.306 + }
1.307 +
1.308 + /**
1.309 + * @see #isColorful()
1.310 + * @see #isColorEnabled()
1.311 + */
1.312 + public void setColorful(boolean colorful) {
1.313 + this.colorful = colorful;
1.314 + }
1.315 +
1.316 + public ColorfulPrintWriter(File file) throws FileNotFoundException {
1.317 + super(file);
1.318 + COLOR_ENABLED = true;
1.319 + }
1.320 +
1.321 + public ColorfulPrintWriter(OutputStream out) {
1.322 + super(out);
1.323 + COLOR_ENABLED = true;
1.324 + }
1.325 +
1.326 + public ColorfulPrintWriter(String fileName) throws FileNotFoundException {
1.327 + super(fileName);
1.328 + COLOR_ENABLED = true;
1.329 + }
1.330 +
1.331 + public ColorfulPrintWriter(Writer out) {
1.332 + super(out);
1.333 + COLOR_ENABLED = true;
1.334 + }
1.335 +
1.336 + public ColorfulPrintWriter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException {
1.337 + super(file, csn);
1.338 + COLOR_ENABLED = true;
1.339 + }
1.340 +
1.341 + /**
1.342 + * @param colorEnabled colors might be definitively disabled by this option – this might be more
1.343 + * optimalizable than dynamic turning off colors by {@linkplain #setColorful(boolean)} which is
1.344 + * not definitive (colors can be turned on during live of this instance). This might be useful
1.345 + * if you need an instance of this class but don't need colors at all.
1.346 + */
1.347 + public ColorfulPrintWriter(OutputStream out, boolean autoFlush, boolean colorEnabled) {
1.348 + super(out, autoFlush);
1.349 + COLOR_ENABLED = colorEnabled;
1.350 + }
1.351 +
1.352 + public ColorfulPrintWriter(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException {
1.353 + super(fileName, csn);
1.354 + COLOR_ENABLED = true;
1.355 + }
1.356 +
1.357 + public ColorfulPrintWriter(Writer out, boolean autoFlush) {
1.358 + super(out, autoFlush);
1.359 + COLOR_ENABLED = true;
1.360 + }
1.361 +}