java/sql-dk/src/info/globalcode/sql/dk/ColorfulPrintWriter.java
branchv_0
changeset 126 2357a9d08660
parent 59 5f745ae795a8
child 155 eb3676c6929b
equal deleted inserted replaced
125:46eb1925f2bb 126:2357a9d08660
    75 
    75 
    76 		public int getCode() {
    76 		public int getCode() {
    77 			return code;
    77 			return code;
    78 		}
    78 		}
    79 	}
    79 	}
       
    80 	private final boolean COLOR_ENABLED;
    80 	private boolean colorful = true;
    81 	private boolean colorful = true;
    81 
    82 
    82 	public void setStyle(TerminalStyle style) {
    83 	public void setStyle(TerminalStyle style) {
    83 		setStyle(EnumSet.of(style));
    84 		setStyle(EnumSet.of(style));
    84 	}
    85 	}
   262 	public void resetAll() {
   263 	public void resetAll() {
   263 		printCodes(TerminalStyle.Reset.code);
   264 		printCodes(TerminalStyle.Reset.code);
   264 	}
   265 	}
   265 
   266 
   266 	private void printCodes(int... codes) {
   267 	private void printCodes(int... codes) {
   267 		if (colorful) {
   268 		if (COLOR_ENABLED && colorful) {
   268 			print("\033[");
   269 			print("\033[");
   269 			for (int i = 0; i < codes.length; i++) {
   270 			for (int i = 0; i < codes.length; i++) {
   270 				print(codes[i]);
   271 				print(codes[i]);
   271 				if (i < codes.length - 1 && codes.length > 1) {
   272 				if (i < codes.length - 1 && codes.length > 1) {
   272 					print(";");
   273 					print(";");
   274 			}
   275 			}
   275 			print("m");
   276 			print("m");
   276 		}
   277 		}
   277 	}
   278 	}
   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 	 */
   279 	public boolean isColorful() {
   286 	public boolean isColorful() {
   280 		return colorful;
   287 		return colorful;
   281 	}
   288 	}
   282 
   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 	 */
   283 	public void setColorful(boolean colorful) {
   305 	public void setColorful(boolean colorful) {
   284 		this.colorful = colorful;
   306 		this.colorful = colorful;
   285 	}
   307 	}
   286 
   308 
   287 	public ColorfulPrintWriter(File file) throws FileNotFoundException {
   309 	public ColorfulPrintWriter(File file) throws FileNotFoundException {
   288 		super(file);
   310 		super(file);
       
   311 		COLOR_ENABLED = true;
   289 	}
   312 	}
   290 
   313 
   291 	public ColorfulPrintWriter(OutputStream out) {
   314 	public ColorfulPrintWriter(OutputStream out) {
   292 		super(out);
   315 		super(out);
       
   316 		COLOR_ENABLED = true;
   293 	}
   317 	}
   294 
   318 
   295 	public ColorfulPrintWriter(String fileName) throws FileNotFoundException {
   319 	public ColorfulPrintWriter(String fileName) throws FileNotFoundException {
   296 		super(fileName);
   320 		super(fileName);
       
   321 		COLOR_ENABLED = true;
   297 	}
   322 	}
   298 
   323 
   299 	public ColorfulPrintWriter(Writer out) {
   324 	public ColorfulPrintWriter(Writer out) {
   300 		super(out);
   325 		super(out);
       
   326 		COLOR_ENABLED = true;
   301 	}
   327 	}
   302 
   328 
   303 	public ColorfulPrintWriter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException {
   329 	public ColorfulPrintWriter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException {
   304 		super(file, csn);
   330 		super(file, csn);
   305 	}
   331 		COLOR_ENABLED = true;
   306 
   332 	}
   307 	public ColorfulPrintWriter(OutputStream out, boolean autoFlush) {
   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) {
   308 		super(out, autoFlush);
   341 		super(out, autoFlush);
       
   342 		COLOR_ENABLED = colorEnabled;
   309 	}
   343 	}
   310 
   344 
   311 	public ColorfulPrintWriter(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException {
   345 	public ColorfulPrintWriter(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException {
   312 		super(fileName, csn);
   346 		super(fileName, csn);
       
   347 		COLOR_ENABLED = true;
   313 	}
   348 	}
   314 
   349 
   315 	public ColorfulPrintWriter(Writer out, boolean autoFlush) {
   350 	public ColorfulPrintWriter(Writer out, boolean autoFlush) {
   316 		super(out, autoFlush);
   351 		super(out, autoFlush);
       
   352 		COLOR_ENABLED = true;
   317 	}
   353 	}
   318 }
   354 }