std::cout << "blblabla... [done]" << std::endl;
Is it possible to make [done]
be in another color, and possibly bold? I'm using Windows 7
In C++ programming, the default background of the output screen is black and the text color is the white color, the task is to color both the background and text color in the output screen. console_color = GetStdHandle(STD_OUTPUT_HANDLE); // P is color code according to your need.
C++ On windows you can use SetConsoleTextAttribute from the windows header.
Sets the attributes of characters written to the console screen buffer by the WriteFile or WriteConsole function, or echoed by the ReadFile or ReadConsole function. This function affects text written after the function call.
This depends on which OS you are using.
If you're using windows you want SetConsoleTextAttribute:
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // Get handle to standard output
SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE);
You can also combine values.
An application can combine the foreground and background constants to achieve different colors. For example, the following combination results in bright cyan text on a blue background.
FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY | BACKGROUND_BLUE
You can then use WriteFile or WriteConsole to actually write the the console.
Yes, you just send a standard escape sequence, e.g.
const char* green = "\033[0;32m";
const char* white = "\033[0;37m";
const char* red = "\033[0;31m";
double profit = round(someComplicatedThing());
std::cout << (profit < 0 ? red : (profit > 0 ? green : white))
<< "Profit is " << profit << white << std::endl;
You also get bold vs normal, colored background etc. The Wikipedia page on ANSI escape code has details, the Bash-Prompt HOWTO has examples.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With