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