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