5

What is the scope of ANSI color codes. In following code everything is coming in blue. I only want "Compiled Successfully" in blue. Is there something like closing tags (eg. in HTML) here?

cout<<"\n\e[0;34mCompiled Successfully!!\n";
sym_table.render();
cout<<"\n\nAll variables according to sizes:\n";
for(auto el: addr_table){
    cout<<el.first<<"     "<<el.second<<endl;
}
Abhishek Kumar
  • 729
  • 6
  • 20

1 Answers1

8

You need to add reset at the end of your string

# Reset
Color_Off='\033[0m'       # Text Reset

# Regular Colors
Black='\033[0;30m'        # Black
Red='\033[0;31m'          # Red
Green='\033[0;32m'        # Green
Yellow='\033[0;33m'       # Yellow
Blue='\033[0;34m'         # Blue
Purple='\033[0;35m'       # Purple
Cyan='\033[0;36m'         # Cyan
White='\033[0;37m'        # White   

the:

cout<<"\n\e[0;34mCompiled Successfully!!\n";

should be:

cout  << "\n\e[0;34mCompiled Successfully!!\e[m\n";   

or better:

\033[0;34mCompiled Successfully!!\033[m  

For more details:

How to change the output color of echo in Linux


NOTE that in C++:
You can:

#define blue "\033[0;34"
#define reset "\033[m"
...
...
...
std::cout << blue << "your-string" << reset << '\n';
Community
  • 1
  • 1
Shakiba Moshiri
  • 21,040
  • 2
  • 34
  • 44