java/sql-dk/src/info/globalcode/sql/dk/ColorfulPrintWriter.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 23 Dec 2013 16:14:03 +0100
branchv_0
changeset 40 a9db7fb3ce65
parent 37 9e6f8e5d5f98
child 55 f5ed7c4efacc
permissions -rw-r--r--
TabularFormatter: print colorful tables\!
     1 /**
     2  * SQL-DK
     3  * Copyright © 2013 František Kučera (frantovo.cz)
     4  *
     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.
     9  *
    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.
    14  *
    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/>.
    17  */
    18 package info.globalcode.sql.dk;
    19 
    20 import java.io.File;
    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;
    27 
    28 /**
    29  *
    30  * @author Ing. František Kučera (frantovo.cz)
    31  */
    32 public class ColorfulPrintWriter extends PrintWriter {
    33 
    34 	public enum TerminalColor {
    35 
    36 		Black(30, 40),
    37 		Red(31, 41),
    38 		Green(32, 42),
    39 		Yellow(33, 43),
    40 		Blue(34, 44),
    41 		Magenta(35, 45),
    42 		Cyan(36, 46),
    43 		White(37, 47);
    44 		private final int foregroundCode;
    45 		private final int backgroundCode;
    46 
    47 		private TerminalColor(int foregroundCode, int backgroundCode) {
    48 			this.foregroundCode = foregroundCode;
    49 			this.backgroundCode = backgroundCode;
    50 		}
    51 
    52 		public int getForegroundCode() {
    53 			return foregroundCode;
    54 		}
    55 
    56 		public int getBackgroundCode() {
    57 			return backgroundCode;
    58 		}
    59 	}
    60 
    61 	public enum TerminalStyle {
    62 
    63 		Reset(0),
    64 		Bright(1),
    65 		Dim(2),
    66 		Underscore(4),
    67 		Blink(5),
    68 		Reverse(7),
    69 		Hidden(8);
    70 		private int code;
    71 
    72 		private TerminalStyle(int code) {
    73 			this.code = code;
    74 		}
    75 
    76 		public int getCode() {
    77 			return code;
    78 		}
    79 	}
    80 	private boolean colorful = true;
    81 
    82 	public void setStyle(EnumSet<TerminalStyle> styles) {
    83 		printCodes(getStyleCodes(styles));
    84 	}
    85 
    86 	private static int[] getStyleCodes(EnumSet<TerminalStyle> styles) {
    87 		int[] array = new int[styles.size()];
    88 		int i = 0;
    89 		for (TerminalStyle s : styles) {
    90 			array[i++] = s.getCode();
    91 		}
    92 		return array;
    93 	}
    94 
    95 	/**
    96 	 * Print (usually audible) bell code (\007, \a, ^G)
    97 	 */
    98 	public void bell() {
    99 		print("\007");
   100 	}
   101 
   102 	/**
   103 	 * Eat the last character
   104 	 */
   105 	public void backspace() {
   106 		print("\b");
   107 	}
   108 
   109 	/**
   110 	 * Eat n last characters
   111 	 *
   112 	 * @param count n
   113 	 */
   114 	public void backspace(int count) {
   115 		for (int i = 0; i < count; i++) {
   116 			backspace();
   117 		}
   118 	}
   119 
   120 	/**
   121 	 * With 100 ms delay and all colors.
   122 	 *
   123 	 * @see #printRainbow(java.lang.String, int,
   124 	 * info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor[])
   125 	 */
   126 	public void printRainbow(String string) {
   127 		printRainbow(string, 100);
   128 	}
   129 
   130 	/**
   131 	 * With all colors.
   132 	 *
   133 	 * @see #printRainbow(java.lang.String, int,
   134 	 * info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor[])
   135 	 */
   136 	public void printRainbow(String string, int delay) {
   137 		printRainbow(string, delay, TerminalColor.values());
   138 	}
   139 
   140 	/**
   141 	 * Prints rainbow text – (re)writes same text subsequently in given colors and then in default
   142 	 * color.
   143 	 *
   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
   148 	 */
   149 	public void printRainbow(String string, int delay, TerminalColor... colors) {
   150 		for (TerminalColor c : colors) {
   151 			print(c, string);
   152 			try {
   153 				Thread.sleep(delay);
   154 			} catch (InterruptedException e) {
   155 				// no time to sleep
   156 				break;
   157 			}
   158 			backspace(string.length());
   159 			flush();
   160 		}
   161 		print(string);
   162 	}
   163 
   164 	public void setForegroundColor(TerminalColor color) {
   165 		printCodes(color.getForegroundCode());
   166 	}
   167 
   168 	public void setBackgroundColor(TerminalColor color) {
   169 		printCodes(color.getBackgroundCode());
   170 	}
   171 
   172 	public void print(TerminalColor foregroundColor, String string) {
   173 		setForegroundColor(foregroundColor);
   174 		print(string);
   175 		resetAll();
   176 	}
   177 
   178 	public void println(TerminalColor foregroundColor, String string) {
   179 		print(foregroundColor, string);
   180 		println();
   181 	}
   182 
   183 	public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, String string) {
   184 		setForegroundColor(foregroundColor);
   185 		setBackgroundColor(backgroundColor);
   186 		print(string);
   187 		resetAll();
   188 	}
   189 
   190 	public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, String string) {
   191 		print(foregroundColor, backgroundColor, string);
   192 		println();
   193 	}
   194 
   195 	public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, EnumSet<TerminalStyle> styles, String string) {
   196 		setForegroundColor(foregroundColor);
   197 		setBackgroundColor(backgroundColor);
   198 		setStyle(styles);
   199 		print(string);
   200 		resetAll();
   201 	}
   202 
   203 	public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, EnumSet<TerminalStyle> styles, String string) {
   204 		print(foregroundColor, backgroundColor, styles, string);
   205 		println();
   206 	}
   207 
   208 	public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, TerminalStyle style, String string) {
   209 		print(foregroundColor, backgroundColor, EnumSet.of(style), string);
   210 	}
   211 
   212 	public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, TerminalStyle style, String string) {
   213 		print(foregroundColor, backgroundColor, style, string);
   214 		println();
   215 	}
   216 
   217 	public void print(TerminalColor foregroundColor, EnumSet<TerminalStyle> styles, String string) {
   218 		setForegroundColor(foregroundColor);
   219 		setStyle(styles);
   220 		print(string);
   221 		resetAll();
   222 	}
   223 
   224 	public void println(TerminalColor foregroundColor, EnumSet<TerminalStyle> styles, String string) {
   225 		print(foregroundColor, styles, string);
   226 		println();
   227 	}
   228 
   229 	public void print(TerminalColor foregroundColor, TerminalStyle style, String string) {
   230 		print(foregroundColor, EnumSet.of(style), string);
   231 	}
   232 
   233 	public void println(TerminalColor foregroundColor, TerminalStyle style, String string) {
   234 		print(foregroundColor, style, string);
   235 		println();
   236 	}
   237 
   238 	public void print(EnumSet<TerminalStyle> styles, String string) {
   239 		setStyle(styles);
   240 		print(string);
   241 		resetAll();
   242 	}
   243 
   244 	public void println(EnumSet<TerminalStyle> styles, String string) {
   245 		print(styles, string);
   246 		println();
   247 	}
   248 
   249 	public void print(TerminalStyle style, String string) {
   250 		print(EnumSet.of(style), string);
   251 	}
   252 
   253 	public void println(TerminalStyle style, String string) {
   254 		print(style, string);
   255 		println();
   256 	}
   257 
   258 	public void resetAll() {
   259 		printCodes(0);
   260 	}
   261 
   262 	private void printCodes(int... codes) {
   263 		if (colorful) {
   264 			print("\033[");
   265 			for (int i = 0; i < codes.length; i++) {
   266 				print(codes[i]);
   267 				if (i < codes.length - 1 && codes.length > 1) {
   268 					print(";");
   269 				}
   270 			}
   271 			print("m");
   272 		}
   273 	}
   274 
   275 	public boolean isColorful() {
   276 		return colorful;
   277 	}
   278 
   279 	public void setColorful(boolean colorful) {
   280 		this.colorful = colorful;
   281 	}
   282 
   283 	public ColorfulPrintWriter(File file) throws FileNotFoundException {
   284 		super(file);
   285 	}
   286 
   287 	public ColorfulPrintWriter(OutputStream out) {
   288 		super(out);
   289 	}
   290 
   291 	public ColorfulPrintWriter(String fileName) throws FileNotFoundException {
   292 		super(fileName);
   293 	}
   294 
   295 	public ColorfulPrintWriter(Writer out) {
   296 		super(out);
   297 	}
   298 
   299 	public ColorfulPrintWriter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException {
   300 		super(file, csn);
   301 	}
   302 
   303 	public ColorfulPrintWriter(OutputStream out, boolean autoFlush) {
   304 		super(out, autoFlush);
   305 	}
   306 
   307 	public ColorfulPrintWriter(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException {
   308 		super(fileName, csn);
   309 	}
   310 
   311 	public ColorfulPrintWriter(Writer out, boolean autoFlush) {
   312 		super(out, autoFlush);
   313 	}
   314 }