libmoost
|
00001 /* vim:set ts=3 sw=3 sts=3 et: */ 00028 #ifndef MOOST_TERMINAL_FORMAT_H__ 00029 #define MOOST_TERMINAL_FORMAT_H__ 00030 00031 #include <string> 00032 #include <sstream> 00033 00034 #include <boost/lexical_cast.hpp> 00035 #include <boost/date_time/posix_time/posix_time.hpp> // timing 00036 00037 namespace moost { 00038 00039 enum eColor 00040 { 00041 C_BLACK = 30, 00042 C_RED = 31, 00043 C_GREEN = 32, 00044 C_YELLOW = 33, 00045 C_BLUE = 34, 00046 C_MAGENTA = 35, 00047 C_CYAN = 36, 00048 C_WHITE = 37, 00049 00050 BGC_BLACK = 40, 00051 BGC_RED = 41, 00052 BGC_GREEN = 42, 00053 BGC_YELLOW = 43, 00054 BGC_BLUE = 44, 00055 BGC_MAGENTA = 45, 00056 BGC_CYAN = 46, 00057 BGC_WHITE = 47 00058 }; 00059 00060 enum eMask { // numeric values only as example of possible bitmask values: 00061 tf_standard=1<<0, 00062 tf_bold=1<<1, 00063 tf_italic=1<<2, 00064 tf_underline=1<<3, 00065 tf_blinking=1<<4, 00066 tf_reverse=1<<5, 00067 00068 tf_black=1<<6, 00069 tf_red=1<<7, 00070 tf_green=1<<8, 00071 tf_blue=1<<9, 00072 tf_magenta=1<<10, 00073 tf_cyan=1<<11, 00074 tf_white=1<<12, 00075 00076 tf_bgblack=1<<13, 00077 tf_bgred=1<<14, 00078 tf_bggreen=1<<15, 00079 tf_bgblue=1<<16, 00080 tf_bgmagenta=1<<17, 00081 tf_bgcyan=1<<18, 00082 tf_bgwhite=1<<19 00083 }; 00084 00085 // ----------------------------------------------------------------------------- 00086 00087 class null_terminal_format 00088 { 00089 public: 00090 static std::string bold() { return ""; } 00091 static std::string bold(const std::string& str) { return str; } 00092 00093 static std::string italic() { return ""; } 00094 static std::string italic(const std::string& str) { return str; } 00095 00096 static std::string underline() { return ""; } 00097 static std::string underline(const std::string& str) { return str; } 00098 00099 static std::string blinking() { return ""; } 00100 static std::string blinking(const std::string& str) { return str; } 00101 00102 static std::string reverse() { return ""; } 00103 static std::string reverse(const std::string& str) { return str; } 00104 00105 static std::string color(eColor /*c*/) { return ""; } 00106 static std::string color(const std::string& str, eColor /*c*/) { return str; } 00107 00108 static std::string reset(){ return ""; } 00109 00110 static std::string get(const std::string& str, eMask /*mask*/) 00111 { return str; } 00112 00113 static std::string getWarning(const std::string& warnStr = "WARNING") { return warnStr; } 00114 static std::string getError(const std::string& errStr = "ERROR") { return errStr; } 00115 static std::string getFailed(const std::string& errStr = "FAILED") { return errStr; } 00116 static std::string getOkay(const std::string& okayStr = "OK") { return okayStr; } 00117 00118 static std::string getTimeStamp(bool enclose = true) 00119 { 00120 boost::posix_time::ptime currTime(boost::posix_time::second_clock::local_time()); 00121 const boost::posix_time::time_duration& td = currTime.time_of_day(); 00122 std::ostringstream oss; 00123 char fill_char = '0'; 00124 00125 if (enclose) 00126 oss << '['; 00127 00128 oss << boost::gregorian::to_simple_string_type<char>(currTime.date()) << ' ' 00129 << std::setw(2) << std::setfill(fill_char) 00130 << boost::date_time::absolute_value(td.hours()) << ":" 00131 << std::setw(2) << std::setfill(fill_char) 00132 << boost::date_time::absolute_value(td.minutes()) << ":" 00133 << std::setw(2) << std::setfill(fill_char) 00134 << boost::date_time::absolute_value(td.seconds()) 00135 ; 00136 00137 if ( enclose ) 00138 oss << "] "; 00139 return oss.str(); 00140 } 00141 }; 00142 00143 // ----------------------------------------------------------------------------- 00144 00145 class vt_100_terminal_format 00146 { 00147 public: 00148 00149 static std::string bold() { return "\033[1m"; } 00150 static std::string bold(const std::string& str) { return bold() + str + reset(); } 00151 00152 static std::string italic() { return ""; } // no italic in vt100 00153 static std::string italic(const std::string& str) { return str; } 00154 00155 static std::string underline() { return "\033[4m"; } 00156 static std::string underline(const std::string& str) { return underline() + str + reset() ; } 00157 00158 static std::string blinking() { return "\033[5m"; } 00159 static std::string blinking(const std::string& str) { return blinking() + str + reset(); } 00160 00161 static std::string reverse() { return "\033[7m"; } 00162 static std::string reverse(const std::string& str) { return reverse() + str + reset(); } 00163 00164 static std::string color(eColor c) 00165 { return "\033[" + boost::lexical_cast<std::string>(c) + "m"; } 00166 static std::string color(const std::string& str, eColor c) 00167 { return color(c) + str + reset(); } 00168 00169 static std::string reset() 00170 { return "\033[0m"; } 00171 00172 static std::string get(const std::string& str, eMask mask) 00173 { 00174 std::ostringstream oss; 00175 00176 if ( mask & tf_bold ) 00177 oss << bold(); 00178 //if ( mask & tf_italic ) 00179 // oss << italic(); 00180 if ( mask & tf_underline ) 00181 oss << underline(); 00182 if ( mask & tf_blinking ) 00183 oss << blinking(); 00184 if ( mask & tf_reverse ) 00185 oss << reverse(); 00186 00187 if ( mask & tf_black ) 00188 oss << color(C_BLACK); 00189 if ( mask & tf_red ) 00190 oss << color(C_RED); 00191 if ( mask & tf_green ) 00192 oss << color(C_GREEN); 00193 if ( mask & tf_blue ) 00194 oss << color(C_BLACK); 00195 if ( mask & tf_magenta ) 00196 oss << color(C_MAGENTA); 00197 if ( mask & tf_cyan ) 00198 oss << color(C_CYAN); 00199 00200 if ( mask & tf_bgblack ) 00201 oss << color(BGC_BLACK); 00202 if ( mask & tf_bgred ) 00203 oss << color(BGC_RED); 00204 if ( mask & tf_bggreen ) 00205 oss << color(BGC_GREEN); 00206 if ( mask & tf_bgblue ) 00207 oss << color(BGC_BLACK); 00208 if ( mask & tf_bgmagenta ) 00209 oss << color(BGC_MAGENTA); 00210 if ( mask & tf_bgcyan ) 00211 oss << color(BGC_CYAN); 00212 00213 oss << str << reset(); 00214 return oss.str(); 00215 } 00216 00217 static std::string getWarning(const std::string& warnStr = "WARNING") 00218 { return bold() + color(C_YELLOW) + warnStr + reset(); } 00219 static std::string getError(const std::string& errStr = "ERROR") 00220 { return bold() + color(C_RED) + errStr + reset(); } 00221 static std::string getFailed(const std::string& errStr = "FAILED") 00222 { return bold() + color(C_RED) + errStr + reset(); } 00223 static std::string getOkay(const std::string& okayStr = "OK") 00224 { return bold() + color(C_GREEN) + okayStr + reset(); } 00225 00226 static std::string getTimeStamp(bool enclose = true) 00227 { return null_terminal_format::getTimeStamp(enclose); } 00228 00229 }; 00230 00231 // ----------------------------------------------------------------------------- 00232 00233 #ifdef _WIN32 00234 typedef null_terminal_format terminal_format; 00235 #else 00236 typedef vt_100_terminal_format terminal_format; 00237 #endif 00238 00239 // ----------------------------------------------------------------------------- 00240 00241 class scoped_format 00242 { 00243 public: 00244 scoped_format(std::ostream& s) : m_s(s) {} 00245 00246 ~scoped_format() 00247 { m_s << terminal_format::reset(); } 00248 00249 private: 00250 std::ostream& m_s; 00251 }; 00252 00253 // ----------------------------------------------------------------------------- 00254 00255 00256 } // moost 00257 00258 #endif // MOOST_TERMINAL_FORMAT_H__