
Wednesday October 13, 2004
Roses are red, and so should be warnings
I am always surprised that text-based user interfaces in computers have
not fully utilized the power of colour. Ever tried to scroll through
10,000 lines of history in an xterm trying to locate that particular
error message, or the command you typed ? So why don't shells emit user
input and program output in different colours ? Why don't programs emit
errors and warnings in colours that stand out amid a sea of text ? If
only K&R had provided a colour argument to printf...
Anyway, here's one sample implementation of a "warning printf". It
makes warnings stand out by printing them in red. It's pretty portable
and works on every unix terminal (that I use :-)
// include stdio and stdarg
void wfprintf(FILE *fp, char *format, ...)
{
int is_tty = isatty (fileno(fp));
va_list ap;
va_start(ap, format);
// ANSI escape for the colour
if (is_tty)
fprintf(fp, "%c[31m", 0x1B); // 31 is colour red
/* print out real message */
(void) vfprintf(fp, format, ap);
// reset terminal
if (is_tty)
fprintf (fp, "%c[m", 0x1B);
}
Unfortunately, though, I haven't discovered any way to check isatty() on the Java platform.
(2004-10-13 03:56:32.0)
Permalink
|