termcolor-d - Colors with writeln(...);

Shriramana Sharma not.sure.why.a.mail.id.is.needed.even.if.invalid at gmail.com
Fri Nov 23 02:15:01 UTC 2018


On Wednesday, 21 November 2018 at 18:36:06 UTC, Vladimirs 
Nordholm wrote:
> https://github.com/vladdeSV/termcolor-d

https://github.com/jamadagni/textattr/

> Saw a library recently which allowed you to color text, but it 
> had an odd syntax.

Maybe the documentation, in trying to be exhaustive, hasn't 
showed how simple it can be. I'm not sure why it is perceived as 
odd. Can you clarify?

> Since I already had some code for coloring text in terminals, I 
> made this (hackish, POSIX only) project during lunch break. It 
> in action:
>
>     import std.stdio : writeln;
>     import termcolor;

import textattr;

>     // Color → Green → Foreground
>     writeln(C.green.fg, "Green text", resetColor);

       writeln(ta("green"), "Green text", ta("off"));

*or* the shorter:

       writeln(ta("g"), "Green text", ta("f"));

*or* the even shorter:

       tawrite("@g", "Green text", "@f", '\n');

The @ indicates that it is an "at"tribute.

It's not an issue to add a tawriteln which adds the newline at 
the end. I first posted the basic library thinking to make 
additions as per demands later.

>     // Color → Red → Background
>     writeln(C.red.bg, "Red background", resetColor);

       tawrite("@/red", "Red background", "@off", '\n');

The / indicates that it's a background colour. This is stated in 
the HTML documentation, but maybe not clear within the 
limitations of the README.md. I'll see what I can do to improve 
it.

Having premade symbols in the library as you have done is easy 
enough for basic 16-colour or even attribute support, but adding 
256 colour or true colour support this way would unnecessarily 
use up too much memory.


More information about the Digitalmars-d-announce mailing list