Zach Shepherd's WordPress Blog

Just another WordPress weblog

Tuesday, April 21, 2009

Styled output (unix c/c++)

After seeing a program that had colored and styled (bold) output, I decided to look into it. There isn’t a lot of information available, so I’m not sure how widely supported it is. It seems to be compatible with gnome-terminal in a few versions of Ubuntu, but I’m not sure what else (if you try it, let me know).

After a lot of trial-and-error (the tutorials I found were all incomplete), I wrote the following program to output the “code” to style text in any one of 8 colors (with an additional 8 background colors) and 4 styles (plain, bold, underlined, and strikethrough). The output leads me to believe underline is poorly supported (only some text is underlined), but for coloring error messages, this is probably sufficient.

Update: It looks like the proper term for these are ANSI escape codes. When you use the proper term, it’s much easier to find tutorials. I’ll leave this up in case anyone stumbles across it not knowing the proper term.


/**
* Author: Zach Shepherd
* Created: April 21, 2009
* License: Released to the public domain
*
* NB: Maximize your screen (terminal must be at least 139 characters wide)
*
* Use: Find the code for the display you want and use in the following code:
* cout << "\e[CODE" << Formated Output << "\e[m";
*
* To combine two effects (e.g. bold and underline), concat the prefixes:
* 1 is bold, 4 is underline so "1;4" is bold underline
*/

#include
#include
using namespace std;

int main() {
string label[8] = {"black", "red", "green", "brown", "blue", "purple", "cyan", "gray"};
string stylelabel[5] = {"plain", "bold", "underline", "strikethrough"};

string colorcode[8] = {"30", "31", "32", "33", "34", "35", "36", "37"};
string bgcolorcode[8] = {"40", "41", "42", "43", "44", "45", "46", "47"};
string stylecode[5] = {"0", "1", "4", "9"};

for (int i=0; i<4; i++) {
cout << stylelabel[i] << endl; // Style Label

// BG Label
cout << "\tDefault\t"; //
for (int k=0; k<8; k++) {
cout << label[k] << "\t\t";
}
cout << endl;

cout << "Default\t"; // FG Label

/* Just style */
cout << "\e[" << stylecode[i] << "m"
<< stylecode[i] << "m"
<< "\e[m" << "\t";
/* Style and background color */
for (int k=0; k<8; k++) {
cout << "\e[" << stylecode[i] << ";" << bgcolorcode[k] << "m"
<< stylecode[i] << ";" << bgcolorcode[k] << "m"
<< "\e[m" << "\t\t";
}
cout << endl;

for (int j=0; j<8; j++) {
cout << label[j] << "\t"; // FG Label

/* Style and foreground color */
cout << "\e[" << stylecode[i] << ";" << colorcode[j] << "m"
<< stylecode[i] << ";" << colorcode[j] << "m"
<< "\e[m" << "\t";

/* All three */
for (int k=0; k<8; k++) {
cout << "\e[" << stylecode[i] << ";" << colorcode[j] << ";" << bgcolorcode[k] << "m"
<< stylecode[i] << ";" << colorcode[j] << ";" << bgcolorcode[k] << "m"
<< "\e[m" << "\t";
}
cout << endl;
}
cout << endl;
}

cout << "\e[7m" << "7m - defaults, reversed" << "\e[m" << endl;

return 0;
}

the cpp file

posted by Zach at 7:04 pm  

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress