Note: Goby version 1 (shown here) is now considered obsolete. Please use version 2 for new projects, and consider upgrading old projects.

Goby Underwater Autonomy Project  Series: 1.1, revision: 163, released on 2013-02-06 14:23:27 -0500
util/liblogger/term_color.h
00001 
00002 //
00003 // this file is part of flex-ostream, a terminal display library
00004 // that provides an ostream with both terminal display and file logging
00005 //
00006 // This program is free software: you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation, either version 3 of the License, or
00009 // (at your option) any later version.
00010 //
00011 // This software is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU General Public License
00017 // along with this software.  If not, see <http://www.gnu.org/licenses/>.
00018 
00019 #ifndef TermColor20091211H
00020 #define TermColor20091211H
00021 
00022 #include <iostream>
00023 #include <string>
00024 
00025 #include <boost/assign.hpp>
00026 #include <boost/foreach.hpp>
00027 
00028 namespace goby
00029 {    
00030     namespace util
00031     {
00032         const std::string esc_red = "\33[31m";
00033         const std::string esc_lt_red = "\33[91m";
00034         const std::string esc_green = "\33[32m";
00035         const std::string esc_lt_green = "\33[92m";
00036         const std::string esc_yellow = "\33[33m";
00037         const std::string esc_lt_yellow = "\33[93m";
00038         const std::string esc_blue = "\33[34m";
00039         const std::string esc_lt_blue ="\33[94m";
00040         const std::string esc_magenta = "\33[35m";
00041         const std::string esc_lt_magenta = "\33[95m";
00042         const std::string esc_cyan = "\33[36m";
00043         const std::string esc_lt_cyan = "\33[96m";
00044         const std::string esc_white = "\33[37m";
00045         const std::string esc_lt_white = "\33[97m";
00046         const std::string esc_nocolor = "\33[0m";
00047 
00049         namespace tcolor
00050         {
00054             std::ostream& add_escape_code(std::ostream& os, const std::string& esc_code);
00055 
00057             inline std::ostream& red(std::ostream& os)
00058             { return(add_escape_code(os, esc_red)); }
00059             
00061             inline std::ostream& lt_red(std::ostream& os) 
00062             { return(add_escape_code(os, esc_lt_red)); }
00063 
00065             inline std::ostream& green(std::ostream& os) 
00066             { return(add_escape_code(os, esc_green)); }
00067 
00069             inline std::ostream& lt_green(std::ostream& os) 
00070             { return(add_escape_code(os, esc_lt_green)); }
00071  
00073             inline std::ostream& yellow(std::ostream& os) 
00074             { return(add_escape_code(os, esc_yellow)); }
00075 
00077             inline std::ostream& lt_yellow(std::ostream& os) 
00078             { return(add_escape_code(os, esc_lt_yellow)); }
00079 
00081             inline std::ostream& blue(std::ostream& os) 
00082             { return(add_escape_code(os, esc_blue)); }
00083 
00085             inline std::ostream& lt_blue(std::ostream& os) 
00086             { return(add_escape_code(os, esc_lt_blue)); }
00087 
00089             inline std::ostream& magenta(std::ostream& os) 
00090             { return(add_escape_code(os, esc_magenta)); }
00091 
00093             inline std::ostream& lt_magenta(std::ostream& os) 
00094             { return(add_escape_code(os, esc_lt_magenta)); }
00095 
00097             inline std::ostream& cyan(std::ostream& os)
00098             { return(add_escape_code(os, esc_cyan)); }
00099 
00101             inline std::ostream& lt_cyan(std::ostream& os) 
00102             { return(add_escape_code(os, esc_lt_cyan)); }
00103  
00105             inline std::ostream& white(std::ostream& os) 
00106             { return(add_escape_code(os, esc_white)); }
00107   
00109             inline std::ostream& lt_white(std::ostream& os) 
00110             { return(add_escape_code(os, esc_lt_white)); }
00111 
00113             inline std::ostream& nocolor(std::ostream& os) 
00114             { return(add_escape_code(os, esc_nocolor)); }    
00115         }
00116 
00118         struct Colors
00119         {
00121             enum Color { nocolor,
00122                          red,     lt_red,
00123                          green,   lt_green,
00124                          yellow,  lt_yellow,
00125                          blue,    lt_blue,
00126                          magenta, lt_magenta,
00127                          cyan,    lt_cyan,
00128                          white,   lt_white };
00129         };
00130 
00132         class TermColor
00133         {
00134           public:
00136             static Colors::Color from_str(const std::string& s)
00137             { return get_instance()->priv_from_str(s); }
00138             
00140             static std::string str_from_col(const Colors::Color& c)
00141             { return get_instance()->priv_str_from_col(c); }
00142 
00144             static Colors::Color from_esc_code(const std::string& s)
00145             { return get_instance()->priv_from_esc_code(s); }
00146 
00148             static std::string esc_code_from_col(const Colors::Color& c)
00149             { return get_instance()->priv_esc_code_from_col(c); }
00150         
00152             static std::string esc_code_from_str(const std::string& s)
00153             { return get_instance()->priv_esc_code_from_str(s); }
00154 
00155           private:
00156             TermColor();
00157             ~TermColor();            
00158             TermColor(const TermColor&);
00159             TermColor& operator = (const TermColor&);
00160 
00161             static TermColor* get_instance();
00162             
00163             Colors::Color priv_from_str(const std::string& s)
00164             { return colors_map_[s]; }
00165             
00166             // red -> "red"
00167             std::string priv_str_from_col(const Colors::Color& c)
00168             {
00169                 typedef std::pair<std::string, Colors::Color> P;
00170                 BOOST_FOREACH(const P& p, colors_map_)
00171                 {
00172                     if(p.second == c)
00173                         return p.first;
00174                 }
00175                 return "nocolor";
00176             }
00177 
00178             // "\33[31m" -> red
00179             Colors::Color priv_from_esc_code(const std::string& s)
00180             {
00181                 return esc_code_map_[s];
00182             }
00183 
00184             // red -> "\33[31m"
00185             std::string priv_esc_code_from_col(const Colors::Color& c)
00186             {
00187                 typedef std::pair<std::string, Colors::Color> P;
00188                 BOOST_FOREACH(const P& p, esc_code_map_)
00189                 {
00190                     if(p.second == c)
00191                         return p.first;
00192                 }
00193                 return esc_nocolor;
00194             }
00195         
00196             // "red" -> "\33[31m"
00197             std::string priv_esc_code_from_str(const std::string& s)
00198             {
00199                 return esc_code_from_col(from_str(s));
00200             }
00201 
00202             
00203           private:        
00204             static TermColor* inst_;
00205             std::map<std::string, Colors::Color> colors_map_;
00206             std::map<std::string, Colors::Color> esc_code_map_;
00207         };    
00208     }
00209 }
00210 
00211 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends