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