Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
May 26, 2012 ColorD | ||||
---|---|---|---|---|
| ||||
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 Re: ColorD | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robik | 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"); } |
May 28, 2012 Re: ColorD | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robik | 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 Re: ColorD | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marco Leise | 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 Re: ColorD | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robik | 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 Re: ColorD | ||||
---|---|---|---|---|
| ||||
Posted in reply to Damian Ziemba | 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 Re: ColorD | ||||
---|---|---|---|---|
| ||||
Posted in reply to 1100110 | 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.
|
Copyright © 1999-2021 by the D Language Foundation