OpenWalnut  1.5.0dev
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 // When changing this platform specific ifdefs, please adapt unittest too!
66 #if defined( __linux__ ) || defined( __APPLE__ )
67  if( m_enabled && ( m_attrib != Default ) )
68  {
69  std::ostringstream ss;
70  char cStart = 0x1B;
71  ss << cStart << "[" << m_attrib << ";" << m_foreground;
72 
73  // handle an unset background specially
74  if( m_background == BGNone )
75  {
76  ss << "m";
77  }
78  else
79  {
80  ss << ";" << m_background << "m";
81  }
82 
83  m_colorString = ss.str();
84 
85  // build reset string
86  std::ostringstream ss2;
87  ss2 << cStart << "[0m";
88  m_colorResetString = ss2.str();
89  }
90 #endif
91 }
92 
93 std::ostream& WTerminalColor::operator<<( std::ostream& ostr ) const
94 {
95  return ostr << m_colorString;
96 }
97 
98 std::string WTerminalColor::operator!() const
99 {
100  return m_colorResetString;
101 }
102 
103 std::string WTerminalColor::operator()() const
104 {
105  return m_colorString;
106 }
107 
108 std::string WTerminalColor::operator+( const std::string& istr ) const
109 {
110  return m_colorString + istr;
111 }
112 
113 void WTerminalColor::setEnabled( bool enabled )
114 {
115  m_enabled = enabled;
116 
118 }
119 
121 {
122  return m_enabled;
123 }
124 
125 std::string WTerminalColor::operator()( const std::string s ) const
126 {
127  return m_colorString + s + m_colorResetString;
128 }
129 
TerminalColorForeground
Foreground colors.
WTerminalColor()
Constructor to create a color code which actually does not do any coloring.
TerminalColorAttribute
Define possible attributes.
std::ostream & operator<<(std::ostream &ostr) const
Gives the control string which actually enables the color.
void setEnabled(bool enabled)
With this you can easily trigger whether the color control string is used or if "" is returned.
std::string m_colorString
The string actually containing the control sequence to enable colors on the console.
void generateControlStrings()
Actually generates the control sequences.
TerminalColorAttribute m_attrib
Color attributes.
std::string operator+(const std::string &istr) const
Combines strings.
std::string operator!() const
Resets the color and returns control string.
std::string m_colorResetString
Control sequence to reset color.
TerminalColorBackground
Background colors.
virtual ~WTerminalColor()
Destructor.
bool isEnabled() const
Is coloring enabled?
std::string operator()() const
Gives the control string which actually enables the color.
TerminalColorForeground m_foreground
The foreground color.
TerminalColorBackground m_background
The background color.
bool m_enabled
True when colors should are used.