How about colors and terminal graphics in std.format?

Chad J chadjoan at __spam.is.bad__gmail.com
Mon Mar 12 21:09:13 PDT 2012


On 03/12/2012 11:58 PM, Chad J wrote:
> On 03/12/2012 10:37 PM, James Miller wrote:
>>
>> I think the problem with putting it into formatting is that it is
>> inherently not output. IOW formatting should go anywhere, but colored
>> output is terminal-only.
>>
>> Also, there are differences between terminals and all sorts of crap
>> that just make this harder to do "simply". However, there's no reason
>> why there cant be an easy way to colorize output in std.terminal (or
>> whatever), that are basically just modified writef(ln) calls
>> (colorWritef?) that only output to stdout (and maybe stderr). I think
>> this would be a good way around it, because then everything that is
>> terminal-specific is kept in one place, and you don't get mistakes
>> like outputting color to a file because you did the wrong sequence, or
>> forgot to check that stdout is a terminal and all that.
>>
>> --
>> James Miller
>
> I do want to be able to format things besides color with the color
> formatting function. Maybe I can pick out the color format specifiers
> first and then pass the rest to format. It'd be a shame to reimplement
> format.
>
> At that point it would be cool if thrown exceptions and the like could
> do color formatting, and also know when to strip it out when writing to
> log files and such. I don't know how difficult or practical it would be,
> but I think that stack traces with color highlights would be awesome.
> It's pretty in-your-face user experience-wise too; might be good PR for D.
>
> So then, now for the fun part. What to name this function?
>
> zoosmellPooplord(ln)("%Cred(Text.%)");

Actually, wait a sec.

So I don't do it in format.

But I can create a color-format function that takes a terminal spec does 
some color formats and optionally some std.format formats as well.  It 
would output a string with the correct escape sequences (hey, you never 
know when you might want to inspect the escape sequences it produces).

So we define
string std.terminal.colorFormat(TerminalSpec spec, string fmtstr);
and std.format remains untouched.
And then...

Why not have writef(ln) use it?  writef(ln) functions know what they are 
attached to right?

They can just strip the formatting out for non-terminal destinations.

I would much prefer this.  I don't like the idea of having different 
writefln and termfln when they both do the same damn thing sans some 
simple coloring functionality.




More information about the Digitalmars-d mailing list