Colored Output in C/C++ (Part 1)

This is part 1 of a 3 part series in printing in color in C/C++

So you wanna do stuff like this from your C/C++ project…

Well I did too, and here’s a little write-up on my journey.

Using the simple escape-code method (described here, here), I wrote the following simple program.

cprintf.h

////////////////////////////////////////////////////////////////////////////////////////////////////
//
// (C)2011 Edwards Research Group
// You are licensed to use this work under a CC-BY-SA License.
// See: http://blog.edwards-research.com/about/
//      http://creativecommons.org/licenses/by-sa/3.0/us/
//
////////////////////////////////////////////////////////////////////////////////////////////////////

#include <stdio.h>
#include <stdarg.h>

#define CLR_BLACK   0
#define CLR_RED     1
#define CLR_GREEN   2
#define CLR_YELLOW  3
#define CLR_BLUE    4
#define CLR_MAGENTA 5
#define CLR_CYAN    6
#define CLR_WHITE   7

#define ATTR_NONE       0
#define ATTR_BOLD       1
#define ATTR_DIM        2
#define ATTR_UNDERLINE  4
#define ATTR_BLINK      5
#define ATTR_REVERSE    7

void cprint_init(int fg, int attr, int bg)
{
    if(bg != -1){
        printf("%c[%d;%d;%dm",27,attr,(30+fg),(40+bg));
    }
    else{
        printf("%c[%d;%dm",27,attr,(30+fg));
    }   
}

void cprint_rst(void)
{
    printf("%c[%dm", 27, 0);
}

int cprintf(int fg, int a, int bg, char * fmt, ...)
{
    va_list args;
    va_start(args, fmt);

    cprint_init(fg,a,bg);
    vprintf(fmt, args);
    cprint_rst();

    va_end(args);
}

cprintf_test.c

////////////////////////////////////////////////////////////////////////////////////////////////////
//
// (C)2011 Edwards Research Group
// You are licensed to use this work under a CC-BY-SA License.
// See: http://blog.edwards-research.com/about/
//      http://creativecommons.org/licenses/by-sa/3.0/us/
//
////////////////////////////////////////////////////////////////////////////////////////////////////

#include <stdio.h>

#include "cprintf.h"

void main(void)
{
    printf("Unformatted...\n");
    // Example with
    cprintf(4,1,-1,"Blue Bold...\n");
    cprintf(CLR_GREEN,ATTR_UNDERLINE,-1,"Green Underlined...\n");
    cprintf(CLR_YELLOW,ATTR_NONE,CLR_RED,"Yellow on Red...");
    printf("\n\n");

    int i,j;
    for(i=0; i<8; i++)
    {
        for(j=0; j<8; j++)
        {
            if(j == 3 | j == 6){ continue; }
            cprintf(i,j,-1,"FG=%d,A=%d", i, j);
            printf("    ");
        }
        printf("\n");
    }
    printf("\n");
    return;
}

NOTE: These will not work by simply copying and pasting, you must replace/insert the escape sequence as noted in the above linked references.

The above graphic is the program running in PuTTy via an SSH session. This is what the program looks like in gnome-terminal:

Well, this worked. But I didn’t really like having to specify all of the parameters (foreground, background, attributes) every time if all I wanted was the foreground changed. Also, this didn’t give me an easy way to define a color set, and then use that for the rest of the program. For example, it wasn’t easy to define an “alert” color set of, say, red bold text, and use that throughout.

My next step was to address this.

Continue to Part 2.

Posted Sunday, April 24th, 2011 under c, programming, tips and tricks.

Leave a Reply