OpenWalnut 1.2.5
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 
00030 #include "WExportCommon.h"
00031 
00032 /**
00033  * Helper class to provide a convenient way to colorize output on the console.
00034  */
00035 class OWCOMMON_EXPORT WTerminalColor // NOLINT
00036 {
00037 friend class WTerminalColorTest;
00038 public:
00039 
00040     /**
00041      * Define possible attributes.
00042      */
00043     enum TerminalColorAttribute
00044     {
00045         Off         = 0,
00046         Bold        = 1,
00047         Underscore  = 4,
00048         Blink       = 5,
00049         Reverse     = 7,
00050         Concealed   = 8,
00051         Default     = 9         // this actually disables coloring
00052     };
00053 
00054     /**
00055      * Foreground colors.
00056      */
00057     enum TerminalColorForeground
00058     {
00059         FGBlack   = 30,
00060         FGRed     = 31,
00061         FGGreen   = 32,
00062         FGYellow  = 33,
00063         FGBlue    = 34,
00064         FGMagenta = 35,
00065         FGCyan    = 36,
00066         FGWhite   = 37
00067     };
00068 
00069     /**
00070      * Background colors.
00071      */
00072     enum TerminalColorBackground
00073     {
00074         BGNone    = 50,
00075         BGBlack   = 40,
00076         BGRed     = 41,
00077         BGGreen   = 42,
00078         BGYellow  = 43,
00079         BGBlue    = 44,
00080         BGMagenta = 45,
00081         BGCyan    = 46,
00082         BGWhite   = 47
00083     };
00084 
00085     /**
00086      * Constructor to create a color code which actually does not do any coloring.
00087      */
00088     WTerminalColor();
00089 
00090     /**
00091      * Creates a new terminal color.
00092      *
00093      * \param attrib attribute, like bold or blink
00094      * \param foreground foreground color
00095      * \param background background color
00096      */
00097     WTerminalColor( TerminalColorAttribute attrib, TerminalColorForeground foreground, TerminalColorBackground background = BGNone );
00098 
00099     /**
00100      * Destructor.
00101      */
00102     virtual ~WTerminalColor();
00103 
00104     /**
00105      * Gives the control string which actually enables the color.
00106      *
00107      * \param ostr the stream to extend by the color code.
00108      *
00109      * \return the color control string
00110      */
00111     std::ostream& operator<<( std::ostream& ostr ) const;
00112 
00113     /**
00114      * Gives the control string which actually enables the color.
00115      *
00116      * \return the color control string
00117      */
00118     std::string operator()() const;
00119 
00120     /**
00121      * Colorizes the given string and resets color after it.
00122      *
00123      * \param s the string to colorize
00124      *
00125      * \return the string including control sequences.
00126      */
00127     std::string operator()( const std::string s ) const;
00128 
00129     /**
00130      * Combines strings.
00131      *
00132      * \param istr the string to combine
00133      *
00134      * \return the concatenated string.
00135      */
00136     std::string operator+( const std::string& istr ) const;
00137 
00138     /**
00139      * Resets the color and returns control string.
00140      *
00141      * \return the control string which resets color settings.
00142      */
00143     std::string operator!() const;
00144 
00145     /**
00146      * With this you can easily trigger whether the color control string is used or if "" is returned.
00147      *
00148      * \param enabled true to have colors.
00149      */
00150     void setEnabled( bool enabled );
00151 
00152     /**
00153      * Is coloring enabled?
00154      *
00155      * \return true if enabled
00156      */
00157     bool isEnabled() const;
00158 
00159 protected:
00160 
00161     /**
00162      * The string actually containing the control sequence to enable colors on the console.
00163      */
00164     std::string m_colorString;
00165 
00166     /**
00167      * Control sequence to reset color.
00168      */
00169     std::string m_colorResetString;
00170 
00171     /**
00172      * Color attributes.
00173      */
00174     TerminalColorAttribute m_attrib;
00175 
00176     /**
00177      * The foreground color.
00178      */
00179     TerminalColorForeground m_foreground;
00180 
00181     /**
00182      * The background color.
00183      */
00184     TerminalColorBackground m_background;
00185 
00186 private:
00187 
00188     /**
00189      * Actually generates the control sequences.
00190      */
00191     void generateControlStrings();
00192 
00193     /**
00194      * True when colors should are used.
00195      */
00196     bool m_enabled;
00197 };
00198 
00199 #endif  // WTERMINALCOLOR_H
00200 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends