OpenWalnut  1.4.0
WTerminalColor.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WTERMINALCOLOR_H
00026 #define WTERMINALCOLOR_H
00027 
00028 #include <string>
00029 #include <iosfwd>
00030 
00031 /**
00032  * Helper class to provide a convenient way to colorize output on the console.
00033  */
00034 class WTerminalColor // NOLINT
00035 {
00036 friend class WTerminalColorTest;
00037 public:
00038     /**
00039      * Define possible attributes.
00040      */
00041     enum TerminalColorAttribute
00042     {
00043         Off         = 0,
00044         Bold        = 1,
00045         Underscore  = 4,
00046         Blink       = 5,
00047         Reverse     = 7,
00048         Concealed   = 8,
00049         Default     = 9         // this actually disables coloring
00050     };
00051 
00052     /**
00053      * Foreground colors.
00054      */
00055     enum TerminalColorForeground
00056     {
00057         FGBlack   = 30,
00058         FGRed     = 31,
00059         FGGreen   = 32,
00060         FGYellow  = 33,
00061         FGBlue    = 34,
00062         FGMagenta = 35,
00063         FGCyan    = 36,
00064         FGWhite   = 37
00065     };
00066 
00067     /**
00068      * Background colors.
00069      */
00070     enum TerminalColorBackground
00071     {
00072         BGNone    = 50,
00073         BGBlack   = 40,
00074         BGRed     = 41,
00075         BGGreen   = 42,
00076         BGYellow  = 43,
00077         BGBlue    = 44,
00078         BGMagenta = 45,
00079         BGCyan    = 46,
00080         BGWhite   = 47
00081     };
00082 
00083     /**
00084      * Constructor to create a color code which actually does not do any coloring.
00085      */
00086     WTerminalColor();
00087 
00088     /**
00089      * Creates a new terminal color.
00090      *
00091      * \param attrib attribute, like bold or blink
00092      * \param foreground foreground color
00093      * \param background background color
00094      */
00095     WTerminalColor( TerminalColorAttribute attrib, TerminalColorForeground foreground, TerminalColorBackground background = BGNone );
00096 
00097     /**
00098      * Destructor.
00099      */
00100     virtual ~WTerminalColor();
00101 
00102     /**
00103      * Gives the control string which actually enables the color.
00104      *
00105      * \param ostr the stream to extend by the color code.
00106      *
00107      * \return the color control string
00108      */
00109     std::ostream& operator<<( std::ostream& ostr ) const;
00110 
00111     /**
00112      * Gives the control string which actually enables the color.
00113      *
00114      * \return the color control string
00115      */
00116     std::string operator()() const;
00117 
00118     /**
00119      * Colorizes the given string and resets color after it.
00120      *
00121      * \param s the string to colorize
00122      *
00123      * \return the string including control sequences.
00124      */
00125     std::string operator()( const std::string s ) const;
00126 
00127     /**
00128      * Combines strings.
00129      *
00130      * \param istr the string to combine
00131      *
00132      * \return the concatenated string.
00133      */
00134     std::string operator+( const std::string& istr ) const;
00135 
00136     /**
00137      * Resets the color and returns control string.
00138      *
00139      * \return the control string which resets color settings.
00140      */
00141     std::string operator!() const;
00142 
00143     /**
00144      * With this you can easily trigger whether the color control string is used or if "" is returned.
00145      *
00146      * \param enabled true to have colors.
00147      */
00148     void setEnabled( bool enabled );
00149 
00150     /**
00151      * Is coloring enabled?
00152      *
00153      * \return true if enabled
00154      */
00155     bool isEnabled() const;
00156 
00157 protected:
00158     /**
00159      * The string actually containing the control sequence to enable colors on the console.
00160      */
00161     std::string m_colorString;
00162 
00163     /**
00164      * Control sequence to reset color.
00165      */
00166     std::string m_colorResetString;
00167 
00168     /**
00169      * Color attributes.
00170      */
00171     TerminalColorAttribute m_attrib;
00172 
00173     /**
00174      * The foreground color.
00175      */
00176     TerminalColorForeground m_foreground;
00177 
00178     /**
00179      * The background color.
00180      */
00181     TerminalColorBackground m_background;
00182 
00183 private:
00184     /**
00185      * Actually generates the control sequences.
00186      */
00187     void generateControlStrings();
00188 
00189     /**
00190      * True when colors should are used.
00191      */
00192     bool m_enabled;
00193 };
00194 
00195 #endif  // WTERMINALCOLOR_H
00196