OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WTerminalColor.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <string>
26 #include <iostream>
27 #include <sstream>
28 
29 #include "WTerminalColor.h"
30 
32  m_attrib( Default ),
33  m_foreground( FGBlack ),
34  m_background( BGBlack ),
35  m_enabled( false )
36 {
37  m_colorString = "";
38  m_colorResetString = "";
39 
41 }
42 
44  m_attrib( attrib ),
45  m_foreground( foreground ),
46  m_background( background ),
47  m_enabled( true )
48 {
49  m_colorString = "";
50  m_colorResetString = "";
51 
53 }
54 
56 {
57  // cleanup
58 }
59 
61 {
62  m_colorString = "";
63  m_colorResetString = "";
64 
65 #if defined( __linux__ ) || defined( __APPLE__ )
66  if( m_enabled && ( m_attrib != Default ) )
67  {
68  std::ostringstream ss;
69  char cStart = 0x1B;
70  ss << cStart << "[" << m_attrib << ";" << m_foreground;
71 
72  // handle an unset background specially
73  if( m_background == BGNone )
74  {
75  ss << "m";
76  }
77  else
78  {
79  ss << ";" << m_background << "m";
80  }
81 
82  m_colorString = ss.str();
83 
84  // build reset string
85  std::ostringstream ss2;
86  ss2 << cStart << "[0m";
87  m_colorResetString = ss2.str();
88  }
89 #endif
90 }
91 
92 std::ostream& WTerminalColor::operator<<( std::ostream& ostr ) const
93 {
94  return ostr << m_colorString;
95 }
96 
97 std::string WTerminalColor::operator!() const
98 {
99  return m_colorResetString;
100 }
101 
102 std::string WTerminalColor::operator()() const
103 {
104  return m_colorString;
105 }
106 
107 std::string WTerminalColor::operator+( const std::string& istr ) const
108 {
109  return m_colorString + istr;
110 }
111 
112 void WTerminalColor::setEnabled( bool enabled )
113 {
114  m_enabled = enabled;
115 
117 }
118 
120 {
121  return m_enabled;
122 }
123 
124 std::string WTerminalColor::operator()( const std::string s ) const
125 {
126  return m_colorString + s + m_colorResetString;
127 }
128