Linux Console Color (the "\033[" way)

Hi all.

I just wanted to help people in this matter since I too went through this and had some problems with good answers related to this problem.

(and I also wanted to help Duoas for I've seen him struggling to tell people to stop using "system" calls. Good Job Duoas)

This is a list of codes used in C++ to change the text color:

black - 30
red - 31
green - 32
brown - 33
blue - 34
magenta - 35
cyan - 36
lightgray - 37

Now these are the basic colours.

Usage of "\033[":
This is to handle the console cursor. I do not have a complete reference so I ask people who know about it to comment with what I do not have.

* 'm' character at the end of each of the following sentences is used as a stop character, where the system should stop and parse the \033[ sintax.

\033[0m - is the default color for the console
\033[0;#m - is the color of the text, where # is one of the codes mentioned above
\033[1m - makes text bold
\033[1;#m - makes colored text bold**
\033[2;#m - colors text according to # but a bit darker
\033[4;#m - colors text in # and underlines
\033[7;#m - colors the background according to #
\033[9;#m - colors text and strikes it
\033[A - moves cursor one line above (carfull: it does not erase the previously written line)
\033[B - moves cursor one line under
\033[C - moves cursor one spacing to the right
\033[D - moves cursor one spacing to the left
\033[E - don't know yet
\033[F - don't know yet

\033[2K - erases everything written on line before this.

**(this could be usefull for those who want more colors. The efect given by bold will give a color change effect)

As a short and simple example that colors text and the bold version:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

int main(int argc, char ** argv)
{
    std::string default_console = "\033["+itoa(0)+"m";
    for (int i = 30; i <= 37; i++)
    {
        std::string color = "\033[0;"+itoa(i)+"m";

        std::cout<<color<<"test "<<i<<std::endl;

        color = "\033[1"+itoa(i)+"m";

        std::cout<<color<<"test "<<i<<" bold"<<std::endl;
        std::cout<<default_console<<std::endl;

    }

    std::cout << "Done" << std::endl;

    return 0;
}


Hope this is usefull,
ToRcH

P.S.: If anyone knows other special characters please comment and add them. TY.
Topic archived. No new replies allowed.