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>
25 #include <boost/test/detail/suppress_warnings.hpp>
30 #if defined(__MINGW32__) && !defined(COMMON_LVB_UNDERSCORE)
31 // mingw badly mimicking windows.h
32 #define COMMON_LVB_UNDERSCORE 0x8000
36 //____________________________________________________________________________//
42 // ************************************************************************** //
43 // ************** term_attr ************** //
44 // ************************************************************************** //
46 struct term_attr { enum _ {
56 // ************************************************************************** //
57 // ************** term_color ************** //
58 // ************************************************************************** //
60 struct term_color { enum _ {
72 // ************************************************************************** //
73 // ************** setcolor ************** //
74 // ************************************************************************** //
80 explicit setcolor( term_attr::_ attr = term_attr::NORMAL,
81 term_color::_ fg = term_color::ORIGINAL,
82 term_color::_ bg = term_color::ORIGINAL )
84 m_command_size = std::sprintf( m_control_command, "%c[%d;%d;%dm", 0x1B, attr, fg + 30, bg + 40 );
88 operator<<( std::ostream& os, setcolor const& sc )
90 if (&os == &std::cout || &os == &std::cerr) {
91 return os.write( sc.m_control_command, sc.m_command_size );
98 char m_control_command[13];
107 void set_console_color(std::ostream& os, WORD *attributes = NULL) const {
109 if (&os == &std::cout) {
110 console_type = STD_OUTPUT_HANDLE;
112 else if (&os == &std::cerr) {
113 console_type = STD_ERROR_HANDLE;
118 HANDLE hConsole = GetStdHandle(console_type);
120 if(hConsole == INVALID_HANDLE_VALUE || hConsole == NULL )
123 if(attributes != NULL) {
124 SetConsoleTextAttribute(hConsole, *attributes);
128 CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
129 GetConsoleScreenBufferInfo(hConsole, &consoleInfo);
130 //if(!has_written_console_ext) {
131 saved_attributes = consoleInfo.wAttributes;
137 case term_color::WHITE:
138 fg_attr = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
140 case term_color::BLACK:
143 case term_color::RED:
144 fg_attr = FOREGROUND_RED;
146 case term_color::GREEN:
147 fg_attr = FOREGROUND_GREEN;
149 case term_color::CYAN:
150 fg_attr = FOREGROUND_GREEN | FOREGROUND_BLUE;
152 case term_color::MAGENTA:
153 fg_attr = FOREGROUND_RED | FOREGROUND_BLUE;
155 case term_color::BLUE:
156 fg_attr = FOREGROUND_BLUE;
158 case term_color::YELLOW:
159 fg_attr = FOREGROUND_RED | FOREGROUND_GREEN;
161 case term_color::ORIGINAL:
163 fg_attr = saved_attributes & (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
170 case term_color::BLACK:
173 case term_color::WHITE:
174 bg_attr = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
176 case term_color::RED:
177 bg_attr = BACKGROUND_RED;
179 case term_color::GREEN:
180 bg_attr = BACKGROUND_GREEN;
182 case term_color::BLUE:
183 bg_attr = BACKGROUND_BLUE;
185 case term_color::ORIGINAL:
187 bg_attr = saved_attributes & (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
194 case term_attr::BRIGHT:
195 text_attr = FOREGROUND_INTENSITY;
197 case term_attr::UNDERLINE:
198 text_attr = COMMON_LVB_UNDERSCORE;
204 SetConsoleTextAttribute(hConsole, fg_attr | bg_attr | text_attr);
206 //has_written_console_ext = true;
213 term_attr::_ attr = term_attr::NORMAL,
214 term_color::_ fg = term_color::ORIGINAL,
215 term_color::_ bg = term_color::ORIGINAL )
216 : /*has_written_console_ext(false)
223 operator<<( std::ostream& os, setcolor const& sc )
225 sc.set_console_color(os);
236 mutable WORD saved_attributes;
237 //mutable bool has_written_console_ext;
241 // ************************************************************************** //
242 // ************** scope_setcolor ************** //
243 // ************************************************************************** //
247 struct scope_setcolor {
248 scope_setcolor() : m_os( 0 ) {}
249 explicit scope_setcolor( std::ostream& os,
250 term_attr::_ attr = term_attr::NORMAL,
251 term_color::_ fg = term_color::ORIGINAL,
252 term_color::_ bg = term_color::ORIGINAL )
255 os << setcolor( attr, fg, bg );
263 scope_setcolor(const scope_setcolor& r);
264 scope_setcolor& operator=(const scope_setcolor& r);
271 struct scope_setcolor : setcolor {
272 scope_setcolor() : m_os( 0 ) {}
273 explicit scope_setcolor(
275 term_attr::_ attr = term_attr::NORMAL,
276 term_color::_ fg = term_color::ORIGINAL,
277 term_color::_ bg = term_color::ORIGINAL )
279 setcolor(attr, fg, bg),
288 set_console_color(*m_os, &this->saved_attributes);
292 scope_setcolor(const scope_setcolor& r);
293 scope_setcolor& operator=(const scope_setcolor& r);
301 #define BOOST_TEST_SCOPE_SETCOLOR( is_color_output, os, attr, color ) \
302 utils::scope_setcolor const sc( \
304 is_color_output ? utils::attr : utils::term_attr::NORMAL, \
305 is_color_output ? utils::color : utils::term_color::ORIGINAL);\
306 ut_detail::ignore_unused_variable_warning( sc ) \
310 } // namespace unit_test
313 #include <boost/test/detail/enable_warnings.hpp>
315 #endif // BOOST_TEST_UTILS_SETCOLOR_HPP