java/sql-dk/src/info/globalcode/sql/dk/ColorfulPrintWriter.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 22 Dec 2013 20:51:32 +0100
branchv_0
changeset 31 ef2fdb55e8ec
child 37 9e6f8e5d5f98
permissions -rw-r--r--
ColorfulPrintWriter: colorz, wow
     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 	public void setForegroundColor(TerminalColor color) {
    96 		printCodes(color.getForegroundCode());
    97 	}
    98 
    99 	public void setBackgroundColor(TerminalColor color) {
   100 		printCodes(color.getBackgroundCode());
   101 	}
   102 
   103 	public void print(TerminalColor foregroundColor, String string) {
   104 		setForegroundColor(foregroundColor);
   105 		print(string);
   106 		resetAll();
   107 	}
   108 
   109 	public void println(TerminalColor foregroundColor, String string) {
   110 		print(foregroundColor, string);
   111 		println();
   112 	}
   113 
   114 	public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, String string) {
   115 		setForegroundColor(foregroundColor);
   116 		setBackgroundColor(backgroundColor);
   117 		print(string);
   118 		resetAll();
   119 	}
   120 
   121 	public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, String string) {
   122 		print(foregroundColor, backgroundColor, string);
   123 		println();
   124 	}
   125 
   126 	public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, EnumSet<TerminalStyle> styles, String string) {
   127 		setForegroundColor(foregroundColor);
   128 		setBackgroundColor(backgroundColor);
   129 		setStyle(styles);
   130 		print(string);
   131 		resetAll();
   132 	}
   133 
   134 	public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, EnumSet<TerminalStyle> styles, String string) {
   135 		print(foregroundColor, backgroundColor, styles, string);
   136 		println();
   137 	}
   138 
   139 	public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, TerminalStyle style, String string) {
   140 		print(foregroundColor, backgroundColor, EnumSet.of(style), string);
   141 	}
   142 
   143 	public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, TerminalStyle style, String string) {
   144 		print(foregroundColor, backgroundColor, style, string);
   145 		println();
   146 	}
   147 
   148 	public void print(TerminalColor foregroundColor, EnumSet<TerminalStyle> styles, String string) {
   149 		setForegroundColor(foregroundColor);
   150 		setStyle(styles);
   151 		print(string);
   152 		resetAll();
   153 	}
   154 
   155 	public void println(TerminalColor foregroundColor, EnumSet<TerminalStyle> styles, String string) {
   156 		print(foregroundColor, styles, string);
   157 		println();
   158 	}
   159 
   160 	public void print(TerminalColor foregroundColor, TerminalStyle style, String string) {
   161 		print(foregroundColor, EnumSet.of(style), string);
   162 	}
   163 
   164 	public void println(TerminalColor foregroundColor, TerminalStyle style, String string) {
   165 		print(foregroundColor, style, string);
   166 		println();
   167 	}
   168 
   169 	public void resetAll() {
   170 		printCodes(0);
   171 	}
   172 
   173 	private void printCodes(int... codes) {
   174 		if (colorful) {
   175 			print("\033[");
   176 			for (int i = 0; i < codes.length; i++) {
   177 				print(codes[i]);
   178 				if (i < codes.length - 1 && codes.length > 1) {
   179 					print(";");
   180 				}
   181 			}
   182 			print("m");
   183 		}
   184 	}
   185 
   186 	public boolean isColorful() {
   187 		return colorful;
   188 	}
   189 
   190 	public void setColorful(boolean colorful) {
   191 		this.colorful = colorful;
   192 	}
   193 
   194 	public ColorfulPrintWriter(File file) throws FileNotFoundException {
   195 		super(file);
   196 	}
   197 
   198 	public ColorfulPrintWriter(OutputStream out) {
   199 		super(out);
   200 	}
   201 
   202 	public ColorfulPrintWriter(String fileName) throws FileNotFoundException {
   203 		super(fileName);
   204 	}
   205 
   206 	public ColorfulPrintWriter(Writer out) {
   207 		super(out);
   208 	}
   209 
   210 	public ColorfulPrintWriter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException {
   211 		super(file, csn);
   212 	}
   213 
   214 	public ColorfulPrintWriter(OutputStream out, boolean autoFlush) {
   215 		super(out, autoFlush);
   216 	}
   217 
   218 	public ColorfulPrintWriter(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException {
   219 		super(fileName, csn);
   220 	}
   221 
   222 	public ColorfulPrintWriter(Writer out, boolean autoFlush) {
   223 		super(out, autoFlush);
   224 	}
   225 }