# Console program with color output?



## neilms (Jan 31, 2013)

Hi,
I am testing a console program written in C++. I want to adjust the output so that instead of the standard white text, I can make certain words appear in different colors. So for example, the following string will output each word in (for example) the following colours:



> This is a test



This - appears as blue text
is - appears as red text
a - appears as green text
test - appears as yellow text.

Can anyone tell me what header files would I need to include doing this under the console? Alternatively, could you point me to a program that might have some useful code that achieves a similar effect? Note, I am not using X windows at all for this project.


----------



## wblock@ (Feb 1, 2013)

These are ANSI color codes.  There are programs in ports that use them.  textproc/codespell, for example.


----------



## fonz (Feb 1, 2013)

neilms said:
			
		

> I want to adjust the output so that instead of the standard white text, I can make certain words appear in different colors.


There's also devel/ncurses. Although for the sole purpose of colouring the output it's probably a bit too much (try the ANSI codes first), it's a useful library that you may want to use later for e.g. menus, TUIs and that sort of thing.


----------



## expl (Feb 1, 2013)

Keep in mind that if you want your output to be pipe friendly you have to detect if your program is not running on a TTY and disable any special characters that TTY would normally escape.


----------



## fonz (Feb 1, 2013)

expl said:
			
		

> Keep in mind that if you want your output to be pipe friendly you have to detect if your program is not running on a TTY and disable any special characters that TTY would normally escape.


Good point. You (=OP) may want to have a look at the source of e.g. ls(1) (-G option) to see how this is done.


----------



## wblock@ (Feb 1, 2013)

expl said:
			
		

> Keep in mind that if you want your output to be pipe friendly you have to detect if your program is not running on a TTY and disable any special characters that TTY would normally escape.



Or make the ANSI color output an option.  I did that in textproc/igor.  less(1) will show ANSI color codes if given the -R option.


----------



## neilms (Feb 4, 2013)

wblock@ said:
			
		

> Or make the ANSI color output an option.  I did that in textproc/igor.  less(1) will show ANSI color codes if given the -R option.



Can you show the function / code that:
1 - parse the command line argument &
2. -how it links to the ansi code?


----------



## wblock@ (Feb 4, 2013)

neilms said:
			
		

> Can you show the function / code that:
> 1 - parse the command line argument &



That depends on the language.  igor is in Perl.



> 2. -how it links to the ansi code?



Look up the ANSI color sequence to use, print them out as characters.


```
% printf "\033[1;31mThis is red \033[1;34mand this is blue.\033[0;24;27m\n"
[color="Red"]This is red [/color][color="Blue"]and this is blue.[/color]
```


----------



## neilms (Feb 6, 2013)

It's ok - I have a C++ solution that I found. It is quite simple really:

```
#include <iostream>
    
     using namespace std;
     
     int main (int argc, char const *argv[]) {
       const char COL_RESET[] = "\x1b[0m";
       
       // Foreground colors are in form of 3x, bacground are 4x
      const char RED[]     = "\x1b[31m";
      const char GREEN[]   = "\x1b[32m";
      const char YELLOW[]  = "\x1b[33m";
      const char BLUE[]    = "\x1b[34m";
      const char MAGENTA[] = "\x1b[35m";
      const char CYAN[]    = "\x1b[36m";
    
      cout << RED     << "Red looks good" << endl;
      cout << GREEN   << "Green looks good" << endl;
      cout << YELLOW  << "Yellow looks good" << endl;
      cout << BLUE    << "Blue looks good" << endl;
      cout << MAGENTA << "Magenta looks good" << endl;
      cout << CYAN    << "Cyan looks good" << COL_RESET << endl;
      cout << "This text should have the normal color" << endl;
      
      // American flag, ASCII style.
      cout << "\x1b[44m" << "*********" 
           << "\x1b[41m" << "=================" << endl;
      cout << "\x1b[44m" << "*********" 
           << "\x1b[41m" << "=================" << endl;
      cout << "\x1b[44m" << "*********" 
           << "\x1b[41m" << "=================" << endl;
      cout << "\x1b[44m" << "*********" 
           << "\x1b[41m" << "=================" << endl;
      cout << "==========================" << endl;
      cout << "==========================" << endl;
      cout << "==========================" << COL_RESET << endl;
  
      return 0;
    }
```

As for the command line arguments - boost library offers ready made 'program_options' functions though C users might use gnu getopt.


----------



## fonz (Feb 6, 2013)

neilms said:
			
		

> As for the command line arguments - boost library offers ready made 'program_options' functions though C users might use gnu getopt.


They might not necessarily be mutually exclusive. For example, getopt(3) can also be used _after_ certain x11-toolkits/libXt functions in order to first process X-specific options (e.g. -geometry) and then the rest. It wouldn't surprise me if boost options and getopt(3) can be combined too (when done right).


----------

