8

I'm trying to change the output of an console application, but I'm finding just Windows versions of the code, which of course don't work in OS X.

The code should look like following:

printf("some text"); //this should be in green

I know, this is only a printf() execution, but how can I convert the output color?

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Dominic Järmann
  • 361
  • 1
  • 7
  • 18
  • Take a look at the answers here: http://stackoverflow.com/questions/14535050/basic-terminal-output-using-c-questions – Paul Roub Oct 23 '15 at 18:40
  • OS X doesn't have a console like Linux, do you mean Terminal? – Barmar Oct 23 '15 at 18:48
  • Is it a full-screen program? Then you should use the `ncurses` library. – Barmar Oct 23 '15 at 18:49
  • 1
    ANSI escape codes work perfectly with OS X's Terminal, for at least the basic 8 colors and the attributes Bold, Inverse, and Underlined. – Jongware Oct 23 '15 at 19:08
  • For better portability, OP should use something like the termcap/terminfo interface, rather than hard-coding escape sequences. OP's application is not full-screen (if it were, hard-coding would be even less useful). – Thomas Dickey Oct 24 '15 at 13:51

1 Answers1

17

The Terminal application on Mac OS X responds to the standard ANSI escape codes for colors when they are printed using, for example, printf() in C/C++ or std::cout << in C++.

The strings that can be used to set foreground and/or background color look like this:

To set only the foreground color:

 "\x1b[30m"

To set only the background color:

 "\x1b[40m"

To set foreground and background color:

 "\x1b[30;40m"

To reset all color attributes back to normal:

 "\x1b[0m"

In the above strings the numbers 30 and 40 are just placeholders for foreground and background color codes, they can be replaced by the numbers from the table below to get one of 8 standard colors:

+---------+------------+------------+
|  color  | foreground | background |
|         |    code    |    code    |
+---------+------------+------------+
| black   |     30     |     40     |
| red     |     31     |     41     |
| green   |     32     |     42     |
| yellow  |     33     |     43     |
| blue    |     34     |     44     |
| magenta |     35     |     45     |
| cyan    |     36     |     46     |
| white   |     37     |     47     |
+---------+------------+------------+

Here is an example:

printf("\x1b[32m green text on regular background \x1b[0m  \n");
printf("\x1b[32;40m green text on black background \x1b[0m  \n");
printf("\x1b[42m regular text on green background \x1b[0m  \n");

It looks like this on a Mac OS X El Capitan (in a Terminal window which by default has black text on a white background).

enter image description here

WhiteViking
  • 3,146
  • 1
  • 19
  • 22
  • 3
    Just an addendum: if I want to be able to redirect my output into a file, I first check if `isatty()` is true, and if not, I don't output these color codes so the output file is clean. `isatty()` can be mentally stored as "is a TTY", i.e., a teletype terminal. – Jongware Oct 23 '15 at 21:45