java/sql-dk/src/info/globalcode/sql/dk/ColorfulPrintWriter.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 28 Dec 2013 20:36:29 +0100
branchv_0
changeset 92 1399ac70a5bd
parent 59 5f745ae795a8
child 126 2357a9d08660
permissions -rw-r--r--
support all types from java.sql.Types.Types
     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(TerminalStyle style) {
    83 		setStyle(EnumSet.of(style));
    84 	}
    85 
    86 	public void setStyle(EnumSet<TerminalStyle> styles) {
    87 		printCodes(getStyleCodes(styles));
    88 	}
    89 
    90 	private static int[] getStyleCodes(EnumSet<TerminalStyle> styles) {
    91 		int[] array = new int[styles.size()];
    92 		int i = 0;
    93 		for (TerminalStyle s : styles) {
    94 			array[i++] = s.getCode();
    95 		}
    96 		return array;
    97 	}
    98 
    99 	/**
   100 	 * Print (usually audible) bell code (\007, \a, ^G)
   101 	 */
   102 	public void bell() {
   103 		print("\007");
   104 	}
   105 
   106 	/**
   107 	 * Eat the last character
   108 	 */
   109 	public void backspace() {
   110 		print("\b");
   111 	}
   112 
   113 	/**
   114 	 * Eat n last characters
   115 	 *
   116 	 * @param count n
   117 	 */
   118 	public void backspace(int count) {
   119 		for (int i = 0; i < count; i++) {
   120 			backspace();
   121 		}
   122 	}
   123 
   124 	/**
   125 	 * With 100 ms delay and all colors.
   126 	 *
   127 	 * @see #printRainbow(java.lang.String, int,
   128 	 * info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor[])
   129 	 */
   130 	public void printRainbow(String string) {
   131 		printRainbow(string, 100);
   132 	}
   133 
   134 	/**
   135 	 * With all colors.
   136 	 *
   137 	 * @see #printRainbow(java.lang.String, int,
   138 	 * info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor[])
   139 	 */
   140 	public void printRainbow(String string, int delay) {
   141 		printRainbow(string, delay, TerminalColor.values());
   142 	}
   143 
   144 	/**
   145 	 * Prints rainbow text – (re)writes same text subsequently in given colors and then in default
   146 	 * color.
   147 	 *
   148 	 * @param string text to be printed, should not contain \n new line (then rainbow does not work
   149 	 * – use println() after printRainbow() instead)
   150 	 * @param delay delay between rewrites
   151 	 * @param colors list of colors to be used
   152 	 */
   153 	public void printRainbow(String string, int delay, TerminalColor... colors) {
   154 		for (TerminalColor c : colors) {
   155 			print(c, string);
   156 			try {
   157 				Thread.sleep(delay);
   158 			} catch (InterruptedException e) {
   159 				// no time to sleep
   160 				break;
   161 			}
   162 			backspace(string.length());
   163 			flush();
   164 		}
   165 		print(string);
   166 	}
   167 
   168 	public void setForegroundColor(TerminalColor color) {
   169 		printCodes(color.getForegroundCode());
   170 	}
   171 
   172 	public void setBackgroundColor(TerminalColor color) {
   173 		printCodes(color.getBackgroundCode());
   174 	}
   175 
   176 	public void print(TerminalColor foregroundColor, String string) {
   177 		setForegroundColor(foregroundColor);
   178 		print(string);
   179 		resetAll();
   180 	}
   181 
   182 	public void println(TerminalColor foregroundColor, String string) {
   183 		print(foregroundColor, string);
   184 		println();
   185 	}
   186 
   187 	public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, String string) {
   188 		setForegroundColor(foregroundColor);
   189 		setBackgroundColor(backgroundColor);
   190 		print(string);
   191 		resetAll();
   192 	}
   193 
   194 	public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, String string) {
   195 		print(foregroundColor, backgroundColor, string);
   196 		println();
   197 	}
   198 
   199 	public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, EnumSet<TerminalStyle> styles, String string) {
   200 		setForegroundColor(foregroundColor);
   201 		setBackgroundColor(backgroundColor);
   202 		setStyle(styles);
   203 		print(string);
   204 		resetAll();
   205 	}
   206 
   207 	public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, EnumSet<TerminalStyle> styles, String string) {
   208 		print(foregroundColor, backgroundColor, styles, string);
   209 		println();
   210 	}
   211 
   212 	public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, TerminalStyle style, String string) {
   213 		print(foregroundColor, backgroundColor, EnumSet.of(style), string);
   214 	}
   215 
   216 	public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, TerminalStyle style, String string) {
   217 		print(foregroundColor, backgroundColor, style, string);
   218 		println();
   219 	}
   220 
   221 	public void print(TerminalColor foregroundColor, EnumSet<TerminalStyle> styles, String string) {
   222 		setForegroundColor(foregroundColor);
   223 		setStyle(styles);
   224 		print(string);
   225 		resetAll();
   226 	}
   227 
   228 	public void println(TerminalColor foregroundColor, EnumSet<TerminalStyle> styles, String string) {
   229 		print(foregroundColor, styles, string);
   230 		println();
   231 	}
   232 
   233 	public void print(TerminalColor foregroundColor, TerminalStyle style, String string) {
   234 		print(foregroundColor, EnumSet.of(style), string);
   235 	}
   236 
   237 	public void println(TerminalColor foregroundColor, TerminalStyle style, String string) {
   238 		print(foregroundColor, style, string);
   239 		println();
   240 	}
   241 
   242 	public void print(EnumSet<TerminalStyle> styles, String string) {
   243 		setStyle(styles);
   244 		print(string);
   245 		resetAll();
   246 	}
   247 
   248 	public void println(EnumSet<TerminalStyle> styles, String string) {
   249 		print(styles, string);
   250 		println();
   251 	}
   252 
   253 	public void print(TerminalStyle style, String string) {
   254 		print(EnumSet.of(style), string);
   255 	}
   256 
   257 	public void println(TerminalStyle style, String string) {
   258 		print(style, string);
   259 		println();
   260 	}
   261 
   262 	public void resetAll() {
   263 		printCodes(TerminalStyle.Reset.code);
   264 	}
   265 
   266 	private void printCodes(int... codes) {
   267 		if (colorful) {
   268 			print("\033[");
   269 			for (int i = 0; i < codes.length; i++) {
   270 				print(codes[i]);
   271 				if (i < codes.length - 1 && codes.length > 1) {
   272 					print(";");
   273 				}
   274 			}
   275 			print("m");
   276 		}
   277 	}
   278 
   279 	public boolean isColorful() {
   280 		return colorful;
   281 	}
   282 
   283 	public void setColorful(boolean colorful) {
   284 		this.colorful = colorful;
   285 	}
   286 
   287 	public ColorfulPrintWriter(File file) throws FileNotFoundException {
   288 		super(file);
   289 	}
   290 
   291 	public ColorfulPrintWriter(OutputStream out) {
   292 		super(out);
   293 	}
   294 
   295 	public ColorfulPrintWriter(String fileName) throws FileNotFoundException {
   296 		super(fileName);
   297 	}
   298 
   299 	public ColorfulPrintWriter(Writer out) {
   300 		super(out);
   301 	}
   302 
   303 	public ColorfulPrintWriter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException {
   304 		super(file, csn);
   305 	}
   306 
   307 	public ColorfulPrintWriter(OutputStream out, boolean autoFlush) {
   308 		super(out, autoFlush);
   309 	}
   310 
   311 	public ColorfulPrintWriter(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException {
   312 		super(fileName, csn);
   313 	}
   314 
   315 	public ColorfulPrintWriter(Writer out, boolean autoFlush) {
   316 		super(out, autoFlush);
   317 	}
   318 }