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)
5
6// See http://www.boost.org/libs/test for the library home page.
7//
8// File : $RCSfile$
9//
10// Version : $Revision$
11//
12// Description : contains definition for setcolor iostream manipulator
13// ***************************************************************************
14
15#ifndef BOOST_TEST_UTILS_SETCOLOR_HPP
16#define BOOST_TEST_UTILS_SETCOLOR_HPP
17
18// Boost.Test
19#include <boost/test/detail/config.hpp>
20
21// STL
22#include <iostream>
23#include <cstdio>
24
25#include <boost/test/detail/suppress_warnings.hpp>
26
27//____________________________________________________________________________//
28
29namespace boost {
30namespace unit_test {
31namespace utils {
32
33// ************************************************************************** //
34// ************** term_attr ************** //
35// ************************************************************************** //
36
37struct term_attr { enum _ {
38 NORMAL = 0,
39 BRIGHT = 1,
40 DIM = 2,
41 UNDERLINE = 4,
42 BLINK = 5,
43 REVERSE = 7,
44 CROSSOUT = 9
45}; };
46
47// ************************************************************************** //
48// ************** term_color ************** //
49// ************************************************************************** //
50
51struct term_color { enum _ {
52 BLACK = 0,
53 RED = 1,
54 GREEN = 2,
55 YELLOW = 3,
56 BLUE = 4,
57 MAGENTA = 5,
58 CYAN = 6,
59 WHITE = 7,
60 ORIGINAL = 9
61}; };
62
63// ************************************************************************** //
64// ************** setcolor ************** //
65// ************************************************************************** //
66
67class setcolor {
68public:
69 // Constructor
70 explicit setcolor( term_attr::_ attr = term_attr::NORMAL,
71 term_color::_ fg = term_color::ORIGINAL,
72 term_color::_ bg = term_color::ORIGINAL )
73 {
74 m_command_size = std::sprintf( m_control_command, "%c[%d;%d;%dm", 0x1B, attr, fg + 30, bg + 40 );
75 }
76
77 friend std::ostream&
78 operator<<( std::ostream& os, setcolor const& sc )
79 {
80 return os.write( sc.m_control_command, sc.m_command_size );
81 }
82
83private:
84 // Data members
85 char m_control_command[13];
86 int m_command_size;
87};
88
89// ************************************************************************** //
90// ************** scope_setcolor ************** //
91// ************************************************************************** //
92
93struct scope_setcolor {
94 scope_setcolor() : m_os( 0 ) {}
95 explicit scope_setcolor( std::ostream& os,
96 term_attr::_ attr = term_attr::NORMAL,
97 term_color::_ fg = term_color::ORIGINAL,
98 term_color::_ bg = term_color::ORIGINAL )
99 : m_os( &os )
100 {
101 os << setcolor( attr, fg, bg );
102 }
103 ~scope_setcolor()
104 {
105 if( m_os )
106 *m_os << setcolor();
107 }
108private:
109 // Data members
110 std::ostream* m_os;
111};
112
113#define BOOST_TEST_SCOPE_SETCOLOR( is_color_output, os, attr, color ) \
114 utils::scope_setcolor const& sc = is_color_output \
115 ? utils::scope_setcolor( os, utils::attr, utils::color ) \
116 : utils::scope_setcolor(); \
117 ut_detail::ignore_unused_variable_warning( sc ) \
118/**/
119
120} // namespace utils
121} // namespace unit_test
122} // namespace boost
123
124#include <boost/test/detail/enable_warnings.hpp>
125
126#endif // BOOST_TEST_UTILS_SETCOLOR_HPP
127