Unix Programming - printing in colour on terminal

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > April 2005 > printing in colour on terminal





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author printing in colour on terminal
qwejohn@hotmail.com

2005-04-21, 7:52 am

Hello,
Can anybody give me a pointer/advice how to write a simple hello world
program which will print in a specified color on the monitior ?
(I mean when running from a shell).

(If it must be done with some graphic library , I prefer it will be
done
with a simple one like curses, or something like it).

Regards,
John

loic-dev@gmx.net

2005-04-21, 7:52 am

Hi,

> Can anybody give me a pointer/advice how to write a simple hello

world
> program which will print in a specified color on the monitior ?
> (I mean when running from a shell).


Yes, if your xterm supports colors, simply use the color escape
sequences
bash$ echo -e "\033[0;31mHello world in red\033[0;0m"

A detailed list might be found at:
http://www.cse.unsw.edu.au/~saraf/cluefiles.html

Cheers,
Loic.

qwejohn@hotmail.com

2005-04-21, 7:52 am

Thnaks;

I want to do it in a simple
c program which prints
hello world
Any idea?

Thanks,
John

loic-dev@gmx.net

2005-04-21, 5:59 pm

Hi,

> I want to do it in a simple
> c program which prints
> hello world
> Any idea?


just use printf() instead of echo -e
printf("\033[0;31mHello world in red\033[0;0m\n");

HTH,
Loic.

phil_gg04@treefic.com

2005-04-21, 5:59 pm

> Can anybody give me a pointer/advice how to write a simple hello
world
> program [in C] which will print in a specified color on the monitior

?

The shortest program is to directly insert the control sequences as
others have suggested. The trouble is that different terminals have
different control sequences. If you want your program to work on many
different terminals you should use a library. At the lowest level,
terminfo and termcap can tell you want sequences a terminal supports.
At a slightly higher level the various variants of curses are what you
want. If you have ncurses installed, see for example "man 3ncurses
color". Using curses will take a bit longer to learn than just putting
escape sequences in a printf, but it could be a better solution in the
longer term.

Good luck,

--Phil.

Floyd L. Davidson

2005-04-21, 5:59 pm

qwejohn@hotmail.com wrote:
>Thnaks;
>
> I want to do it in a simple
>c program which prints
>hello world
>Any idea?


One idea is *do NOT* embed escape sequences directly into your
program! There are enough variations between terminals that it
will sooner or later byte you.

Here is probably enough to get you started. If you'd like to see
a (significantly) larger demo program that shows far more than you'd
ever really want to do without invoking all of curses itself, look
here http://uniat/home/code/terminfo/index.html.

Regardless, download the latest version of Thomas Dickey's ncurses
from http://invisible-island.net/ncurses/ and read the docs as well
as look at the example programs in the /test/ directory.

/* compile command:
* gcc -O2 -Wall -W foo.c -lncurses -o foo
*/

#include <stdio.h>
#include <stdlib.h>
#include <term.h>

void init_terminal(void); /* init the terminal */
void tisend(char *); /* send string */
void cls(void); /* clear the screen */
void gotoxy(int, int); /* position the cursor */
void sc(void); /* save cursor location */
void rc(void); /* restore cursor location */
void redText(void); /* set foreground to red */
void regText(void); /* restore foreground color */

int main(void)
{
init_terminal();
sc();
cls();
gotoxy(10,10);
redText();
printf("This starts at row 10, column 10");
regText();
fflush(stdout);
rc();
return EXIT_SUCCESS;
}
void redText()
{
tisend(tparm(set_a_foreground, 1));
}

void regText()
{
tputs(exit_attribute_mode, 1, putchar);
}

/* home the cursor and clear to end of screen */
void cls(void)
{
tputs(clear_screen, 1, putchar);
}

/* cursor positioning, home is 1,1 */
void gotoxy(int col, int row)
{
tisend(tparm(cursor_address, row, col));
}

/* restore the cursor location */
void
rc(void)
{
tputs(restore_cursor, 1, putchar);
}

/* save the cursor location */
void sc(void)
{
tputs(save_cursor, 1, putchar);
}

/* send a terminfo parameter string returned by tparm() */
void
tisend(char *parm)
{
if ( (char *) -1 != parm ) {
tputs(parm, 1, putchar);
}
}

/* set up the terminal */
void init_terminal(void)
{
char *term = getenv("TERM");
int err_ret;

if ( !strcmp(term, "xterm")) {
term="xterm-color";
}

if ( !term || ! *term) {
fprintf(stderr,"The TERM environment variable is not set.\n");
exit(EXIT_FAILURE);
}

setupterm(term, 1, &err_ret);

switch (err_ret) {
case -1:
fprintf(stderr,"Can't access terminfo database - ");
fprintf(stderr,"check TERMINFO environment variable\n");
exit(EXIT_FAILURE);
break;
case 0:
fprintf(stderr, "Can't find entry for terminal %s\n", term);
exit(EXIT_FAILURE);
case 1:
break;
default:
fprintf(stderr, "Unknown tgetent return code %d\n", err_ret);
exit(EXIT_FAILURE);
}
}

--
Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd@barrow.com
Thomas Dickey

2005-04-21, 5:59 pm

loic-dev@gmx.net wrote:
> A detailed list might be found at:
> http://www.cse.unsw.edu.au/~saraf/cluefiles.html


It might be, but is not.

--
Thomas E. Dickey
http://invisible-island.net
ftp://invisible-island.net
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com