Added ConsoleColor class for creating ANSI color escape codes.

This commit is contained in:
Joanthan Bernard 2014-11-21 15:18:09 -06:00
parent 8e1ed220fd
commit 3f1b89aeac
2 changed files with 68 additions and 3 deletions

View File

@ -1,6 +1,6 @@
#Fri, 21 Nov 2014 10:16:37 -0600 #Fri, 21 Nov 2014 15:09:35 -0600
name=jdb-util name=jdb-util
version=3.2 version=3.3
lib.local=true lib.local=true
build.number=0 build.number=1

View File

@ -0,0 +1,65 @@
/**
* # ConsoleColor
* @author Jonathan Bernard (jdbernard@gmail.com)
* @org jdbernard.com/util/ConsoleColor
* @copyright 2010-2014 Jonathan Bernard
*/
package com.jdbernard.util;
/**
* The ConsoleColor class is a wrapper around [ANSI escape codes].
*
* [ANSI escape codes]: http://en.wikipedia.org/wiki/ANSI_escape_code
*/
public class ConsoleColor {
// Storage for color information.
public final Colors fg;
public final Colors bg;
public final boolean bright;
public static enum Colors {
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE }
public ConsoleColor(String propString) {
String[] vals = propString.split("[,;: ]");
fg = Colors.valueOf(vals[0]);
if (vals.length == 2) {
bg = null;
bright = Boolean.parseBoolean(vals[1]); }
else if (vals.length == 3) {
bg = Colors.valueOf(vals[1]);
bright = Boolean.parseBoolean(vals[2]); }
else { bg = null; bright = false; } }
public ConsoleColor(Colors fgColor) { this(fgColor, Colors.BLACK, false); }
public ConsoleColor(Colors fgColor, boolean bright) {
this(fgColor, Colors.BLACK, bright); }
public ConsoleColor(Colors fgColor, Colors bgColor, boolean bright) {
this.fg = fgColor; this.bg = bgColor; this.bright = bright; }
public String toString() {
String result = "\u001b[";
boolean needSemi = false;
if (bright) {
result += "1";
needSemi = true; }
if (fg != null) {
if (needSemi) result += ";";
result += "3" + Integer.toString(fg.ordinal());
needSemi = true; }
if (bg != null) {
if (needSemi) result += ";";
result += "4" + Integer.toString(bg.ordinal()); }
return result + "m";
}
}