Linux Console Color (the "\033[" Way) - Forum - C++

Có thể bạn quan tâm

cplusplus.com
  • TUTORIALS
  • REFERENCE
  • ARTICLES
  • FORUM

C++

  • Tutorials
  • Reference
  • Articles
  • Forum

Forum

  • Beginners
  • Windows Programming
  • UNIX/Linux Programming
  • General C++ Programming
  • Lounge
  • Jobs
  • Forum
  • UNIX/Linux Programming
  • Linux Console Color (the "\033[" way)

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

ToRcH (1) 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:
1234567891011121314151617181920212223 #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. kbw (9488) http://ascii-table.com/ansi-escape-sequences.php http://ascii-table.com/ansi-escape-sequences-vt-100.php Topic archived. No new replies allowed. Home page | Privacy policy© cplusplus.com, 2000-2025 - All rights reserved - v3.3.3Spotted an error? contact us

Từ khóa » C++ 033 0m