views:

183

answers:

3

I have a C file running on Linux. It prints some lines in red (failures) and some in green (passes). As you might expect, it uses escape codes in the printf statements as follows:

#define BLACK  "\033[22;30m"
#define GREEN  "\033[22;31m"

printf(GREEN "this will show up green" BLACK "\n");

If the BLACK at the end wasn't there, the terminal text will continue to be green for everything. In case you didn't catch it, that's fine for a terminal window with a non-black background, but otherwise you'll end up with black-on-black. Not good! Running the program has this problem, as does capturing the output in a text file and then viewing the file with "more" or "less".

Is there a code to restore defaults instead of specifying a color at the end of the printf statement? This needs to be in C, but I would be interested in reading about other approaches.

Update: Thank you all. Your responses helped me find even more useful info elsewhere. I updated my macros as follows (note 31 is for red and I fixed that typo below):

#define RESET_COLOR "\e[m"
#define MAKE_GREEN "\e[32m"

printf(MAKE_GREEN "this will show up green" RESET_COLOR "\n");

I found the following links helpful in understanding how these codes work:

http://www.phwinfo.com/forum/comp-unix-shell/450861-bash-shell-escapes-not-working-via-putty-ssh.html explains what these escape sequences do, and to use ncurses if portability is needed.

http://www.linuxselfhelp.com/howtos/Bash-Prompt/Bash-Prompt-HOWTO-6.html

http://bluesock.org/~willg/dev/ansi.html shows even more escape sequences; useful to get the big picture

+2  A: 
"\033[0m"

See here: http://en.wikipedia.org/wiki/ANSI_color

adamk
+8  A: 

Try using:

#define RESETCOLOR "\033[0m"

That should reset it to the defaults.

More about these terminal codes can be found here: http://en.wikipedia.org/wiki/ANSI_escape_code

Frxstrem
A: 

type reset

There is a binary found in Linux and OSX called reset.

Nils
Ok not the exact answer, but maybe looking at it helps.
Nils
"reset" wipes out the terminal window and places the prompt at the top of the screen, which is not the effect I need, but thank you.
jasper77