12 #ifndef ecl_console_TERMCOLOR_HPP_ 13 #define ecl_console_TERMCOLOR_HPP_ 18 #if defined(_WIN32) || defined(_WIN64) 20 #elif defined(__APPLE__) 22 #elif defined(linux) || defined(__linux) 25 # error "unsupported platform" 33 #if defined(OS_MACOS) || defined(OS_LINUX) 35 #elif defined(OS_WINDOWS) 49 inline FILE* get_standard_stream(
const std::ostream& stream);
50 inline bool is_atty(
const std::ostream& stream);
52 #if defined(OS_WINDOWS) 53 void win_change_attributes(std::ostream& stream,
int foreground,
int background=-1);
59 std::ostream& reset(std::ostream& stream)
61 if (console::is_atty(stream))
63 #if defined(OS_MACOS) || defined(OS_LINUX) 65 #elif defined(OS_WINDOWS) 66 console::win_change_attributes(stream, -1, -1);
74 std::ostream& bold(std::ostream& stream)
76 if (console::is_atty(stream))
78 #if defined(OS_MACOS) || defined(OS_LINUX) 80 #elif defined(OS_WINDOWS) 88 std::ostream& dark(std::ostream& stream)
90 if (console::is_atty(stream))
92 #if defined(OS_MACOS) || defined(OS_LINUX) 94 #elif defined(OS_WINDOWS) 102 std::ostream& underline(std::ostream& stream)
104 if (console::is_atty(stream))
106 #if defined(OS_MACOS) || defined(OS_LINUX) 108 #elif defined(OS_WINDOWS) 116 std::ostream& blink(std::ostream& stream)
118 if (console::is_atty(stream))
120 #if defined(OS_MACOS) || defined(OS_LINUX) 122 #elif defined(OS_WINDOWS) 130 std::ostream& reverse(std::ostream& stream)
132 if (console::is_atty(stream))
134 #if defined(OS_MACOS) || defined(OS_LINUX) 136 #elif defined(OS_WINDOWS) 144 std::ostream& concealed(std::ostream& stream)
146 if (console::is_atty(stream))
148 #if defined(OS_MACOS) || defined(OS_LINUX) 150 #elif defined(OS_WINDOWS) 158 std::ostream& grey(std::ostream& stream)
160 if (console::is_atty(stream))
162 #if defined(OS_MACOS) || defined(OS_LINUX) 163 stream <<
"\033[30m";
164 #elif defined(OS_WINDOWS) 165 console::win_change_attributes(stream,
174 std::ostream& red(std::ostream& stream)
176 if (console::is_atty(stream))
178 #if defined(OS_MACOS) || defined(OS_LINUX) 179 stream <<
"\033[31m";
180 #elif defined(OS_WINDOWS) 181 console::win_change_attributes(stream,
190 std::ostream& green(std::ostream& stream)
192 if (console::is_atty(stream))
194 #if defined(OS_MACOS) || defined(OS_LINUX) 195 stream <<
"\033[32m";
196 #elif defined(OS_WINDOWS) 197 console::win_change_attributes(stream,
206 std::ostream& yellow(std::ostream& stream)
208 if (console::is_atty(stream))
210 #if defined(OS_MACOS) || defined(OS_LINUX) 211 stream <<
"\033[33m";
212 #elif defined(OS_WINDOWS) 213 console::win_change_attributes(stream,
214 FOREGROUND_GREEN | FOREGROUND_RED
222 std::ostream& blue(std::ostream& stream)
224 if (console::is_atty(stream))
226 #if defined(OS_MACOS) || defined(OS_LINUX) 227 stream <<
"\033[34m";
228 #elif defined(OS_WINDOWS) 229 console::win_change_attributes(stream,
238 std::ostream& magenta(std::ostream& stream)
240 if (console::is_atty(stream))
242 #if defined(OS_MACOS) || defined(OS_LINUX) 243 stream <<
"\033[35m";
244 #elif defined(OS_WINDOWS) 245 console::win_change_attributes(stream,
246 FOREGROUND_BLUE | FOREGROUND_RED
254 std::ostream& cyan(std::ostream& stream)
256 if (console::is_atty(stream))
258 #if defined(OS_MACOS) || defined(OS_LINUX) 259 stream <<
"\033[36m";
260 #elif defined(OS_WINDOWS) 261 console::win_change_attributes(stream,
262 FOREGROUND_BLUE | FOREGROUND_GREEN
270 std::ostream& white(std::ostream& stream)
272 if (console::is_atty(stream))
274 #if defined(OS_MACOS) || defined(OS_LINUX) 275 stream <<
"\033[37m";
276 #elif defined(OS_WINDOWS) 277 console::win_change_attributes(stream,
278 FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED
288 std::ostream& on_grey(std::ostream& stream)
290 if (console::is_atty(stream))
292 #if defined(OS_MACOS) || defined(OS_LINUX) 293 stream <<
"\033[40m";
294 #elif defined(OS_WINDOWS) 295 console::win_change_attributes(stream, -1,
304 std::ostream& on_red(std::ostream& stream)
306 if (console::is_atty(stream))
308 #if defined(OS_MACOS) || defined(OS_LINUX) 309 stream <<
"\033[41m";
310 #elif defined(OS_WINDOWS) 311 console::win_change_attributes(stream, -1,
320 std::ostream& on_green(std::ostream& stream)
322 if (console::is_atty(stream))
324 #if defined(OS_MACOS) || defined(OS_LINUX) 325 stream <<
"\033[42m";
326 #elif defined(OS_WINDOWS) 327 console::win_change_attributes(stream, -1,
336 std::ostream& on_yellow(std::ostream& stream)
338 if (console::is_atty(stream))
340 #if defined(OS_MACOS) || defined(OS_LINUX) 341 stream <<
"\033[43m";
342 #elif defined(OS_WINDOWS) 343 console::win_change_attributes(stream, -1,
344 BACKGROUND_GREEN | BACKGROUND_RED
352 std::ostream& on_blue(std::ostream& stream)
354 if (console::is_atty(stream))
356 #if defined(OS_MACOS) || defined(OS_LINUX) 357 stream <<
"\033[44m";
358 #elif defined(OS_WINDOWS) 359 console::win_change_attributes(stream, -1,
368 std::ostream& on_magenta(std::ostream& stream)
370 if (console::is_atty(stream))
372 #if defined(OS_MACOS) || defined(OS_LINUX) 373 stream <<
"\033[45m";
374 #elif defined(OS_WINDOWS) 375 console::win_change_attributes(stream, -1,
376 BACKGROUND_BLUE | BACKGROUND_RED
384 std::ostream& on_cyan(std::ostream& stream)
386 if (console::is_atty(stream))
388 #if defined(OS_MACOS) || defined(OS_LINUX) 389 stream <<
"\033[46m";
390 #elif defined(OS_WINDOWS) 391 console::win_change_attributes(stream, -1,
392 BACKGROUND_GREEN | BACKGROUND_BLUE
400 std::ostream& on_white(std::ostream& stream)
402 if (console::is_atty(stream))
404 #if defined(OS_MACOS) || defined(OS_LINUX) 405 stream <<
"\033[47m";
406 #elif defined(OS_WINDOWS) 407 console::win_change_attributes(stream, -1,
408 BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_RED
428 FILE* get_standard_stream(
const std::ostream& stream)
430 if (&stream == &std::cout)
432 else if (&stream == &std::cerr)
441 bool is_atty(
const std::ostream& stream)
447 FILE* std_stream = get_standard_stream(stream);
450 #if defined(OS_MACOS) || defined(OS_LINUX) 451 return ::isatty(fileno(std_stream));
452 #elif defined(OS_WINDOWS) 453 return ::_isatty(_fileno(std_stream));
458 #if defined(OS_WINDOWS) 459 void win_change_attributes(std::ostream& stream,
int foreground,
int background)
464 static WORD defaultAttributes = 0;
467 HANDLE hTerminal = INVALID_HANDLE_VALUE;
468 if (&stream == &std::cout)
469 hTerminal = GetStdHandle(STD_OUTPUT_HANDLE);
470 else if (&stream == &std::cerr)
471 hTerminal = GetStdHandle(STD_ERROR_HANDLE);
474 if (!defaultAttributes)
476 CONSOLE_SCREEN_BUFFER_INFO info;
477 if (!GetConsoleScreenBufferInfo(hTerminal, &info))
479 defaultAttributes = info.wAttributes;
483 if (foreground == -1 && background == -1)
485 SetConsoleTextAttribute(hTerminal, defaultAttributes);
490 CONSOLE_SCREEN_BUFFER_INFO info;
491 if (!GetConsoleScreenBufferInfo(hTerminal, &info))
494 if (foreground != -1)
496 info.wAttributes &= ~(info.wAttributes & 0x0F);
497 info.wAttributes |=
static_cast<WORD
>(foreground);
500 if (background != -1)
502 info.wAttributes &= ~(info.wAttributes & 0xF0);
503 info.wAttributes |=
static_cast<WORD
>(background);
506 SetConsoleTextAttribute(hTerminal, info.wAttributes);
519 #endif // ecl_console_TERMCOLOR_HPP_