franta-hg@31: /**
franta-hg@31: * SQL-DK
franta-hg@31: * Copyright © 2013 František Kučera (frantovo.cz)
franta-hg@31: *
franta-hg@31: * This program is free software: you can redistribute it and/or modify
franta-hg@31: * it under the terms of the GNU General Public License as published by
franta-hg@31: * the Free Software Foundation, either version 3 of the License, or
franta-hg@31: * (at your option) any later version.
franta-hg@31: *
franta-hg@31: * This program is distributed in the hope that it will be useful,
franta-hg@31: * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@31: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@31: * GNU General Public License for more details.
franta-hg@31: *
franta-hg@31: * You should have received a copy of the GNU General Public License
franta-hg@31: * along with this program. If not, see .
franta-hg@31: */
franta-hg@31: package info.globalcode.sql.dk;
franta-hg@31:
franta-hg@31: import java.io.File;
franta-hg@31: import java.io.FileNotFoundException;
franta-hg@31: import java.io.OutputStream;
franta-hg@31: import java.io.PrintWriter;
franta-hg@31: import java.io.UnsupportedEncodingException;
franta-hg@31: import java.io.Writer;
franta-hg@31: import java.util.EnumSet;
franta-hg@31:
franta-hg@31: /**
franta-hg@31: *
franta-hg@31: * @author Ing. František Kučera (frantovo.cz)
franta-hg@31: */
franta-hg@31: public class ColorfulPrintWriter extends PrintWriter {
franta-hg@31:
franta-hg@31: public enum TerminalColor {
franta-hg@31:
franta-hg@31: Black(30, 40),
franta-hg@31: Red(31, 41),
franta-hg@31: Green(32, 42),
franta-hg@31: Yellow(33, 43),
franta-hg@31: Blue(34, 44),
franta-hg@31: Magenta(35, 45),
franta-hg@31: Cyan(36, 46),
franta-hg@31: White(37, 47);
franta-hg@31: private final int foregroundCode;
franta-hg@31: private final int backgroundCode;
franta-hg@31:
franta-hg@31: private TerminalColor(int foregroundCode, int backgroundCode) {
franta-hg@31: this.foregroundCode = foregroundCode;
franta-hg@31: this.backgroundCode = backgroundCode;
franta-hg@31: }
franta-hg@31:
franta-hg@31: public int getForegroundCode() {
franta-hg@31: return foregroundCode;
franta-hg@31: }
franta-hg@31:
franta-hg@31: public int getBackgroundCode() {
franta-hg@31: return backgroundCode;
franta-hg@31: }
franta-hg@31: }
franta-hg@31:
franta-hg@31: public enum TerminalStyle {
franta-hg@31:
franta-hg@31: Reset(0),
franta-hg@31: Bright(1),
franta-hg@31: Dim(2),
franta-hg@31: Underscore(4),
franta-hg@31: Blink(5),
franta-hg@31: Reverse(7),
franta-hg@31: Hidden(8);
franta-hg@31: private int code;
franta-hg@31:
franta-hg@31: private TerminalStyle(int code) {
franta-hg@31: this.code = code;
franta-hg@31: }
franta-hg@31:
franta-hg@31: public int getCode() {
franta-hg@31: return code;
franta-hg@31: }
franta-hg@31: }
franta-hg@31: private boolean colorful = true;
franta-hg@31:
franta-hg@31: public void setStyle(EnumSet styles) {
franta-hg@31: printCodes(getStyleCodes(styles));
franta-hg@31: }
franta-hg@31:
franta-hg@31: private static int[] getStyleCodes(EnumSet styles) {
franta-hg@31: int[] array = new int[styles.size()];
franta-hg@31: int i = 0;
franta-hg@31: for (TerminalStyle s : styles) {
franta-hg@31: array[i++] = s.getCode();
franta-hg@31: }
franta-hg@31: return array;
franta-hg@31: }
franta-hg@31:
franta-hg@31: public void setForegroundColor(TerminalColor color) {
franta-hg@31: printCodes(color.getForegroundCode());
franta-hg@31: }
franta-hg@31:
franta-hg@31: public void setBackgroundColor(TerminalColor color) {
franta-hg@31: printCodes(color.getBackgroundCode());
franta-hg@31: }
franta-hg@31:
franta-hg@31: public void print(TerminalColor foregroundColor, String string) {
franta-hg@31: setForegroundColor(foregroundColor);
franta-hg@31: print(string);
franta-hg@31: resetAll();
franta-hg@31: }
franta-hg@31:
franta-hg@31: public void println(TerminalColor foregroundColor, String string) {
franta-hg@31: print(foregroundColor, string);
franta-hg@31: println();
franta-hg@31: }
franta-hg@31:
franta-hg@31: public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, String string) {
franta-hg@31: setForegroundColor(foregroundColor);
franta-hg@31: setBackgroundColor(backgroundColor);
franta-hg@31: print(string);
franta-hg@31: resetAll();
franta-hg@31: }
franta-hg@31:
franta-hg@31: public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, String string) {
franta-hg@31: print(foregroundColor, backgroundColor, string);
franta-hg@31: println();
franta-hg@31: }
franta-hg@31:
franta-hg@31: public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, EnumSet styles, String string) {
franta-hg@31: setForegroundColor(foregroundColor);
franta-hg@31: setBackgroundColor(backgroundColor);
franta-hg@31: setStyle(styles);
franta-hg@31: print(string);
franta-hg@31: resetAll();
franta-hg@31: }
franta-hg@31:
franta-hg@31: public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, EnumSet styles, String string) {
franta-hg@31: print(foregroundColor, backgroundColor, styles, string);
franta-hg@31: println();
franta-hg@31: }
franta-hg@31:
franta-hg@31: public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, TerminalStyle style, String string) {
franta-hg@31: print(foregroundColor, backgroundColor, EnumSet.of(style), string);
franta-hg@31: }
franta-hg@31:
franta-hg@31: public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, TerminalStyle style, String string) {
franta-hg@31: print(foregroundColor, backgroundColor, style, string);
franta-hg@31: println();
franta-hg@31: }
franta-hg@31:
franta-hg@31: public void print(TerminalColor foregroundColor, EnumSet styles, String string) {
franta-hg@31: setForegroundColor(foregroundColor);
franta-hg@31: setStyle(styles);
franta-hg@31: print(string);
franta-hg@31: resetAll();
franta-hg@31: }
franta-hg@31:
franta-hg@31: public void println(TerminalColor foregroundColor, EnumSet styles, String string) {
franta-hg@31: print(foregroundColor, styles, string);
franta-hg@31: println();
franta-hg@31: }
franta-hg@31:
franta-hg@31: public void print(TerminalColor foregroundColor, TerminalStyle style, String string) {
franta-hg@31: print(foregroundColor, EnumSet.of(style), string);
franta-hg@31: }
franta-hg@31:
franta-hg@31: public void println(TerminalColor foregroundColor, TerminalStyle style, String string) {
franta-hg@31: print(foregroundColor, style, string);
franta-hg@31: println();
franta-hg@31: }
franta-hg@31:
franta-hg@31: public void resetAll() {
franta-hg@31: printCodes(0);
franta-hg@31: }
franta-hg@31:
franta-hg@31: private void printCodes(int... codes) {
franta-hg@31: if (colorful) {
franta-hg@31: print("\033[");
franta-hg@31: for (int i = 0; i < codes.length; i++) {
franta-hg@31: print(codes[i]);
franta-hg@31: if (i < codes.length - 1 && codes.length > 1) {
franta-hg@31: print(";");
franta-hg@31: }
franta-hg@31: }
franta-hg@31: print("m");
franta-hg@31: }
franta-hg@31: }
franta-hg@31:
franta-hg@31: public boolean isColorful() {
franta-hg@31: return colorful;
franta-hg@31: }
franta-hg@31:
franta-hg@31: public void setColorful(boolean colorful) {
franta-hg@31: this.colorful = colorful;
franta-hg@31: }
franta-hg@31:
franta-hg@31: public ColorfulPrintWriter(File file) throws FileNotFoundException {
franta-hg@31: super(file);
franta-hg@31: }
franta-hg@31:
franta-hg@31: public ColorfulPrintWriter(OutputStream out) {
franta-hg@31: super(out);
franta-hg@31: }
franta-hg@31:
franta-hg@31: public ColorfulPrintWriter(String fileName) throws FileNotFoundException {
franta-hg@31: super(fileName);
franta-hg@31: }
franta-hg@31:
franta-hg@31: public ColorfulPrintWriter(Writer out) {
franta-hg@31: super(out);
franta-hg@31: }
franta-hg@31:
franta-hg@31: public ColorfulPrintWriter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException {
franta-hg@31: super(file, csn);
franta-hg@31: }
franta-hg@31:
franta-hg@31: public ColorfulPrintWriter(OutputStream out, boolean autoFlush) {
franta-hg@31: super(out, autoFlush);
franta-hg@31: }
franta-hg@31:
franta-hg@31: public ColorfulPrintWriter(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException {
franta-hg@31: super(fileName, csn);
franta-hg@31: }
franta-hg@31:
franta-hg@31: public ColorfulPrintWriter(Writer out, boolean autoFlush) {
franta-hg@31: super(out, autoFlush);
franta-hg@31: }
franta-hg@31: }