java/copy-image-resizer/src/cz/frantovo/copyImageResizer/logging/ColorfulPrintWriter.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 17 Nov 2014 18:12:27 +0100
changeset 8 4819598eb623
permissions -rw-r--r--
custom console colorful logger
franta-hg@8
     1
/**
franta-hg@8
     2
 * copy-image-resizer
franta-hg@8
     3
 * Copyright © 2013 František Kučera (frantovo.cz)
franta-hg@8
     4
 *
franta-hg@8
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@8
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@8
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@8
     8
 * (at your option) any later version.
franta-hg@8
     9
 *
franta-hg@8
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@8
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@8
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@8
    13
 * GNU General Public License for more details.
franta-hg@8
    14
 *
franta-hg@8
    15
 * You should have received a copy of the GNU General Public License
franta-hg@8
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@8
    17
 */
franta-hg@8
    18
package cz.frantovo.copyImageResizer.logging;
franta-hg@8
    19
franta-hg@8
    20
import java.io.File;
franta-hg@8
    21
import java.io.FileNotFoundException;
franta-hg@8
    22
import java.io.OutputStream;
franta-hg@8
    23
import java.io.PrintWriter;
franta-hg@8
    24
import java.io.UnsupportedEncodingException;
franta-hg@8
    25
import java.io.Writer;
franta-hg@8
    26
import java.util.EnumSet;
franta-hg@8
    27
franta-hg@8
    28
/**
franta-hg@8
    29
 * PrintWriter with convenience methods for printing color and formatted text.
franta-hg@8
    30
 *
franta-hg@8
    31
 * Uses ANSI Escape Sequences.
franta-hg@8
    32
 * See: http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
franta-hg@8
    33
 *
franta-hg@8
    34
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@8
    35
 */
franta-hg@8
    36
public class ColorfulPrintWriter extends PrintWriter {
franta-hg@8
    37
franta-hg@8
    38
	public enum TerminalColor {
franta-hg@8
    39
franta-hg@8
    40
		Black(30, 40),
franta-hg@8
    41
		Red(31, 41),
franta-hg@8
    42
		Green(32, 42),
franta-hg@8
    43
		Yellow(33, 43),
franta-hg@8
    44
		Blue(34, 44),
franta-hg@8
    45
		Magenta(35, 45),
franta-hg@8
    46
		Cyan(36, 46),
franta-hg@8
    47
		White(37, 47);
franta-hg@8
    48
		private final int foregroundCode;
franta-hg@8
    49
		private final int backgroundCode;
franta-hg@8
    50
franta-hg@8
    51
		private TerminalColor(int foregroundCode, int backgroundCode) {
franta-hg@8
    52
			this.foregroundCode = foregroundCode;
franta-hg@8
    53
			this.backgroundCode = backgroundCode;
franta-hg@8
    54
		}
franta-hg@8
    55
franta-hg@8
    56
		public int getForegroundCode() {
franta-hg@8
    57
			return foregroundCode;
franta-hg@8
    58
		}
franta-hg@8
    59
franta-hg@8
    60
		public int getBackgroundCode() {
franta-hg@8
    61
			return backgroundCode;
franta-hg@8
    62
		}
franta-hg@8
    63
	}
franta-hg@8
    64
franta-hg@8
    65
	public enum TerminalStyle {
franta-hg@8
    66
franta-hg@8
    67
		Reset(0),
franta-hg@8
    68
		Bright(1),
franta-hg@8
    69
		Dim(2),
franta-hg@8
    70
		Underscore(4),
franta-hg@8
    71
		Blink(5),
franta-hg@8
    72
		Reverse(7),
franta-hg@8
    73
		Hidden(8);
franta-hg@8
    74
		private int code;
franta-hg@8
    75
franta-hg@8
    76
		private TerminalStyle(int code) {
franta-hg@8
    77
			this.code = code;
franta-hg@8
    78
		}
franta-hg@8
    79
franta-hg@8
    80
		public int getCode() {
franta-hg@8
    81
			return code;
franta-hg@8
    82
		}
franta-hg@8
    83
	}
franta-hg@8
    84
	private final boolean COLOR_ENABLED;
franta-hg@8
    85
	private boolean colorful = true;
franta-hg@8
    86
franta-hg@8
    87
	public void setStyle(TerminalStyle style) {
franta-hg@8
    88
		setStyle(EnumSet.of(style));
franta-hg@8
    89
	}
franta-hg@8
    90
franta-hg@8
    91
	public void setStyle(EnumSet<TerminalStyle> styles) {
franta-hg@8
    92
		printCodes(getStyleCodes(styles));
franta-hg@8
    93
	}
franta-hg@8
    94
franta-hg@8
    95
	private static int[] getStyleCodes(EnumSet<TerminalStyle> styles) {
franta-hg@8
    96
		int[] array = new int[styles.size()];
franta-hg@8
    97
		int i = 0;
franta-hg@8
    98
		for (TerminalStyle s : styles) {
franta-hg@8
    99
			array[i++] = s.getCode();
franta-hg@8
   100
		}
franta-hg@8
   101
		return array;
franta-hg@8
   102
	}
franta-hg@8
   103
franta-hg@8
   104
	/**
franta-hg@8
   105
	 * Print (usually audible) bell code (\007, \a, ^G)
franta-hg@8
   106
	 */
franta-hg@8
   107
	public void bell() {
franta-hg@8
   108
		print("\007");
franta-hg@8
   109
	}
franta-hg@8
   110
franta-hg@8
   111
	/**
franta-hg@8
   112
	 * Eat the last character
franta-hg@8
   113
	 */
franta-hg@8
   114
	public void backspace() {
franta-hg@8
   115
		print("\b");
franta-hg@8
   116
	}
franta-hg@8
   117
franta-hg@8
   118
	/**
franta-hg@8
   119
	 * Eat n last characters
franta-hg@8
   120
	 *
franta-hg@8
   121
	 * @param count n
franta-hg@8
   122
	 */
franta-hg@8
   123
	public void backspace(int count) {
franta-hg@8
   124
		for (int i = 0; i < count; i++) {
franta-hg@8
   125
			backspace();
franta-hg@8
   126
		}
franta-hg@8
   127
	}
franta-hg@8
   128
franta-hg@8
   129
	/**
franta-hg@8
   130
	 * With 100 ms delay and all colors.
franta-hg@8
   131
	 *
franta-hg@8
   132
	 * @see #printRainbow(java.lang.String, int,
franta-hg@8
   133
	 * info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor[])
franta-hg@8
   134
	 */
franta-hg@8
   135
	public void printRainbow(String string) {
franta-hg@8
   136
		printRainbow(string, 100);
franta-hg@8
   137
	}
franta-hg@8
   138
franta-hg@8
   139
	/**
franta-hg@8
   140
	 * With all colors.
franta-hg@8
   141
	 *
franta-hg@8
   142
	 * @see #printRainbow(java.lang.String, int,
franta-hg@8
   143
	 * info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor[])
franta-hg@8
   144
	 */
franta-hg@8
   145
	public void printRainbow(String string, int delay) {
franta-hg@8
   146
		printRainbow(string, delay, TerminalColor.values());
franta-hg@8
   147
	}
franta-hg@8
   148
franta-hg@8
   149
	/**
franta-hg@8
   150
	 * Prints rainbow text – (re)writes same text subsequently in given colors and then in default
franta-hg@8
   151
	 * color.
franta-hg@8
   152
	 *
franta-hg@8
   153
	 * @param string text to be printed, should not contain \n new line (then rainbow does not work
franta-hg@8
   154
	 * – use println() after printRainbow() instead)
franta-hg@8
   155
	 * @param delay delay between rewrites
franta-hg@8
   156
	 * @param colors list of colors to be used
franta-hg@8
   157
	 */
franta-hg@8
   158
	public void printRainbow(String string, int delay, TerminalColor... colors) {
franta-hg@8
   159
		for (TerminalColor c : colors) {
franta-hg@8
   160
			print(c, string);
franta-hg@8
   161
			try {
franta-hg@8
   162
				Thread.sleep(delay);
franta-hg@8
   163
			} catch (InterruptedException e) {
franta-hg@8
   164
				// no time to sleep
franta-hg@8
   165
				break;
franta-hg@8
   166
			}
franta-hg@8
   167
			backspace(string.length());
franta-hg@8
   168
			flush();
franta-hg@8
   169
		}
franta-hg@8
   170
		print(string);
franta-hg@8
   171
	}
franta-hg@8
   172
franta-hg@8
   173
	public void setForegroundColor(TerminalColor color) {
franta-hg@8
   174
		printCodes(color.getForegroundCode());
franta-hg@8
   175
	}
franta-hg@8
   176
franta-hg@8
   177
	public void setBackgroundColor(TerminalColor color) {
franta-hg@8
   178
		printCodes(color.getBackgroundCode());
franta-hg@8
   179
	}
franta-hg@8
   180
franta-hg@8
   181
	public void print(TerminalColor foregroundColor, String string) {
franta-hg@8
   182
		setForegroundColor(foregroundColor);
franta-hg@8
   183
		print(string);
franta-hg@8
   184
		resetAll();
franta-hg@8
   185
	}
franta-hg@8
   186
franta-hg@8
   187
	public void println(TerminalColor foregroundColor, String string) {
franta-hg@8
   188
		print(foregroundColor, string);
franta-hg@8
   189
		println();
franta-hg@8
   190
	}
franta-hg@8
   191
franta-hg@8
   192
	public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, String string) {
franta-hg@8
   193
		setForegroundColor(foregroundColor);
franta-hg@8
   194
		setBackgroundColor(backgroundColor);
franta-hg@8
   195
		print(string);
franta-hg@8
   196
		resetAll();
franta-hg@8
   197
	}
franta-hg@8
   198
franta-hg@8
   199
	public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, String string) {
franta-hg@8
   200
		print(foregroundColor, backgroundColor, string);
franta-hg@8
   201
		println();
franta-hg@8
   202
	}
franta-hg@8
   203
franta-hg@8
   204
	public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, EnumSet<TerminalStyle> styles, String string) {
franta-hg@8
   205
		setForegroundColor(foregroundColor);
franta-hg@8
   206
		setBackgroundColor(backgroundColor);
franta-hg@8
   207
		setStyle(styles);
franta-hg@8
   208
		print(string);
franta-hg@8
   209
		resetAll();
franta-hg@8
   210
	}
franta-hg@8
   211
franta-hg@8
   212
	public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, EnumSet<TerminalStyle> styles, String string) {
franta-hg@8
   213
		print(foregroundColor, backgroundColor, styles, string);
franta-hg@8
   214
		println();
franta-hg@8
   215
	}
franta-hg@8
   216
franta-hg@8
   217
	public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, TerminalStyle style, String string) {
franta-hg@8
   218
		print(foregroundColor, backgroundColor, EnumSet.of(style), string);
franta-hg@8
   219
	}
franta-hg@8
   220
franta-hg@8
   221
	public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, TerminalStyle style, String string) {
franta-hg@8
   222
		print(foregroundColor, backgroundColor, style, string);
franta-hg@8
   223
		println();
franta-hg@8
   224
	}
franta-hg@8
   225
franta-hg@8
   226
	public void print(TerminalColor foregroundColor, EnumSet<TerminalStyle> styles, String string) {
franta-hg@8
   227
		setForegroundColor(foregroundColor);
franta-hg@8
   228
		setStyle(styles);
franta-hg@8
   229
		print(string);
franta-hg@8
   230
		resetAll();
franta-hg@8
   231
	}
franta-hg@8
   232
franta-hg@8
   233
	public void println(TerminalColor foregroundColor, EnumSet<TerminalStyle> styles, String string) {
franta-hg@8
   234
		print(foregroundColor, styles, string);
franta-hg@8
   235
		println();
franta-hg@8
   236
	}
franta-hg@8
   237
franta-hg@8
   238
	public void print(TerminalColor foregroundColor, TerminalStyle style, String string) {
franta-hg@8
   239
		print(foregroundColor, EnumSet.of(style), string);
franta-hg@8
   240
	}
franta-hg@8
   241
franta-hg@8
   242
	public void println(TerminalColor foregroundColor, TerminalStyle style, String string) {
franta-hg@8
   243
		print(foregroundColor, style, string);
franta-hg@8
   244
		println();
franta-hg@8
   245
	}
franta-hg@8
   246
franta-hg@8
   247
	public void print(EnumSet<TerminalStyle> styles, String string) {
franta-hg@8
   248
		setStyle(styles);
franta-hg@8
   249
		print(string);
franta-hg@8
   250
		resetAll();
franta-hg@8
   251
	}
franta-hg@8
   252
franta-hg@8
   253
	public void println(EnumSet<TerminalStyle> styles, String string) {
franta-hg@8
   254
		print(styles, string);
franta-hg@8
   255
		println();
franta-hg@8
   256
	}
franta-hg@8
   257
franta-hg@8
   258
	public void print(TerminalStyle style, String string) {
franta-hg@8
   259
		print(EnumSet.of(style), string);
franta-hg@8
   260
	}
franta-hg@8
   261
franta-hg@8
   262
	public void println(TerminalStyle style, String string) {
franta-hg@8
   263
		print(style, string);
franta-hg@8
   264
		println();
franta-hg@8
   265
	}
franta-hg@8
   266
franta-hg@8
   267
	public void resetAll() {
franta-hg@8
   268
		printCodes(TerminalStyle.Reset.code);
franta-hg@8
   269
	}
franta-hg@8
   270
franta-hg@8
   271
	private void printCodes(int... codes) {
franta-hg@8
   272
		if (COLOR_ENABLED && colorful) {
franta-hg@8
   273
			print("\033[");
franta-hg@8
   274
			for (int i = 0; i < codes.length; i++) {
franta-hg@8
   275
				print(codes[i]);
franta-hg@8
   276
				if (i < codes.length - 1 && codes.length > 1) {
franta-hg@8
   277
					print(";");
franta-hg@8
   278
				}
franta-hg@8
   279
			}
franta-hg@8
   280
			print("m");
franta-hg@8
   281
		}
franta-hg@8
   282
	}
franta-hg@8
   283
franta-hg@8
   284
	/**
franta-hg@8
   285
	 * Colors can be switched on/off during usage of this writer.
franta-hg@8
   286
	 *
franta-hg@8
   287
	 * @return whether colors are currently turned on
franta-hg@8
   288
	 * @see #isColorEnabled()
franta-hg@8
   289
	 */
franta-hg@8
   290
	public boolean isColorful() {
franta-hg@8
   291
		return colorful;
franta-hg@8
   292
	}
franta-hg@8
   293
franta-hg@8
   294
	/**
franta-hg@8
   295
	 * Collors might be definitively disabled in constructor. If not, they can be turned on/off
franta-hg@8
   296
	 * during usage of this writer by {@linkplain #setColorful(boolean)}
franta-hg@8
   297
	 *
franta-hg@8
   298
	 * @return whether colors are allowed for this instance of this class
franta-hg@8
   299
	 * @see #isColorful()
franta-hg@8
   300
	 */
franta-hg@8
   301
	public boolean isColorEnabled() {
franta-hg@8
   302
		return COLOR_ENABLED;
franta-hg@8
   303
	}
franta-hg@8
   304
franta-hg@8
   305
	/**
franta-hg@8
   306
	 * @see #isColorful()
franta-hg@8
   307
	 * @see #isColorEnabled()
franta-hg@8
   308
	 */
franta-hg@8
   309
	public void setColorful(boolean colorful) {
franta-hg@8
   310
		this.colorful = colorful;
franta-hg@8
   311
	}
franta-hg@8
   312
franta-hg@8
   313
	public ColorfulPrintWriter(File file) throws FileNotFoundException {
franta-hg@8
   314
		super(file);
franta-hg@8
   315
		COLOR_ENABLED = true;
franta-hg@8
   316
	}
franta-hg@8
   317
franta-hg@8
   318
	public ColorfulPrintWriter(OutputStream out) {
franta-hg@8
   319
		super(out);
franta-hg@8
   320
		COLOR_ENABLED = true;
franta-hg@8
   321
	}
franta-hg@8
   322
franta-hg@8
   323
	public ColorfulPrintWriter(String fileName) throws FileNotFoundException {
franta-hg@8
   324
		super(fileName);
franta-hg@8
   325
		COLOR_ENABLED = true;
franta-hg@8
   326
	}
franta-hg@8
   327
franta-hg@8
   328
	public ColorfulPrintWriter(Writer out) {
franta-hg@8
   329
		super(out);
franta-hg@8
   330
		COLOR_ENABLED = true;
franta-hg@8
   331
	}
franta-hg@8
   332
franta-hg@8
   333
	public ColorfulPrintWriter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException {
franta-hg@8
   334
		super(file, csn);
franta-hg@8
   335
		COLOR_ENABLED = true;
franta-hg@8
   336
	}
franta-hg@8
   337
franta-hg@8
   338
	/**
franta-hg@8
   339
	 * @param colorEnabled colors might be definitively disabled by this option – this might be more
franta-hg@8
   340
	 * optimalizable than dynamic turning off colors by {@linkplain #setColorful(boolean)} which is
franta-hg@8
   341
	 * not definitive (colors can be turned on during live of this instance). This might be useful
franta-hg@8
   342
	 * if you need an instance of this class but don't need colors at all.
franta-hg@8
   343
	 */
franta-hg@8
   344
	public ColorfulPrintWriter(OutputStream out, boolean autoFlush, boolean colorEnabled) {
franta-hg@8
   345
		super(out, autoFlush);
franta-hg@8
   346
		COLOR_ENABLED = colorEnabled;
franta-hg@8
   347
	}
franta-hg@8
   348
franta-hg@8
   349
	public ColorfulPrintWriter(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException {
franta-hg@8
   350
		super(fileName, csn);
franta-hg@8
   351
		COLOR_ENABLED = true;
franta-hg@8
   352
	}
franta-hg@8
   353
franta-hg@8
   354
	public ColorfulPrintWriter(Writer out, boolean autoFlush) {
franta-hg@8
   355
		super(out, autoFlush);
franta-hg@8
   356
		COLOR_ENABLED = true;
franta-hg@8
   357
	}
franta-hg@8
   358
}