C++ Logo


Advanced search

Re: Color Text Output

From: Thiago Macieira <thiago_at_[hidden]>
Date: Fri, 25 Jun 2021 13:14:25 -0700
On Friday, 25 June 2021 00:14:14 PDT David Braun via Std-Proposals wrote:
> I propose to add colored text output to the C++ standard via the <iomanip>
> header. See the attached document for details.
 I am looking for feedback,
> particularly for the ANSI (Linux/Unix/Mac) implementation.

Your implementation, as a proof of concept, is probably fine, but way too
simplistic. Just off the top of my head:

1) A proper implementation would need to use termcap.h or teaminfo.h to obtain
the information on the actual terminal on which colours are supported and what
the correct escape sequence is.

2) What about background colours? Bold colors? 256 color tables, or 16M/RGB
ones? Your proposal said you didn't want them, but please give more rationale
and whether this can be expected in the future or as an extension. Can we also
expect more from termios.h at any point in the future?

3) You probably want to have a property to control whether colours are enabled
and you should provide a good algorithm for determining the default. For
example, if isatty(fileno(stream)), then colours are enabled for that stream,
otherwise they aren't. But one may want to force-enable them (usually with
after the user provided the option), for example, when writing to a pipe that
is processed and then written to a terminal, like less -R.

4) Note I said "stream" not "stdout". One can open /dev/tty and write to that,
without it being stdout. Some other programs may want to move their output
stream to another, so they can close stdout to avoid accidental messages being
written. Therefore, this API must work on any stream, not just std::cout.

5) should there be some kind of feedback for "yeah, colours are supported"?
Some applications may decide that, without colours, they need to do something
different with their output.
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DPG Cloud Engineering

Received on 2021-06-25 15:14:30