1 | #ifndef BANDIT_REPORTERS_COLORIZER_H |
2 | #define BANDIT_REPORTERS_COLORIZER_H |
3 | |
4 | #include <string> |
5 | |
6 | #if defined(_WIN32) && !defined(BANDIT_CONFIG_COLOR_ANSI) |
7 | # ifndef NOMINMAX |
8 | # define NOMINMAX |
9 | # endif |
10 | # define WIN32_LEAN_AND_MEAN |
11 | # include <windows.h> |
12 | #endif |
13 | |
14 | namespace bandit { |
15 | namespace detail { |
16 | #if defined(_WIN32) && !defined(BANDIT_CONFIG_COLOR_ANSI) |
17 | struct colorizer { |
18 | colorizer(bool colors_enabled = true) |
19 | : colors_enabled_(colors_enabled), |
20 | stdout_handle_(GetStdHandle(STD_OUTPUT_HANDLE)) { |
21 | background_color_ = original_color_ = get_console_color(); |
22 | background_color_ &= BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY; |
23 | } |
24 | |
25 | const std::string green() const { |
26 | if (colors_enabled_) { |
27 | set_console_color(FOREGROUND_GREEN | FOREGROUND_INTENSITY | background_color_); |
28 | } |
29 | return "" ; |
30 | } |
31 | |
32 | const std::string yellow() const { |
33 | if (colors_enabled_) { |
34 | set_console_color(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY | background_color_); |
35 | } |
36 | return "" ; |
37 | } |
38 | |
39 | const std::string blue() const { |
40 | if (colors_enabled_) { |
41 | set_console_color(FOREGROUND_BLUE | FOREGROUND_INTENSITY | background_color_); |
42 | } |
43 | return "" ; |
44 | } |
45 | |
46 | const std::string red() const { |
47 | if (colors_enabled_) { |
48 | set_console_color(FOREGROUND_RED | FOREGROUND_INTENSITY | background_color_); |
49 | } |
50 | return "" ; |
51 | } |
52 | |
53 | const std::string white() const { |
54 | if (colors_enabled_) { |
55 | set_console_color(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | |
56 | FOREGROUND_INTENSITY | background_color_); |
57 | } |
58 | return "" ; |
59 | } |
60 | |
61 | const std::string reset() const { |
62 | if (colors_enabled_) { |
63 | set_console_color(original_color_); |
64 | } |
65 | return "" ; |
66 | } |
67 | |
68 | private: |
69 | WORD get_console_color() const { |
70 | CONSOLE_SCREEN_BUFFER_INFO info{}; |
71 | GetConsoleScreenBufferInfo(stdout_handle_, &info); |
72 | return info.wAttributes; |
73 | } |
74 | |
75 | void set_console_color(WORD color) const { |
76 | SetConsoleTextAttribute(stdout_handle_, color); |
77 | } |
78 | |
79 | private: |
80 | bool colors_enabled_; |
81 | HANDLE stdout_handle_; |
82 | WORD original_color_; |
83 | WORD background_color_; |
84 | }; |
85 | #else |
86 | struct colorizer { |
87 | colorizer(bool colors_enabled = true) : colors_enabled_(colors_enabled) {} |
88 | |
89 | const std::string green() const { |
90 | return colors_enabled_ ? "\033[1;32m" : "" ; |
91 | } |
92 | |
93 | const std::string yellow() const { |
94 | return colors_enabled_ ? "\033[1;33m" : "" ; |
95 | } |
96 | |
97 | const std::string blue() const { |
98 | return colors_enabled_ ? "\033[1;34m" : "" ; |
99 | } |
100 | |
101 | const std::string red() const { |
102 | return colors_enabled_ ? "\033[1;31m" : "" ; |
103 | } |
104 | |
105 | const std::string white() const { |
106 | return colors_enabled_ ? "\033[1;37m" : "" ; |
107 | } |
108 | |
109 | const std::string reset() const { |
110 | return colors_enabled_ ? "\033[0m" : "" ; |
111 | } |
112 | |
113 | private: |
114 | bool colors_enabled_; |
115 | }; |
116 | #endif |
117 | } |
118 | } |
119 | #endif |
120 | |