Is there a way to print colored output using iostream
and Xcode? I'd like to be able to, for example, print Hello World!
with Hello
red, World
blue and !
yellow. How can I do that?
-
1possible duplicate of (at least related) stackoverflow.com/questions/7414983/…– Johan LundbergFeb 6, 2012 at 9:40
-
Read this thread daniweb.com/software-development/cpp/threads/9921– Henk JansenFeb 6, 2012 at 9:40
-
See ascii-table.com/ansi-escape-sequences.php– Some programmer dudeFeb 6, 2012 at 9:47
-
@Anycorn what alternatives would you propose?– Mr ListerFeb 6, 2012 at 9:56
3 Answers
You need the terminal color codes. For linux it's the following (your system might be different, look it up):
//the following are UBUNTU/LINUX, and MacOS ONLY terminal color codes.
#define RESET "\033[0m"
#define BLACK "\033[30m" /* Black */
#define RED "\033[31m" /* Red */
#define GREEN "\033[32m" /* Green */
#define YELLOW "\033[33m" /* Yellow */
#define BLUE "\033[34m" /* Blue */
#define MAGENTA "\033[35m" /* Magenta */
#define CYAN "\033[36m" /* Cyan */
#define WHITE "\033[37m" /* White */
#define BOLDBLACK "\033[1m\033[30m" /* Bold Black */
#define BOLDRED "\033[1m\033[31m" /* Bold Red */
#define BOLDGREEN "\033[1m\033[32m" /* Bold Green */
#define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */
#define BOLDBLUE "\033[1m\033[34m" /* Bold Blue */
#define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */
#define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */
#define BOLDWHITE "\033[1m\033[37m" /* Bold White */
This allows you to do the following:
std::cout << RED << "hello world" << RESET << std::endl;
Note: If you don't use RESET the color will remain changed until the next time you use a color code.
-
1He's on Mac OS (at least I presume he is, since he mentions XCode) so this should work. Feb 6, 2012 at 9:54
-
16
-
@shuttle87, thanks for your reply. How can I set instead 3 variables with 3 different colors, such as
char hello = 'H'
,char world = 'W'
andchar ex = '!'
to be colored differently?– ShoeFeb 6, 2012 at 10:35 -
1you need to send the formatting code to terminal then the character then the reset code. So if you wanted to do this for an individual character you would do something like
cout << RED << hello << RESET;
. You could also write a few utility functions to make it easier if this is something you do regularly. Feb 6, 2012 at 10:41 -
2@Paul R do you know how to detect if a console supports ANSI escape codes, so that Xcode console doesn't print them?– elmatticJan 26, 2013 at 15:44
In a more c++ way for an ANSI capable terminal, it is possible to write your own ansi stream manipulators like std::endl but for handling ansi escape code.
Code for doing so can look like this for basic raw implementation:
namespace ansi {
template < class CharT, class Traits >
constexpr
std::basic_ostream< CharT, Traits > & reset( std::basic_ostream< CharT, Traits > &os )
{
return os << "\033[0m";
}
template < class CharT, class Traits >
constexpr
std::basic_ostream< CharT, Traits > & foreground_black( std::basic_ostream< CharT, Traits > &os )
{
return os << "\033[30m";
}
template < class CharT, class Traits >
constexpr
std::basic_ostream< CharT, Traits > & foreground_red( std::basic_ostream< CharT, Traits > &os )
{
return os << "\033[31m";
}
...
} // ansi
And it can be used in a code like this:
std::cout << ansi::foreground_red << "in red" << ansi::reset << std::endl;
-
2
-
Nice. Slightly simplified these by deducing return type with
constexpr auto&
– MHebesJan 11, 2022 at 18:29
Use {fmt} library, which is being slowly absorbed into C++ standard, starting with C++20 in <format>
header. Text colors and styles are not in the standard yet, AFAIK, but you can get them with the version from github, where you can find this example:
#include <fmt/color.h>
int main() {
fmt::print(fg(fmt::color::crimson) | fmt::emphasis::bold,
"Hello, {}!\n", "world");
fmt::print(fg(fmt::color::floral_white) | bg(fmt::color::slate_gray) |
fmt::emphasis::underline, "Hello, {}!\n", "мир");
fmt::print(fg(fmt::color::steel_blue) | fmt::emphasis::italic,
"Hello, {}!\n", "世界");
}