OpenWalnut
1.4.0
|
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 #include <string> 00026 #include <iostream> 00027 #include <sstream> 00028 00029 #include "WTerminalColor.h" 00030 00031 WTerminalColor::WTerminalColor(): 00032 m_attrib( Default ), 00033 m_foreground( FGBlack ), 00034 m_background( BGBlack ), 00035 m_enabled( false ) 00036 { 00037 m_colorString = ""; 00038 m_colorResetString = ""; 00039 00040 generateControlStrings(); 00041 } 00042 00043 WTerminalColor::WTerminalColor( TerminalColorAttribute attrib, TerminalColorForeground foreground, TerminalColorBackground background ): 00044 m_attrib( attrib ), 00045 m_foreground( foreground ), 00046 m_background( background ), 00047 m_enabled( true ) 00048 { 00049 m_colorString = ""; 00050 m_colorResetString = ""; 00051 00052 generateControlStrings(); 00053 } 00054 00055 WTerminalColor::~WTerminalColor() 00056 { 00057 // cleanup 00058 } 00059 00060 void WTerminalColor::generateControlStrings() 00061 { 00062 m_colorString = ""; 00063 m_colorResetString = ""; 00064 00065 // When changing this platform specific ifdefs, please adapt unittest too! 00066 #if defined( __linux__ ) || defined( __APPLE__ ) 00067 if( m_enabled && ( m_attrib != Default ) ) 00068 { 00069 std::ostringstream ss; 00070 char cStart = 0x1B; 00071 ss << cStart << "[" << m_attrib << ";" << m_foreground; 00072 00073 // handle an unset background specially 00074 if( m_background == BGNone ) 00075 { 00076 ss << "m"; 00077 } 00078 else 00079 { 00080 ss << ";" << m_background << "m"; 00081 } 00082 00083 m_colorString = ss.str(); 00084 00085 // build reset string 00086 std::ostringstream ss2; 00087 ss2 << cStart << "[0m"; 00088 m_colorResetString = ss2.str(); 00089 } 00090 #endif 00091 } 00092 00093 std::ostream& WTerminalColor::operator<<( std::ostream& ostr ) const 00094 { 00095 return ostr << m_colorString; 00096 } 00097 00098 std::string WTerminalColor::operator!() const 00099 { 00100 return m_colorResetString; 00101 } 00102 00103 std::string WTerminalColor::operator()() const 00104 { 00105 return m_colorString; 00106 } 00107 00108 std::string WTerminalColor::operator+( const std::string& istr ) const 00109 { 00110 return m_colorString + istr; 00111 } 00112 00113 void WTerminalColor::setEnabled( bool enabled ) 00114 { 00115 m_enabled = enabled; 00116 00117 generateControlStrings(); 00118 } 00119 00120 bool WTerminalColor::isEnabled() const 00121 { 00122 return m_enabled; 00123 } 00124 00125 std::string WTerminalColor::operator()( const std::string s ) const 00126 { 00127 return m_colorString + s + m_colorResetString; 00128 } 00129