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