1 // (C) Copyright Gennadiy Rozental 2009-2014. 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 29 namespace boost { 30 namespace unit_test { 31 32 // ************************************************************************** // 33 // ************** term_attr ************** // 34 // ************************************************************************** // 35 36 struct term_attr { enum _ { 37 NORMAL = 0, 38 BRIGHT = 1, 39 DIM = 2, 40 UNDERLINE = 4, 41 BLINK = 5, 42 REVERSE = 7, 43 CROSSOUT = 9 44 }; }; 45 46 // ************************************************************************** // 47 // ************** term_color ************** // 48 // ************************************************************************** // 49 50 struct term_color { enum _ { 51 BLACK = 0, 52 RED = 1, 53 GREEN = 2, 54 YELLOW = 3, 55 BLUE = 4, 56 MAGENTA = 5, 57 CYAN = 6, 58 WHITE = 7, 59 ORIGINAL = 9 60 }; }; 61 62 // ************************************************************************** // 63 // ************** setcolor ************** // 64 // ************************************************************************** // 65 66 class setcolor { 67 public: 68 // Constructor setcolor(term_attr::_ attr=term_attr::NORMAL,term_color::_ fg=term_color::ORIGINAL,term_color::_ bg=term_color::ORIGINAL)69 explicit setcolor( term_attr::_ attr = term_attr::NORMAL, 70 term_color::_ fg = term_color::ORIGINAL, 71 term_color::_ bg = term_color::ORIGINAL ) 72 { 73 m_command_size = std::sprintf( m_control_command, "%c[%d;%d;%dm", 0x1B, attr, fg + 30, bg + 40 ); 74 } 75 76 friend std::ostream& operator <<(std::ostream & os,setcolor const & sc)77 operator<<( std::ostream& os, setcolor const& sc ) 78 { 79 return os.write( sc.m_control_command, sc.m_command_size ); 80 } 81 82 private: 83 // Data members 84 char m_control_command[13]; 85 int m_command_size; 86 }; 87 88 // ************************************************************************** // 89 // ************** scope_setcolor ************** // 90 // ************************************************************************** // 91 92 struct scope_setcolor { scope_setcolorboost::unit_test::scope_setcolor93 scope_setcolor() : m_os( 0 ) {} scope_setcolorboost::unit_test::scope_setcolor94 explicit scope_setcolor( std::ostream& os, 95 term_attr::_ attr = term_attr::NORMAL, 96 term_color::_ fg = term_color::ORIGINAL, 97 term_color::_ bg = term_color::ORIGINAL ) 98 : m_os( &os ) 99 { 100 os << setcolor( attr, fg, bg ); 101 } ~scope_setcolorboost::unit_test::scope_setcolor102 ~scope_setcolor() 103 { 104 if( m_os ) 105 *m_os << setcolor(); 106 } 107 private: 108 // Data members 109 std::ostream* m_os; 110 }; 111 112 } // namespace unit_test 113 } // namespace boost 114 115 #include <boost/test/detail/enable_warnings.hpp> 116 117 #endif // BOOST_TEST_UTILS_SETCOLOR_HPP 118