This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: ANSI Colors in a C program under Linux...


Hendrix wrote:
> 
> Hi folks,
> 
> ...
> text-based colors in its interface...  How would it be possible for me
> to utilize these colors in my own C, or C++, program?...

Much the same way: have your output to stdout have an escape sequence.
The escape sequence has ESC[???m for attributes.  End the string, or
follow up soon with ESC[0m to reset to normal, unless you want
_everything_
thereafter to have those attributes.  The ESC is symbolized through
GCC with '\e'.

I used http://www.google.com/search?q=&num=100 to find "ansi escape
codes".
http://www.evergreen.edu/user/serv_res/research/bsi/people/dawn/program/ansi_esc.htm
seemed like a good page, and I used it to remind myself of the exact
contents of the sequences.

> Whenever I ask
> questions about color and graphics in "comp.lang.c" they send me
> elsewhere and say that it is compiler specific...???  I do not agree
> with this!!!

Well, I think that the ESC symbolization _is_ compiler specific.  But
even _more_ to the point, the color changes are _terminal_ specific.
The escape sequences below would work, but without any color change,
on an xterm coming from an HP-UX/PA box.  However, on an rxvt coming
from a Debian Linux/sparc box, the colors came out just fine.

> I think that a C program can be written with ANSI escape
> characters in order to colorize a screen, and I believe that this would
> be included in the ANSI Standard of the C language...  It isn't graphics
> functions, it just uses the ANSI escape sequences to print...

Here you go:

int main()
{
	printf("\e[31mHello\e[32m, \e[34mBlue \e[32mWorld\e[0m.\n");
 }

> 
> ...If it is possible to present ANSI
> colors in a C program (and still complie to the ANSI C Standard) then
> could someone show me a "Hello World" kind of program that has
> a blue background with yellow writing ...
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
My, aren't we picky?

OK, as you wish:

int main()
{
	printf("\e[33,44mHello, Blue World\e[0m.\n");
 }

-- 
Bolan.Meek@wcom.com 972-729-5387
bolan@koyote.com (home phone on request)
http://www.koyote.com/users/bolan
RE: xmailtool http://www.koyote.com/users/bolan/xmailtool/index.html
I am the "ILOVEGNU" signature virus. Just copy me to your signature.
This email was infected under the terms of the GNU General Public
License.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]