ANSI_ESC            = String.fromCharCode(0x1B);
ANSI_CSI            = ANSI_ESC + '[';
ANSI_TEXT_PROP      = 'm';
ANSI_RESET          = '0';
ANSI_BOLD           = '1';
ANSI_FAINT          = '2'; // unsupported?
ANSI_NORMAL         = '22';
ANSI_ITALIC         = '3'; // unsupported?
ANSI_UNDER          = '4';
ANSI_UNDER_DBL      = '21'; // unsupported?
ANSI_UNDER_OFF      = '24';
ANSI_BLINK          = '5';
ANSI_BLINK_FAST     = '6'; // unsupported?
ANSI_BLINK_OFF      = '25';
ANSI_REVERSE        = '7';
ANSI_POSITIVE       = '27';
ANSI_CONCEAL        = '8';
ANSI_REVEAL         = '28';
ANSI_FG             = '3';
ANSI_BG             = '4';
ANSI_FG_INTENSE     = '9';
ANSI_BG_INTENSE     = '10';
ANSI_BLACK          = '0';
ANSI_RED            = '1';
ANSI_GREEN          = '2';
ANSI_YELLOW         = '3';
ANSI_BLUE           = '4';
ANSI_MAGENTA        = '5';
ANSI_CYAN           = '6';
ANSI_WHITE          = '7';

ANSIControlCode = function(code, parameters)
{
    if (parameters == undefined)
        parameters = "";
    else if (typeof(parameters) == 'object' && (parameters instanceof Array))
        parameters = parameters.join(';');
    return ANSI_CSI + String(parameters) + String(code);
}

// simple text helpers:

ANSITextApplyProperties = function(string, properties)
{
    return ANSIControlCode(ANSI_TEXT_PROP, properties) + String(string) + ANSIControlCode(ANSI_TEXT_PROP);
}

var colorCodeMap = {
    "black"   : ANSI_BLACK,
    "red"     : ANSI_RED,
    "green"   : ANSI_GREEN,
    "yellow"  : ANSI_YELLOW,
    "blue"    : ANSI_BLUE,
    "magenta" : ANSI_MAGENTA,
    "cyan"    : ANSI_CYAN,
    "white"   : ANSI_WHITE
}

ANSITextColorize = function(string, color)
{
    if (colorCodeMap[color] == undefined)
        return string;
    return ANSITextApplyProperties(string, ANSI_FG + colorCodeMap[color]);
}
