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