OpenWalnut
1.2.5
|
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 #if defined( __linux__ ) || defined( __APPLE__ ) 00066 if( m_enabled && ( m_attrib != Default ) ) 00067 { 00068 std::ostringstream ss; 00069 char cStart = 0x1B; 00070 ss << cStart << "[" << m_attrib << ";" << m_foreground; 00071 00072 // handle an unset background specially 00073 if( m_background == BGNone ) 00074 { 00075 ss << "m"; 00076 } 00077 else 00078 { 00079 ss << ";" << m_background << "m"; 00080 } 00081 00082 m_colorString = ss.str(); 00083 00084 // build reset string 00085 std::ostringstream ss2; 00086 ss2 << cStart << "[0m"; 00087 m_colorResetString = ss2.str(); 00088 } 00089 #endif 00090 } 00091 00092 std::ostream& WTerminalColor::operator<<( std::ostream& ostr ) const 00093 { 00094 return ostr << m_colorString; 00095 } 00096 00097 std::string WTerminalColor::operator!() const 00098 { 00099 return m_colorResetString; 00100 } 00101 00102 std::string WTerminalColor::operator()() const 00103 { 00104 return m_colorString; 00105 } 00106 00107 std::string WTerminalColor::operator+( const std::string& istr ) const 00108 { 00109 return m_colorString + istr; 00110 } 00111 00112 void WTerminalColor::setEnabled( bool enabled ) 00113 { 00114 m_enabled = enabled; 00115 00116 generateControlStrings(); 00117 } 00118 00119 bool WTerminalColor::isEnabled() const 00120 { 00121 return m_enabled; 00122 } 00123 00124 std::string WTerminalColor::operator()( const std::string s ) const 00125 { 00126 return m_colorString + s + m_colorResetString; 00127 } 00128