ColorD

bioinfornatics bioinfornatics at fedoraproject.org
Sat May 26 07:47:34 PDT 2012


mine code for ansi terminal
======================
enum Attributes : size_t{
    RESET,
    BOLD,
    NORMAL,
    UNDERLINE,
    BLINK,
    INVERSE,
    HIDE
}

size_t getBackgroundColor( string color ){
    size_t result;
    switch(color){
        case("black"):
            result = 30;
            break;
        case("red"):
            result = 31;
            break;
        case("green"):
            result = 32;
            break;
        case("yellow"):
            result = 33;
            break;
        case("blue"):
            result = 34;
            break;
        case("violet"):
            result = 35;
            break;
        case("cyan"):
            result = 36;
            break;
        case("white"):
            result = 37;
            break;
        case("default"):
            result = 0;
            break;
        default:
            throw new Exception( "Unknow color: %d".format(color) );
    }
    return result;
}

size_t getUndergroundColor( string color ){
    size_t result;
    switch(color){
        case("black"):
            result = 40;
            break;
        case("red"):
            result = 41;
            break;
        case("green"):
            result = 42;
            break;
        case("yellow"):
            result = 43;
            break;
        case("blue"):
            result = 44;
            break;
        case("violet"):
            result = 45;
            break;
        case("cyan"):
            result = 46;
            break;
        case("white"):
            result = 47;
            break;
        case("default"):
            result = 0;
            break;
        default:
            throw new Exception( "Unknow color: %d".format(color) );
    }
    return result;
}

string colorString( string backgroundColor, string undergroundColor,
string content, Attributes[] attributes...){
    size_t bg       = getBackgroundColor( backgroundColor );
    size_t fg       = getUndergroundColor( undergroundColor );
    string result  = "\033[";
    foreach(attribute; attributes){
        result = result ~ "%d;".format(attribute);
    }
    result = result ~ "%d;%dm%s\033[0;0m".format(fg, bg, content);
    return result;
}


void clean(){
	write("\033[H\033[2J");
}





More information about the Digitalmars-d mailing list