package org.swan.accessories;
public class ConsoleConstant {
// Reset
private static final String RESET = " 33[0m"; // Text Reset
private static final int COLOR_OFFSET = 16;
private static final int COLOR_MASK = (1 << 8) - 1;
private static final int FONT_STYLE_OFFSET = 4;
private static final int FONT_STYLE_MASK = (1 << 4) - 1;
private static final int STYLE_OFFSET = 0;
private static final int STYLE_MASK = (1 << 4) - 1;
public static final int SGR_FONT_STYLE_NORMAL = 0 << STYLE_OFFSET;
public static final int SGR_FONT_STYLE_BOLD = 1 << STYLE_OFFSET;
public static final int SGR_FONT_STYLE_FAINT = 2 << STYLE_OFFSET;
public static final int SGR_FONT_STYLE_ITALIC = 3 << STYLE_OFFSET;
public static final int SGR_FONT_STYLE_UNDER_LINE = 4 << STYLE_OFFSET;
public static final int SGR_FONT_STYLE_DOUBLE_UNDER_LINE = 21 << STYLE_OFFSET;
public static final int BACKGROUND = 1 << FONT_STYLE_OFFSET + 4;
public static final int FONT_COLOR_STYLE_NORMAL = 3 << FONT_STYLE_OFFSET;
public static final int FONT_COLOR_STYLE_BRIGHT = 9 << FONT_STYLE_OFFSET;
// Regular Colors
public static final int BLACK = 0 << COLOR_OFFSET; // BLACK
public static final int RED = 1 << COLOR_OFFSET; // RED
public static final int GREEN = 2 << COLOR_OFFSET; // GREEN
public static final int YELLOW = 3 << COLOR_OFFSET; // YELLOW
public static final int BLUE = 4 << COLOR_OFFSET; // BLUE
public static final int PURPLE = 5 << COLOR_OFFSET; // PURPLE
public static final int CYAN = 6 << COLOR_OFFSET; // CYAN
public static final int WHITE = 7 << COLOR_OFFSET; // WHITE
public static final ConsoleConstant STYLE_BLACK = newInstance(BLACK);
public static final ConsoleConstant STYLE_WHITE = newInstance(WHITE);
public static final ConsoleConstant STYLE_RED = newInstance(RED);
public static final ConsoleConstant STYLE_RESET = newInstance(0);
private final int style;
public static ConsoleConstant newInstance(int style) {
return new ConsoleConstant(style);
}
public ConsoleConstant setColor(int color) {
return newInstance(appendAttribute(color, COLOR_OFFSET, COLOR_MASK));
}
public ConsoleConstant setStyle(int style) {
return newInstance(appendAttribute(style, STYLE_OFFSET, STYLE_MASK));
}
public ConsoleConstant setBackground(boolean isBackground) {
return newInstance(isBackground ? (this.style | BACKGROUND) : (this.style & ~BACKGROUND));
}
public ConsoleConstant setColorStyle(int color) {
return newInstance(appendAttribute(color, FONT_STYLE_OFFSET, FONT_STYLE_MASK));
}
private int appendAttribute(int color, int offset, int mask) {
int newColor = color & (mask << offset);
return (this.style & ~(mask << offset)) | newColor;
}
private ConsoleConstant(int style) {
this.style = style;
}
@Override
public String toString() {
return getEscString(this.style);
}
private static String getEscString(int style) {
if (style == 0) return RESET;
StringBuilder sb = new StringBuilder();
sb.append(" 33[");
sb.append(getSGR(style)).append(getColor(style));
sb.append("m");
return sb.toString();
}
private static String getSGR(int style) {
if ((style & BACKGROUND) != 0)
return "";
return (style & 0xFF) + ";";
}
private static String getColor(final int style) {
int color = style >> COLOR_OFFSET & COLOR_MASK;
int colorStyle = (style >> FONT_STYLE_OFFSET) & FONT_STYLE_MASK;
colorStyle = colorStyle == 0 ? FONT_COLOR_STYLE_NORMAL >> FONT_STYLE_OFFSET : colorStyle;
if ((style & BACKGROUND) != 0) colorStyle++;
return "" + colorStyle + color;
}
}