CppCommon  1.0.4.1
C++ Common Library
console.cpp
Go to the documentation of this file.
1 
9 #include "system/console.h"
10 
11 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
12 #include <cstdio>
13 #elif defined(_WIN32) || defined(_WIN64)
14 #include <windows.h>
15 #endif
16 
17 namespace CppCommon {
18 
19 void Console::SetColor(Color color, Color background)
20 {
21 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
22  const char* colors[] =
23  {
24  "\033[22;30m", // Black color
25  "\033[22;34m", // Blue color
26  "\033[22;32m", // Green color
27  "\033[22;36m", // Cyan color
28  "\033[22;31m", // Red color
29  "\033[22;35m", // Magenta color
30  "\033[22;33m", // Brown color
31  "\033[22;37m", // Grey color
32  "\033[01;30m", // Dark grey color
33  "\033[01;34m", // Light blue color
34  "\033[01;32m", // Light green color
35  "\033[01;36m", // Light cyan color
36  "\033[01;31m", // Light red color
37  "\033[01;35m", // Light magenta color
38  "\033[01;33m", // Yellow color
39  "\033[01;37m" // White color
40  };
41  const char* backgrounds[] =
42  {
43  "\033[00000m", // Black color
44  "\033[02;44m", // Blue color
45  "\033[02;42m", // Green color
46  "\033[02;46m", // Cyan color
47  "\033[02;41m", // Red color
48  "\033[02;45m", // Magenta color
49  "\033[02;43m", // Brown color
50  "\033[02;47m", // Grey color
51  "\033[00;40m", // Dark grey color
52  "\033[00;44m", // Light blue color
53  "\033[00;42m", // Light green color
54  "\033[00;46m", // Light cyan color
55  "\033[00;41m", // Light red color
56  "\033[00;45m", // Light magenta color
57  "\033[00;43m", // Yellow color
58  "\033[00;47m" // White color
59  };
60  std::fwrite(backgrounds[(int)background - (int)Color::BLACK], 1, 8, stdout);
61  std::fwrite(colors[(int)color - (int)Color::BLACK], 1, 8, stdout);
62 #elif defined(_WIN32) || defined(_WIN64)
63  HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
64  SetConsoleTextAttribute(hConsole, (((WORD)color) & 0x0F) + ((((WORD)background) & 0x0F) << 4));
65 #endif
66 }
67 
68 } // namespace CppCommon
static void SetColor(Color color, Color background=Color::BLACK)
Set console text color.
Definition: console.cpp:19
Console management definition.
C++ Common project definitions.
Definition: token_bucket.h:15
Color
Supported console colors.
Definition: console.h:18
@ BLACK
Black color.