OpenWalnut  1.5.0dev
WTerminalColor.h
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 #ifndef WTERMINALCOLOR_H
26 #define WTERMINALCOLOR_H
27 
28 #include <string>
29 #include <iosfwd>
30 
31 /**
32  * Helper class to provide a convenient way to colorize output on the console.
33  */
34 class WTerminalColor // NOLINT
35 {
36 /**
37  * Acess for the test class.
38  */
39 friend class WTerminalColorTest;
40 public:
41  /**
42  * Define possible attributes.
43  */
45  {
46  Off = 0,
47  Bold = 1,
48  Underscore = 4,
49  Blink = 5,
50  Reverse = 7,
51  Concealed = 8,
52  Default = 9 // this actually disables coloring
53  };
54 
55  /**
56  * Foreground colors.
57  */
59  {
60  FGBlack = 30,
61  FGRed = 31,
62  FGGreen = 32,
63  FGYellow = 33,
64  FGBlue = 34,
65  FGMagenta = 35,
66  FGCyan = 36,
67  FGWhite = 37
68  };
69 
70  /**
71  * Background colors.
72  */
74  {
75  BGNone = 50,
76  BGBlack = 40,
77  BGRed = 41,
78  BGGreen = 42,
79  BGYellow = 43,
80  BGBlue = 44,
81  BGMagenta = 45,
82  BGCyan = 46,
83  BGWhite = 47
84  };
85 
86  /**
87  * Constructor to create a color code which actually does not do any coloring.
88  */
90 
91  /**
92  * Creates a new terminal color.
93  *
94  * \param attrib attribute, like bold or blink
95  * \param foreground foreground color
96  * \param background background color
97  */
99 
100  /**
101  * Destructor.
102  */
103  virtual ~WTerminalColor();
104 
105  /**
106  * Gives the control string which actually enables the color.
107  *
108  * \param ostr the stream to extend by the color code.
109  *
110  * \return the color control string
111  */
112  std::ostream& operator<<( std::ostream& ostr ) const;
113 
114  /**
115  * Gives the control string which actually enables the color.
116  *
117  * \return the color control string
118  */
119  std::string operator()() const;
120 
121  /**
122  * Colorizes the given string and resets color after it.
123  *
124  * \param s the string to colorize
125  *
126  * \return the string including control sequences.
127  */
128  std::string operator()( const std::string s ) const;
129 
130  /**
131  * Combines strings.
132  *
133  * \param istr the string to combine
134  *
135  * \return the concatenated string.
136  */
137  std::string operator+( const std::string& istr ) const;
138 
139  /**
140  * Resets the color and returns control string.
141  *
142  * \return the control string which resets color settings.
143  */
144  std::string operator!() const;
145 
146  /**
147  * With this you can easily trigger whether the color control string is used or if "" is returned.
148  *
149  * \param enabled true to have colors.
150  */
151  void setEnabled( bool enabled );
152 
153  /**
154  * Is coloring enabled?
155  *
156  * \return true if enabled
157  */
158  bool isEnabled() const;
159 
160 protected:
161  /**
162  * The string actually containing the control sequence to enable colors on the console.
163  */
164  std::string m_colorString;
165 
166  /**
167  * Control sequence to reset color.
168  */
169  std::string m_colorResetString;
170 
171  /**
172  * Color attributes.
173  */
175 
176  /**
177  * The foreground color.
178  */
180 
181  /**
182  * The background color.
183  */
185 
186 private:
187  /**
188  * Actually generates the control sequences.
189  */
190  void generateControlStrings();
191 
192  /**
193  * True when colors should are used.
194  */
195  bool m_enabled;
196 };
197 
198 #endif // WTERMINALCOLOR_H
Test WTerminalColor.
Helper class to provide a convenient way to colorize output on the console.
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.