www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - ColorD

reply "Robik" <szadows gmail.com> writes:
I would like to share with my new library written in D. As name 
may suggest (or not) it adds color to your console output, it 
works on both Linux and Windows platforms. I haven't seen any 
similar library for D language, so I decided to create this one.

Source and examples(included in Readme file) can be found on 
GitHub repo: https://github.com/robik/ColorD

Regards,
Robik.
May 26 2012
next sibling parent bioinfornatics <bioinfornatics fedoraproject.org> writes:
mine code for ansi terminal
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
enum Attributes : size_t{
    RESET,
    BOLD,
    NORMAL,
    UNDERLINE,
    BLINK,
    INVERSE,
    HIDE
}

size_t getBackgroundColor( string color ){
    size_t result;
    switch(color){
        case("black"):
            result =3D 30;
            break;
        case("red"):
            result =3D 31;
            break;
        case("green"):
            result =3D 32;
            break;
        case("yellow"):
            result =3D 33;
            break;
        case("blue"):
            result =3D 34;
            break;
        case("violet"):
            result =3D 35;
            break;
        case("cyan"):
            result =3D 36;
            break;
        case("white"):
            result =3D 37;
            break;
        case("default"):
            result =3D 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 =3D 40;
            break;
        case("red"):
            result =3D 41;
            break;
        case("green"):
            result =3D 42;
            break;
        case("yellow"):
            result =3D 43;
            break;
        case("blue"):
            result =3D 44;
            break;
        case("violet"):
            result =3D 45;
            break;
        case("cyan"):
            result =3D 46;
            break;
        case("white"):
            result =3D 47;
            break;
        case("default"):
            result =3D 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       =3D getBackgroundColor( backgroundColor );
    size_t fg       =3D getUndergroundColor( undergroundColor );
    string result  =3D "\033[";
    foreach(attribute; attributes){
        result =3D result ~ "%d;".format(attribute);
    }
    result =3D result ~ "%d;%dm%s\033[0;0m".format(fg, bg, content);
    return result;
}


void clean(){
	write("\033[H\033[2J");
}
May 26 2012
prev sibling parent reply Marco Leise <Marco.Leise gmx.de> writes:
Am Sat, 26 May 2012 16:30:58 +0200
schrieb "Robik" <szadows gmail.com>:

 I would like to share with my new library written in D. As name 
 may suggest (or not) it adds color to your console output, it 
 works on both Linux and Windows platforms. I haven't seen any 
 similar library for D language, so I decided to create this one.
 
 Source and examples(included in Readme file) can be found on 
 GitHub repo: https://github.com/robik/ColorD
 
 Regards,
 Robik.
Ah, this can be used to pimp the console output. Give it the following additions to make it complete and useful: * fg/bg colors are just two attributes, add e.g. bold, underline, italic (http://en.wikipedia.org/wiki/ANSI_escape_code) * detect type of "stdout"; terminal or pipe/file (Posix: http://linux.die.net/man/3/isatty) The latter is important to not accidentally write escape codes into a file if output is redirected. Some tools let you chose to output with or without colors, so it would be nice if you library could offer a "isAnsiTerminal" function, so the programmer can chose to output with or without colors. :) This is a good idea! -- Marco
May 28 2012
parent reply "Robik" <szadows gmail.com> writes:
On Monday, 28 May 2012 at 13:08:27 UTC, Marco Leise wrote:
 Am Sat, 26 May 2012 16:30:58 +0200
 schrieb "Robik" <szadows gmail.com>:

 I would like to share with my new library written in D. As 
 name may suggest (or not) it adds color to your console 
 output, it works on both Linux and Windows platforms. I 
 haven't seen any similar library for D language, so I decided 
 to create this one.
 
 Source and examples(included in Readme file) can be found on 
 GitHub repo: https://github.com/robik/ColorD
 
 Regards,
 Robik.
Ah, this can be used to pimp the console output. Give it the following additions to make it complete and useful: * fg/bg colors are just two attributes, add e.g. bold, underline, italic (http://en.wikipedia.org/wiki/ANSI_escape_code) * detect type of "stdout"; terminal or pipe/file (Posix: http://linux.die.net/man/3/isatty) The latter is important to not accidentally write escape codes into a file if output is redirected. Some tools let you chose to output with or without colors, so it would be nice if you library could offer a "isAnsiTerminal" function, so the programmer can chose to output with or without colors. :) This is a good idea!
Okay, I think I will have to set up VM and start working on it. Thanks for your suggestion by the way!
May 28 2012
parent reply "Damian Ziemba" <spam dzfl.pl> writes:
On Monday, 28 May 2012 at 13:30:44 UTC, Robik wrote:
 On Monday, 28 May 2012 at 13:08:27 UTC, Marco Leise wrote:
 Am Sat, 26 May 2012 16:30:58 +0200
 schrieb "Robik" <szadows gmail.com>:

 I would like to share with my new library written in D. As 
 name may suggest (or not) it adds color to your console 
 output, it works on both Linux and Windows platforms. I 
 haven't seen any similar library for D language, so I decided 
 to create this one.
 
 Source and examples(included in Readme file) can be found on 
 GitHub repo: https://github.com/robik/ColorD
 
 Regards,
 Robik.
+1
May 28 2012
parent reply 1100110 <10equals2 gmail.com> writes:
On Mon, 28 May 2012 11:55:24 -0500, Damian Ziemba <spam dzfl.pl> wrote:

 On Monday, 28 May 2012 at 13:30:44 UTC, Robik wrote:
 On Monday, 28 May 2012 at 13:08:27 UTC, Marco Leise wrote:
 Am Sat, 26 May 2012 16:30:58 +0200
 schrieb "Robik" <szadows gmail.com>:

 I would like to share with my new library written in D. As name may  
 suggest (or not) it adds color to your console output, it works on  
 both Linux and Windows platforms. I haven't seen any similar library  
 for D language, so I decided to create this one.
  Source and examples(included in Readme file) can be found on GitHub  
 repo: https://github.com/robik/ColorD
  Regards,
 Robik.
+1
Nice! Does it support checking if the terminal supports color? -- Using Opera's revolutionary email client: http://www.opera.com/mail/
May 28 2012
parent "Robik" <szadows gmail.com> writes:
On Monday, 28 May 2012 at 17:21:25 UTC, 1100110 wrote:
 On Mon, 28 May 2012 11:55:24 -0500, Damian Ziemba 
 <spam dzfl.pl> wrote:

 On Monday, 28 May 2012 at 13:30:44 UTC, Robik wrote:
 On Monday, 28 May 2012 at 13:08:27 UTC, Marco Leise wrote:
 Am Sat, 26 May 2012 16:30:58 +0200
 schrieb "Robik" <szadows gmail.com>:

 I would like to share with my new library written in D. As 
 name may suggest (or not) it adds color to your console 
 output, it works on both Linux and Windows platforms. I 
 haven't seen any similar library for D language, so I 
 decided to create this one.
 Source and examples(included in Readme file) can be found 
 on GitHub repo: https://github.com/robik/ColorD
 Regards,
 Robik.
+1
Nice! Does it support checking if the terminal supports color?
Not yet. But I guess I will have to add it soon. Regards, Robik.
May 28 2012