1 // (C) Copyright Gennadiy Rozental 2001.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
6 // See http://www.boost.org/libs/test for the library home page.
10 // Version : $Revision$
12 // Description : contains definition for setcolor iostream manipulator
13 // ***************************************************************************
15 #ifndef BOOST_TEST_UTILS_SETCOLOR_HPP
16 #define BOOST_TEST_UTILS_SETCOLOR_HPP
19 #include <boost/test/detail/config.hpp>
21 #include <boost/core/ignore_unused.hpp>
27 #include <boost/test/detail/suppress_warnings.hpp>
32 #if defined(__MINGW32__) && !defined(COMMON_LVB_UNDERSCORE)
33 // mingw badly mimicking windows.h
34 #define COMMON_LVB_UNDERSCORE 0x8000
38 //____________________________________________________________________________//
44 // ************************************************************************** //
45 // ************** term_attr ************** //
46 // ************************************************************************** //
48 struct term_attr { enum _ {
58 // ************************************************************************** //
59 // ************** term_color ************** //
60 // ************************************************************************** //
62 struct term_color { enum _ {
74 // ************************************************************************** //
75 // ************** setcolor ************** //
76 // ************************************************************************** //
82 explicit setcolor( bool is_color_output = false,
83 term_attr::_ attr = term_attr::NORMAL,
84 term_color::_ fg = term_color::ORIGINAL,
85 term_color::_ bg = term_color::ORIGINAL )
86 : m_is_color_output(is_color_output)
88 m_command_size = std::sprintf( m_control_command, "%c[%c;3%c;4%cm",
90 static_cast<char>(attr + '0'),
91 static_cast<char>(fg + '0'),
92 static_cast<char>(bg + '0'));
96 operator<<( std::ostream& os, setcolor const& sc )
98 if (sc.m_is_color_output && (&os == &std::cout || &os == &std::cerr)) {
99 return os.write( sc.m_control_command, sc.m_command_size );
106 bool m_is_color_output;
107 char m_control_command[13];
116 void set_console_color(std::ostream& os, WORD *attributes = NULL) const {
117 if (!m_is_color_output) {
121 if (&os == &std::cout) {
122 console_type = STD_OUTPUT_HANDLE;
124 else if (&os == &std::cerr) {
125 console_type = STD_ERROR_HANDLE;
130 HANDLE hConsole = GetStdHandle(console_type);
132 if(hConsole == INVALID_HANDLE_VALUE || hConsole == NULL )
135 if(attributes != NULL) {
136 SetConsoleTextAttribute(hConsole, *attributes);
140 CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
141 GetConsoleScreenBufferInfo(hConsole, &consoleInfo);
142 saved_attributes = consoleInfo.wAttributes;
147 case term_color::WHITE:
148 fg_attr = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
150 case term_color::BLACK:
153 case term_color::RED:
154 fg_attr = FOREGROUND_RED;
156 case term_color::GREEN:
157 fg_attr = FOREGROUND_GREEN;
159 case term_color::CYAN:
160 fg_attr = FOREGROUND_GREEN | FOREGROUND_BLUE;
162 case term_color::MAGENTA:
163 fg_attr = FOREGROUND_RED | FOREGROUND_BLUE;
165 case term_color::BLUE:
166 fg_attr = FOREGROUND_BLUE;
168 case term_color::YELLOW:
169 fg_attr = FOREGROUND_RED | FOREGROUND_GREEN;
171 case term_color::ORIGINAL:
173 fg_attr = saved_attributes & (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
180 case term_color::BLACK:
183 case term_color::WHITE:
184 bg_attr = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
186 case term_color::RED:
187 bg_attr = BACKGROUND_RED;
189 case term_color::GREEN:
190 bg_attr = BACKGROUND_GREEN;
192 case term_color::BLUE:
193 bg_attr = BACKGROUND_BLUE;
195 case term_color::ORIGINAL:
197 bg_attr = saved_attributes & (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
204 case term_attr::BRIGHT:
205 text_attr = FOREGROUND_INTENSITY;
207 case term_attr::UNDERLINE:
208 text_attr = COMMON_LVB_UNDERSCORE;
214 SetConsoleTextAttribute(hConsole, fg_attr | bg_attr | text_attr);
221 bool is_color_output = false,
222 term_attr::_ attr = term_attr::NORMAL,
223 term_color::_ fg = term_color::ORIGINAL,
224 term_color::_ bg = term_color::ORIGINAL )
225 : m_is_color_output(is_color_output)
232 operator<<( std::ostream& os, setcolor const& sc )
234 sc.set_console_color(os);
239 bool m_is_color_output;
246 mutable WORD saved_attributes;
250 // ************************************************************************** //
251 // ************** scope_setcolor ************** //
252 // ************************************************************************** //
256 struct scope_setcolor {
257 scope_setcolor() : m_os( 0 ) {}
258 explicit scope_setcolor( bool is_color_output,
260 term_attr::_ attr = term_attr::NORMAL,
261 term_color::_ fg = term_color::ORIGINAL,
262 term_color::_ bg = term_color::ORIGINAL )
264 , m_is_color_output( is_color_output )
266 os << setcolor( is_color_output, attr, fg, bg );
271 *m_os << setcolor( m_is_color_output );
274 scope_setcolor(const scope_setcolor& r);
275 scope_setcolor& operator=(const scope_setcolor& r);
278 bool m_is_color_output;
283 struct scope_setcolor : setcolor {
284 scope_setcolor() : m_os( 0 ) {}
285 explicit scope_setcolor(
286 bool is_color_output,
288 term_attr::_ attr = term_attr::NORMAL,
289 term_color::_ fg = term_color::ORIGINAL,
290 term_color::_ bg = term_color::ORIGINAL )
291 : setcolor(is_color_output, attr, fg, bg)
300 set_console_color(*m_os, &this->saved_attributes);
304 scope_setcolor(const scope_setcolor& r);
305 scope_setcolor& operator=(const scope_setcolor& r);
313 #define BOOST_TEST_SCOPE_SETCOLOR( is_color_output, os, attr, color ) \
314 utils::scope_setcolor const sc(is_color_output, os, utils::attr, utils::color); \
315 boost::ignore_unused( sc ) \
319 } // namespace unit_test
322 #include <boost/test/detail/enable_warnings.hpp>
324 #endif // BOOST_TEST_UTILS_SETCOLOR_HPP