You can use the following header and its defined escape sequence codes to print out coloured text on your terminal.

ansi.hpp

   1 #ifndef ANSI_HPP
   2 #define ANSI_HPP
   3 
   4 // ansi escape sequences codes
   5 
   6 #define NORM  "\033[00;00m" // Default Terminal Colour
   7 
   8 #define GRAY  "\033[00;37m" // Gray
   9 #define RED   "\033[00;31m" // Red
  10 #define GREEN "\033[00;32m" // Green
  11 #define YELL  "\033[00;33m" // Yellow
  12 #define BLUE  "\033[00;34m" // Blue
  13 #define MAGNE "\033[00;35m" // Magenta
  14 #define CYAN  "\033[00;36m" // Cyan
  15 #define WHITE "\033[01;37m" // White
  16 #define BLACK "\033[00;30m" // Black
  17 
  18 #define B_RED   "\033[01;31m" // Bright Red
  19 #define B_GREEN "\033[01;32m" // Bright Green
  20 #define B_YELL  "\033[01;33m" // Bright Yellow
  21 #define B_BLUE  "\033[01;34m" // Bright Blue
  22 #define B_MAGNE "\033[01;35m" // Bright Magenta
  23 #define B_CYAN  "\033[01;36m" // Bright Cyan
  24 
  25 // other types
  26 #define RETURN "\033[0;0H" // returns the cursor to 0,0 of the terminal
  27 #define CLEAR "\033[2J" // clears the screen
  28 
  29 #endif

You can test if your terminal supports them by running the following program (and including the header)

test.cpp

   1 #include <iostream>
   2 #include <ostream>
   3 
   4 #include "ansi.hpp"
   5 
   6 int main()
   7 {
   8   using std::cout;
   9   using std::endl;
  10 
  11   cout << CLEAR;
  12   cout << RED << "RED" << endl;
  13   cout << GRAY << "GRAY" << endl;
  14   cout << GREEN << "GREEN" << endl;
  15   cout << YELL << "YELLOW" << endl;
  16   cout << BLUE << "BLUE" << endl;
  17   cout << MAGNE << "MAGNE" << endl;
  18   cout << CYAN << "CYAN" << endl;
  19   cout << WHITE << "WHITE" << endl;
  20   cout << NORM << "NORM" << endl;
  21 
  22   cout << B_RED << "B RED" << endl;
  23   cout << GRAY << "GRAY" << endl;
  24   cout << B_GREEN << "B GREEN" << endl;
  25   cout << B_YELL << "B YELLOW" << endl;
  26   cout << B_BLUE << "B BLUE" << endl;
  27   cout << B_MAGNE << "B MAGNE" << endl;
  28   cout << B_CYAN << "B CYAN" << endl;
  29   cout << WHITE << "WHITE" << endl;
  30   cout << NORM << "NORM" << endl;
  31 
  32   cout << BLACK << "BLACK" << endl;
  33 
  34   return 0;
  35 }

Ansi_codes (last edited 2005-10-01 22:21:51 by rubicant)