www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - termcolor-d - Colors with writeln(...);

reply Vladimirs Nordholm <v vladde.net> writes:
https://github.com/vladdeSV/termcolor-d

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

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;

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

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

     // only tested on macOS running zsh using 
iTerm2/Hyper.js/Terminal.app

Hope this helps those who are making CLI applications :^)
Nov 21
next sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Wed, Nov 21, 2018 at 06:36:06PM +0000, Vladimirs Nordholm via
Digitalmars-d-announce wrote:
 https://github.com/vladdeSV/termcolor-d
 
 Saw a library recently which allowed you to color text, but it had an
 odd syntax.
 
 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;
 
     // Color → Green → Foreground
     writeln(C.green.fg, "Green text", resetColor);
 
     // Color → Red → Background
     writeln(C.red.bg, "Red background", resetColor);
 
     // only tested on macOS running zsh using iTerm2/Hyper.js/Terminal.app
 
 Hope this helps those who are making CLI applications :^)
Clever idea! Doesn't quite cover all the color features of newer terminals, but good enough for basic coloring on the terminal. Maybe I'll steal your idea next time I'm looking for some terminal colors. :D T -- I am not young enough to know everything. -- Oscar Wilde
Nov 21
parent Vladimirs Nordholm <v vladde.net> writes:
On Wednesday, 21 November 2018 at 23:30:39 UTC, H. S. Teoh wrote:
 On Wed, Nov 21, 2018 at 06:36:06PM +0000, Vladimirs Nordholm 
 via Digitalmars-d-announce wrote:
 [...]
Clever idea! Doesn't quite cover all the color features of newer terminals, but good enough for basic coloring on the terminal. Maybe I'll steal your idea next time I'm looking for some terminal colors. :D T
Thanks, and feel free to!
Nov 21
prev sibling next sibling parent reply Dennis <dkorpel gmail.com> writes:
On Wednesday, 21 November 2018 at 18:36:06 UTC, Vladimirs 
Nordholm wrote:
 (hackish, POSIX only)
Windows support coming? :)
Nov 21
parent reply Vladimirs Nordholm <v vladde.net> writes:
On Wednesday, 21 November 2018 at 23:46:00 UTC, Dennis wrote:
 On Wednesday, 21 November 2018 at 18:36:06 UTC, Vladimirs 
 Nordholm wrote:
 (hackish, POSIX only)
Windows support coming? :)
Maybe during another lunch break ;) However, not sure if it's active anymore, but ConsoleD (by Robik and Adam D. Ruppe) has most Windows specific colors and attributes available. Maybe give that a look?
Nov 21
next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Wed, Nov 21, 2018 at 11:49:29PM +0000, Vladimirs Nordholm via
Digitalmars-d-announce wrote:
 On Wednesday, 21 November 2018 at 23:46:00 UTC, Dennis wrote:
 On Wednesday, 21 November 2018 at 18:36:06 UTC, Vladimirs Nordholm
 wrote:
 (hackish, POSIX only)
Windows support coming? :)
Maybe during another lunch break ;) However, not sure if it's active anymore, but ConsoleD (by Robik and Adam D. Ruppe) has most Windows specific colors and attributes available. Maybe give that a look?
[...] I've used Adam Ruppe's terminal.d to great effect in my CLI programs. Highly recommended: https://github.com/adamdruppe/arsd/blob/master/terminal.d T -- Why did the mathematician reinvent the square wheel? Because he wanted to drive smoothly over an inverted catenary road.
Nov 21
prev sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
On Wednesday, 21 November 2018 at 23:49:29 UTC, Vladimirs 
Nordholm wrote:
 However, not sure if it's active anymore, but ConsoleD (by 
 Robik and Adam D. Ruppe) has most Windows specific colors and 
 attributes available. Maybe give that a look?
Well, the consoleD part I'm not sure about, but I still work on my terminal.d which includes cross-platform color support (among a lot more like mouse input, line editing, etc). Your api is not compatible with old Windows, but newer versions of Windows support the same esc sequences linux uses - though I think you have to opt into it with a console api call. (I might be adding support to this to terminal.d but eh i already have the features in there)
Nov 21
prev sibling parent Shriramana Sharma writes:
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.
Nov 22