Skip to content

Instantly share code, notes, and snippets.

@TheEmpty
Created November 29, 2013 00:28
Show Gist options
  • Save TheEmpty/7699928 to your computer and use it in GitHub Desktop.
Save TheEmpty/7699928 to your computer and use it in GitHub Desktop.
public class AnExample {
public static void main(String[] args) {
System.out.println(
ColoredString.s("Such ANSI, Easy Color. Many wow.").bold().cyan().blink();
);
}
}
package com.reliablerabbit.colors;
public class ColoredString {
private String s;
private int options;
public ColoredString(String s) { this.s = s; }
public ColoredString(ColoredString c) { this.s = c.getS(); }
public static ColoredString s(String s) { return new ColoredString(s); }
public String getS() { return s; }
// font modifiers
public ColoredString bold() { options = options | Colors.BOLD; return this; }
public ColoredString underline() { options = options | Colors.UNDERLINE; return this; }
public ColoredString blink() { options = options | Colors.BLINK; return this; }
// text colors
public ColoredString black() { options = options | Colors.BLACK; return this; }
public ColoredString red() { options = options | Colors.RED; return this; }
public ColoredString green() { options = options | Colors.GREEN; return this; }
public ColoredString yellow() { options = options | Colors.YELLOW; return this; }
public ColoredString blue() { options = options | Colors.BLUE; return this; }
public ColoredString magenta() { options = options | Colors.MAGENTA; return this; }
public ColoredString cyan() { options = options | Colors.CYAN; return this; }
public ColoredString white() { options = options | Colors.WHITE; return this; }
// render
@Override
public String toString() {
// if netbeans: return Colors.c( options & Colors.COLOR_MASK, s );
// if windows: return s;
// if NIX: return Colors.c(options, s);
return Colors.c(options, s);
}
public String rainbow() {
StringBuffer rainbow = new StringBuffer();
char[] characters = s.toCharArray();
short[] colors = { Colors.RED, Colors.YELLOW, Colors.GREEN, Colors.CYAN, Colors.BLUE };
int offset = (int) (System.nanoTime() % colors.length);
for(int i = 0; i < characters.length; i++) {
short color = colors[ ((i + offset) % colors.length) ];
rainbow.append( Colors.c(options | color, String.valueOf(characters[i])) );
}
return rainbow.toString();
}
}
package com.reliablerabbit.colors;
public class Colors {
public final static short BLINK = 4096;
public final static short BOLD = 1;
public final static short UNDERLINE = 4;
public final static short BLACK = 16;
public final static short RED = 32;
public final static short GREEN = 64;
public final static short YELLOW = 128;
public final static short BLUE = 256;
public final static short MAGENTA = 512;
public final static short CYAN = 1024;
public final static short WHITE = 2048;
public final static short COLOR_MASK = 0x3fb;
public final static String CLEAR_SCREEN = "\033[2J\033[;H";
public static String c(int options, String s) {
StringBuffer buff = new StringBuffer();
if((options & Colors.BOLD) == Colors.BOLD) buff.append("\u001B[1m");
if((options & Colors.UNDERLINE) == Colors.UNDERLINE) buff.append("\u001B[4m");
if((options & Colors.BLINK) == Colors.BLINK) buff.append("\u001B[5m");
if((options & Colors.BLACK) == Colors.BLACK) buff.append("\u001B[30m");
if((options & Colors.RED) == Colors.RED) buff.append("\u001B[31m");
if((options & Colors.GREEN) == Colors.GREEN) buff.append("\u001B[32m");
if((options & Colors.YELLOW) == Colors.YELLOW) buff.append("\u001B[33m");
if((options & Colors.BLUE) == Colors.BLUE) buff.append("\u001B[34m");
if((options & Colors.MAGENTA) == Colors.MAGENTA) buff.append("\u001B[35m");
if((options & Colors.CYAN) == Colors.CYAN) buff.append("\u001B[36m");
if((options & Colors.WHITE) == Colors.WHITE) buff.append("\u001B[37m");
buff.append(s);
buff.append("\u001B[0m");
return buff.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment