View mode: basic / threaded / horizontal-split · Log in · Help
October 21, 2012
Re: [RFC] ColorD
On Sunday, 21 October 2012 at 21:25:14 UTC, Era Scarecrow wrote:
> On Sunday, 21 October 2012 at 19:28:21 UTC, Robik wrote:
>> I would like to introduce ColorD, small library that allows to 
>> simply manipulate console output colors, both on Windows and 
>> Posix operating systems. It also supports font styles such as 
>> underline and strikethrough(Posix feature only).
>
>  Does this rely on nCurses? (or similar)

No, everything is written from scratch. On Windows side it used 
WinAPI functions, on Posix, ANSI codes.
October 21, 2012
Re: [RFC] ColorD
Era Scarecrow wrote:
> On Sunday, 21 October 2012 at 19:28:21 UTC, Robik wrote:
> >I would like to introduce ColorD, small library that allows to
> >simply manipulate console output colors, both on Windows and Posix
> >operating systems. It also supports font styles such as underline
> >and strikethrough(Posix feature only).
> 
>  Does this rely on nCurses? (or similar)

It doesn't. It sends ANSI escape codes on Posix.

Jens
October 21, 2012
Re: [RFC] ColorD
On 10/21/2012 05:01 PM, Jens Mueller wrote:
> Robik wrote:
>> Hello,
>>
>> I would like to introduce ColorD, small library that allows to
>> simply manipulate console output colors, both on Windows and Posix
>> operating systems. It also supports font styles such as underline
>> and strikethrough(Posix feature only).
>>
>>
>> Simple example:
>>
>> import std.stdio, colord;
>> void main()
>> {
>>      setConsoleColors(Fg.red, Bg.blue);
>>      writeln("Red text on blue background.");
>>      resetConsoleColors(); // Bring back initial state
>> }
>>
>>
>> Feedback welcome.
>>
>> GitHub: https://github.com/robik/ColorD
>
> Interesting looks solid to me.
> Some nit-picks:
> * Coloring on Posix depends a ANSI terminal. Can you check that a
>    terminal is ANSI compatible?
> * There are some magic numbers in the code. These may be difficult to
>    figure out for maintaining.
> * enum Color should maybe be same on all systems.
>    This is a rather small issue. But I may transfer the Color to another
>    system. So if it is possible there should only be one enum Color for
>    all systems.
> * Call it terminal or similar. Because other terminal related stuff can
>    be added and IMHO it's a better name.
>
> I have written a similar library. Not finished. Let's join forces.
> https://github.com/jkm/terminal
>
> Johannes Pfau has written a progress bar. I will add this.
>
> Jens

Hey that looks cool.

It seems to have a hard ncurses/termcap/etc dependency.

I'll admit when I started trying to work on doing this thing, I never 
got anything onto the screen.  What stopped me was that I couldn't 
figure out how to detect ncurses/termcap/etc.  I was going to shoot for 
Phobos inclusion and making Phobos always link with ncurses seems like a 
bad idea.

Ultimately I expect it to work with writeln or writefln to make it 
discoverable and easy to work with.

Back then I did design a format spec for introducing colors into format 
strings:
www.chadjoan.com/d/dmd.2.058/html/d/phobos/std_format.html
October 21, 2012
Re: [RFC] ColorD
Am 21.10.2012, 22:24 Uhr, schrieb Robik <szadows@gmail.com>:

> On Sunday, 21 October 2012 at 20:19:54 UTC, Peter Sommerfeld wrote:
>> Robik wrote:
>>
>>> I would like to introduce ColorD, small library that allows to simply  
>>> manipulate console output colors, both on Windows and Posix operating  
>>> systems.
>>
>>> GitHub: https://github.com/robik/ColorD
>>
>> On windows I got an error:
>> Not a property <EnumTypedef!<color,"fg">>.opDispatch
>> Same for "bg".
>>
>> void resetConsoleColors()
>> {
>>     setConsoleColors(Fg.initial, Bg.initial); // here
>> }
>>
>> Peter
>
> What version are you using?

dmd 2.060. -O -release  -inline -property -w -wi

Peter
October 21, 2012
Re: [RFC] ColorD
Chad J wrote:
> On 10/21/2012 05:01 PM, Jens Mueller wrote:
> >Robik wrote:
> >>Hello,
> >>
> >>I would like to introduce ColorD, small library that allows to
> >>simply manipulate console output colors, both on Windows and Posix
> >>operating systems. It also supports font styles such as underline
> >>and strikethrough(Posix feature only).
> >>
> >>
> >>Simple example:
> >>
> >>import std.stdio, colord;
> >>void main()
> >>{
> >>     setConsoleColors(Fg.red, Bg.blue);
> >>     writeln("Red text on blue background.");
> >>     resetConsoleColors(); // Bring back initial state
> >>}
> >>
> >>
> >>Feedback welcome.
> >>
> >>GitHub: https://github.com/robik/ColorD
> >
> >Interesting looks solid to me.
> >Some nit-picks:
> >* Coloring on Posix depends a ANSI terminal. Can you check that a
> >   terminal is ANSI compatible?
> >* There are some magic numbers in the code. These may be difficult to
> >   figure out for maintaining.
> >* enum Color should maybe be same on all systems.
> >   This is a rather small issue. But I may transfer the Color to another
> >   system. So if it is possible there should only be one enum Color for
> >   all systems.
> >* Call it terminal or similar. Because other terminal related stuff can
> >   be added and IMHO it's a better name.
> >
> >I have written a similar library. Not finished. Let's join forces.
> >https://github.com/jkm/terminal
> >
> >Johannes Pfau has written a progress bar. I will add this.
> >
> >Jens
> 
> Hey that looks cool.
> 
> It seems to have a hard ncurses/termcap/etc dependency.

Yes. I think you cannot make it portable without. Please proof me wrong
and I'll fix this.

> I'll admit when I started trying to work on doing this thing, I
> never got anything onto the screen.  What stopped me was that I
> couldn't figure out how to detect ncurses/termcap/etc.  I was going
> to shoot for Phobos inclusion and making Phobos always link with
> ncurses seems like a bad idea.

Dependence on Phobos is bad. If you can detect whether a terminal is
ANSI compatible then this mode should be default. But I don't know how
to detect this.

> Ultimately I expect it to work with writeln or writefln to make it
> discoverable and easy to work with.

One could try this. At least for Linux. You just have to add the
appropriate escape sequences. But this won't work on Windows.

> Back then I did design a format spec for introducing colors into
> format strings:
> www.chadjoan.com/d/dmd.2.058/html/d/phobos/std_format.html

I doubt that the Phobos maintainers will accept this. This is very
invasive.
I added writecf, writec, etc. with additional arguments.
writec(Color.red, "some text")
or
writecf(Color.red, "%s", "some text")
This is fine I think. But better options may be worth investigating.

Jens
October 21, 2012
Re: [RFC] ColorD
On 10/21/2012 12:28 PM, Robik wrote:
> Simple example:
>
> import std.stdio, colord;
> void main()
> {
>      setConsoleColors(Fg.red, Bg.blue);
>      writeln("Red text on blue background.");
>      resetConsoleColors(); // Bring back initial state
> }

Need a method to get the current state, and reset the current state. Otherwise, 
nested calls to the console functions will screw up the state.

I.e.:

    auto save = getConsoleState();
    setConsoleColors(Fg.red, Bg.blue);
    writeln("Red text on blue background.");
    setConsoleState(save); // Bring back initial state

Or better:

    auto save = getConsoleState();
    scope (exit) setConsoleState(save);
    setConsoleColors(Fg.red, Bg.blue);
    writeln("Red text on blue background.");
October 21, 2012
Re: [RFC] ColorD
On 10/21/2012 06:11 PM, Jens Mueller wrote:
> Chad J wrote:
>> On 10/21/2012 05:01 PM, Jens Mueller wrote:
>>
>> It seems to have a hard ncurses/termcap/etc dependency.
>
> Yes. I think you cannot make it portable without. Please proof me wrong
> and I'll fix this.
>

Well, traditionally it's done with automake/autoconf.  You'd end up with 
preprocessor defines that tell you whether the lib has been statically 
linked or not.  This isn't available here because Phobos doesn't use 
these as a build system and I hope it never does.

>> I'll admit when I started trying to work on doing this thing, I
>> never got anything onto the screen.  What stopped me was that I
>> couldn't figure out how to detect ncurses/termcap/etc.  I was going
>> to shoot for Phobos inclusion and making Phobos always link with
>> ncurses seems like a bad idea.
>
> Dependence on Phobos is bad. If you can detect whether a terminal is
> ANSI compatible then this mode should be default. But I don't know how
> to detect this.
>

Wrong direction on the dependency.  I wouldn't expect Terminal 
coloring/detection to rely on Phobos.  I'd expect it to be one of the 
lower-level modules built into Phobos.

>> Ultimately I expect it to work with writeln or writefln to make it
>> discoverable and easy to work with.
>
> One could try this. At least for Linux. You just have to add the
> appropriate escape sequences. But this won't work on Windows.
>

I remember having a plan for this.  See below.

>> Back then I did design a format spec for introducing colors into
>> format strings:
>> www.chadjoan.com/d/dmd.2.058/html/d/phobos/std_format.html
>
> I doubt that the Phobos maintainers will accept this. This is very
> invasive.

Hmmm, depends what is meant by invasive.

I feel it's the only way to have discoverable and concise syntax.  I'd 
be pretty disappointed if they didn't, regardless of who submits the 
pull request.

I remember it being possible in Phobos to determine the destination of 
the format operation.  If the destination is a string in memory, then no 
color formatting would be applied.  If the destination is a Linux 
terminal of some kind, then some ncurses terminal info would be looked 
up (possible a cached lookup) and escape sequences generated based on 
that.  If the destination is a Windows terminal, then these approaches 
can be considered:
(1) Split the formatted text up on the color format boundaries.  Send 
the slices into the stream one by one, calling the necessary WinAPI 
color formatting functions inbetween.  I think this might not have been 
possible with Phobos' architecture.
(2) Insert ANSI escape sequences into the text.  The I/O code for 
Windows would then have to intercept these and convert them into the 
appropriate WinAPI calls.  I think this was possible, and even 
distinguishable from the case of writing to a string in memory.

If the invasiveness worry comes from the possibility of dumping escape 
sequences into non-terminal destinations, then I hope the above wall of 
text can alleviate that concern.

> I added writecf, writec, etc. with additional arguments.
> writec(Color.red, "some text")
> or
> writecf(Color.red, "%s", "some text")
> This is fine I think. But better options may be worth investigating.
>
> Jens

I really think this should be in Phobos.  If it doesn't go into Phobos, 
then people will write crappy terminal apps with no color.  If it does 
go into Phobos, then the better devs will see the opportunity and use 
it.  Something 3rd party is much less discoverable and won't have nearly 
as much impact.  The use case is almost all CLI apps, so it's not like 
an uncommon corner-case or something.

I run a Gentoo system where things are configured to use color output 
wherever possible.  The portage devs went through all of the necessary 
contortions to get Python to output colored text, somehow.  I feel the 
end result is indispensable.  Color is an extremely useful tool for 
making sure that the user doesn't overlook important bits while scanning 
text.  Outside of Gentoo, I find this most notable in grep: uncolored 
grep output is just awful, but the coloring makes it possible to easily 
identify why the regular expression behaved the way it did.

I look forward to a better CLI ecosystem where highly reliable D 
programs are written quickly and write beautiful colored output ;)
October 21, 2012
Re: [RFC] ColorD
Additionally note that the format syntax handles Walter's concerns here:
http://forum.dlang.org/post/k61t63$pi4$1@digitalmars.com

The color format syntax uses a pair of matched parentheses, and thus 
makes it impossible to leave the console in a different state than when 
the formatting call was entered.

Lower level stuff like terminal functions dedicated specifically to 
saving/restoring state would make sense and be used underneath the 
formatter to accomplish what it does.
October 21, 2012
Re: [RFC] ColorD
Chad J wrote:
> On 10/21/2012 06:11 PM, Jens Mueller wrote:
> >Chad J wrote:
> >>On 10/21/2012 05:01 PM, Jens Mueller wrote:
> >>
> >>It seems to have a hard ncurses/termcap/etc dependency.
> >
> >Yes. I think you cannot make it portable without. Please proof me wrong
> >and I'll fix this.
> >
> 
> Well, traditionally it's done with automake/autoconf.  You'd end up
> with preprocessor defines that tell you whether the lib has been
> statically linked or not.  This isn't available here because Phobos
> doesn't use these as a build system and I hope it never does.

I mean to detect if your terminal is ANSI compatible without adding
another dependency.
It's easy to provide different version(...) to support different modes.
One could do something like:
1. Check at run time if ncurses etc. are available.
 * If they are use them.
 * Otherwise fall back to ANSI codes or throw an Exception.

What do you think?

> >>I'll admit when I started trying to work on doing this thing, I
> >>never got anything onto the screen.  What stopped me was that I
> >>couldn't figure out how to detect ncurses/termcap/etc.  I was going
> >>to shoot for Phobos inclusion and making Phobos always link with
> >>ncurses seems like a bad idea.
> >
> >Dependence on Phobos is bad. If you can detect whether a terminal is
> >ANSI compatible then this mode should be default. But I don't know how
> >to detect this.
> >
> 
> Wrong direction on the dependency.  I wouldn't expect Terminal
> coloring/detection to rely on Phobos.  I'd expect it to be one of
> the lower-level modules built into Phobos.

I mean it's bad to have Phobos depend on ncurses.
Though one can go with loading at run time.

> >>Ultimately I expect it to work with writeln or writefln to make it
> >>discoverable and easy to work with.
> >
> >One could try this. At least for Linux. You just have to add the
> >appropriate escape sequences. But this won't work on Windows.
> >
> 
> I remember having a plan for this.  See below.
> 
> >>Back then I did design a format spec for introducing colors into
> >>format strings:
> >>www.chadjoan.com/d/dmd.2.058/html/d/phobos/std_format.html
> >
> >I doubt that the Phobos maintainers will accept this. This is very
> >invasive.
> 
> Hmmm, depends what is meant by invasive.
> 
> I feel it's the only way to have discoverable and concise syntax.
> I'd be pretty disappointed if they didn't, regardless of who submits
> the pull request.
> 
> I remember it being possible in Phobos to determine the destination
> of the format operation.  If the destination is a string in memory,
> then no color formatting would be applied.  If the destination is a
> Linux terminal of some kind, then some ncurses terminal info would
> be looked up (possible a cached lookup) and escape sequences
> generated based on that.  If the destination is a Windows terminal,
> then these approaches can be considered:
> (1) Split the formatted text up on the color format boundaries.
> Send the slices into the stream one by one, calling the necessary
> WinAPI color formatting functions inbetween.  I think this might not
> have been possible with Phobos' architecture.
> (2) Insert ANSI escape sequences into the text.  The I/O code for
> Windows would then have to intercept these and convert them into the
> appropriate WinAPI calls.  I think this was possible, and even
> distinguishable from the case of writing to a string in memory.
> 
> If the invasiveness worry comes from the possibility of dumping
> escape sequences into non-terminal destinations, then I hope the
> above wall of text can alleviate that concern.

Checking whether something is a terminal can be done using isatty on the
file handle. I think this will work.
But it is invasive because you want to add it to the formatting spec. Is
this the usual way it is done? I don't know how it is done in Python
or other languages.

> >I added writecf, writec, etc. with additional arguments.
> >writec(Color.red, "some text")
> >or
> >writecf(Color.red, "%s", "some text")
> >This is fine I think. But better options may be worth investigating.
> >
> >Jens
> 
> I really think this should be in Phobos.  If it doesn't go into
> Phobos, then people will write crappy terminal apps with no color.
> If it does go into Phobos, then the better devs will see the
> opportunity and use it.  Something 3rd party is much less
> discoverable and won't have nearly as much impact.  The use case is
> almost all CLI apps, so it's not like an uncommon corner-case or
> something.

True. When I asked there was less/no interest. I think we should just
join and make one module that shows up top ten when googled for "D
terminal/console color".

> I run a Gentoo system where things are configured to use color
> output wherever possible.  The portage devs went through all of the
> necessary contortions to get Python to output colored text, somehow.
> I feel the end result is indispensable.  Color is an extremely
> useful tool for making sure that the user doesn't overlook important
> bits while scanning text.  Outside of Gentoo, I find this most
> notable in grep: uncolored grep output is just awful, but the
> coloring makes it possible to easily identify why the regular
> expression behaved the way it did.
> 
> I look forward to a better CLI ecosystem where highly reliable D
> programs are written quickly and write beautiful colored output ;)

It is. I'd like you to join and get this done.
And you're right we should have Phobos integration in mind. Maybe they
will add it. But in the mean time we can have a separate module.

Jens
October 21, 2012
Re: [RFC] ColorD
Walter Bright wrote:
> On 10/21/2012 12:28 PM, Robik wrote:
> > Simple example:
> >
> > import std.stdio, colord;
> > void main()
> > {
> >      setConsoleColors(Fg.red, Bg.blue);
> >      writeln("Red text on blue background.");
> >      resetConsoleColors(); // Bring back initial state
> > }
> 
> Need a method to get the current state, and reset the current state.
> Otherwise, nested calls to the console functions will screw up the
> state.
> 
> I.e.:
> 
>     auto save = getConsoleState();
>     setConsoleColors(Fg.red, Bg.blue);
>     writeln("Red text on blue background.");
>     setConsoleState(save); // Bring back initial state
> 
> Or better:
> 
>     auto save = getConsoleState();
>     scope (exit) setConsoleState(save);
>     setConsoleColors(Fg.red, Bg.blue);
>     writeln("Red text on blue background.");

Very true.
Problem is that on Linux (probably most Unix* systems) you cannot get
the current color. Only the default one.
I added wrappers like above.
https://github.com/jkm/terminal/blob/master/src/terminal.d#L187

Jens
1 2 3 4 5 6
Top | Discussion index | About this forum | D home