Goby v2
term_color.h
1 // Copyright 2009-2018 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Goby Underwater Autonomy Project Libraries
8 // ("The Goby Libraries").
9 //
10 // The Goby Libraries are free software: you can redistribute them and/or modify
11 // them under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // The Goby Libraries are distributed in the hope that they will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
22 
23 #ifndef TermColor20091211H
24 #define TermColor20091211H
25 
26 #include <iostream>
27 #include <string>
28 
29 #include <boost/assign.hpp>
30 #include <boost/foreach.hpp>
31 #include <boost/shared_ptr.hpp>
32 
33 namespace goby
34 {
35 namespace common
36 {
37 const std::string esc_red = "\33[31m";
38 const std::string esc_lt_red = "\33[91m";
39 const std::string esc_green = "\33[32m";
40 const std::string esc_lt_green = "\33[92m";
41 const std::string esc_yellow = "\33[33m";
42 const std::string esc_lt_yellow = "\33[93m";
43 const std::string esc_blue = "\33[34m";
44 const std::string esc_lt_blue = "\33[94m";
45 const std::string esc_magenta = "\33[35m";
46 const std::string esc_lt_magenta = "\33[95m";
47 const std::string esc_cyan = "\33[36m";
48 const std::string esc_lt_cyan = "\33[96m";
49 const std::string esc_white = "\33[37m";
50 const std::string esc_lt_white = "\33[97m";
51 const std::string esc_nocolor = "\33[0m";
52 
54 namespace tcolor
55 {
59 std::ostream& add_escape_code(std::ostream& os, const std::string& esc_code);
60 
62 inline std::ostream& red(std::ostream& os) { return (add_escape_code(os, esc_red)); }
63 
65 inline std::ostream& lt_red(std::ostream& os) { return (add_escape_code(os, esc_lt_red)); }
66 
68 inline std::ostream& green(std::ostream& os) { return (add_escape_code(os, esc_green)); }
69 
71 inline std::ostream& lt_green(std::ostream& os) { return (add_escape_code(os, esc_lt_green)); }
72 
74 inline std::ostream& yellow(std::ostream& os) { return (add_escape_code(os, esc_yellow)); }
75 
77 inline std::ostream& lt_yellow(std::ostream& os) { return (add_escape_code(os, esc_lt_yellow)); }
78 
80 inline std::ostream& blue(std::ostream& os) { return (add_escape_code(os, esc_blue)); }
81 
83 inline std::ostream& lt_blue(std::ostream& os) { return (add_escape_code(os, esc_lt_blue)); }
84 
86 inline std::ostream& magenta(std::ostream& os) { return (add_escape_code(os, esc_magenta)); }
87 
89 inline std::ostream& lt_magenta(std::ostream& os) { return (add_escape_code(os, esc_lt_magenta)); }
90 
92 inline std::ostream& cyan(std::ostream& os) { return (add_escape_code(os, esc_cyan)); }
93 
95 inline std::ostream& lt_cyan(std::ostream& os) { return (add_escape_code(os, esc_lt_cyan)); }
96 
98 inline std::ostream& white(std::ostream& os) { return (add_escape_code(os, esc_white)); }
99 
101 inline std::ostream& lt_white(std::ostream& os) { return (add_escape_code(os, esc_lt_white)); }
102 
104 inline std::ostream& nocolor(std::ostream& os) { return (add_escape_code(os, esc_nocolor)); }
105 } // namespace tcolor
106 
108 struct Colors
109 {
111  enum Color
112  {
113  nocolor,
114  red,
115  lt_red,
116  green,
117  lt_green,
118  yellow,
119  lt_yellow,
120  blue,
121  lt_blue,
122  magenta,
123  lt_magenta,
124  cyan,
125  lt_cyan,
126  white,
127  lt_white
128  };
129 };
130 
133 {
134  public:
136  static Colors::Color from_str(const std::string& s) { return get_instance()->priv_from_str(s); }
137 
139  static std::string str_from_col(const Colors::Color& c)
140  {
141  return get_instance()->priv_str_from_col(c);
142  }
143 
145  static Colors::Color from_esc_code(const std::string& s)
146  {
147  return get_instance()->priv_from_esc_code(s);
148  }
149 
151  static std::string esc_code_from_col(const Colors::Color& c)
152  {
153  return get_instance()->priv_esc_code_from_col(c);
154  }
155 
157  static std::string esc_code_from_str(const std::string& s)
158  {
159  return get_instance()->priv_esc_code_from_str(s);
160  }
161 
162  private:
163  // so we can use shared_ptr to hold the singleton
164  template <typename T> friend void boost::checked_delete(T*);
165 
166  TermColor();
167  ~TermColor() {}
168 
169  TermColor(const TermColor&);
170  TermColor& operator=(const TermColor&);
171 
172  static TermColor* get_instance() { return inst_.get(); }
173 
174  Colors::Color priv_from_str(const std::string& s) { return colors_map_[s]; }
175 
176  // red -> "red"
177  std::string priv_str_from_col(const Colors::Color& c)
178  {
179  typedef std::pair<std::string, Colors::Color> P;
180  BOOST_FOREACH (const P& p, colors_map_)
181  {
182  if (p.second == c)
183  return p.first;
184  }
185  return "nocolor";
186  }
187 
188  // "\33[31m" -> red
189  Colors::Color priv_from_esc_code(const std::string& s) { return esc_code_map_[s]; }
190 
191  // red -> "\33[31m"
192  std::string priv_esc_code_from_col(const Colors::Color& c)
193  {
194  typedef std::pair<std::string, Colors::Color> P;
195  BOOST_FOREACH (const P& p, esc_code_map_)
196  {
197  if (p.second == c)
198  return p.first;
199  }
200  return esc_nocolor;
201  }
202 
203  // "red" -> "\33[31m"
204  std::string priv_esc_code_from_str(const std::string& s)
205  {
206  return esc_code_from_col(from_str(s));
207  }
208 
209  private:
210  static boost::shared_ptr<TermColor> inst_;
211  std::map<std::string, Colors::Color> colors_map_;
212  std::map<std::string, Colors::Color> esc_code_map_;
213 };
214 } // namespace common
215 } // namespace goby
216 
217 #endif
std::ostream & white(std::ostream &os)
All text following this manipulator is white (e.g. std::cout << white << "text";) ...
Definition: term_color.h:98
std::ostream & add_escape_code(std::ostream &os, const std::string &esc_code)
Definition: term_color.cpp:27
static std::string str_from_col(const Colors::Color &c)
String from color enumeration (e,g, red -> "red")
Definition: term_color.h:139
std::ostream & lt_blue(std::ostream &os)
All text following this manipulator is light blue (e.g. std::cout << lt_blue << "text";) ...
Definition: term_color.h:83
Converts between string, escape code, and enumeration representations of the terminal colors...
Definition: term_color.h:132
std::ostream & lt_red(std::ostream &os)
All text following this manipulator is light red (e.g. std::cout << lt_red << "text";) ...
Definition: term_color.h:65
static Colors::Color from_str(const std::string &s)
Color enumeration from string (e.g. "blue" -> blue)
Definition: term_color.h:136
std::ostream & lt_cyan(std::ostream &os)
All text following this manipulator is light cyan (e.g. std::cout << lt_cyan << "text";) ...
Definition: term_color.h:95
std::ostream & lt_white(std::ostream &os)
All text following this manipulator is bright white (e.g. std::cout << lt_white << "text";) ...
Definition: term_color.h:101
std::ostream & magenta(std::ostream &os)
All text following this manipulator is magenta (e.g. std::cout << magenta << "text";) ...
Definition: term_color.h:86
Color
The eight terminal colors (and bold or "light" variants)
Definition: term_color.h:111
static Colors::Color from_esc_code(const std::string &s)
Color enumeration from escape code (e,g, "\33[31m" -> red)
Definition: term_color.h:145
std::ostream & lt_green(std::ostream &os)
All text following this manipulator is light green (e.g. std::cout << lt_green << "text";) ...
Definition: term_color.h:71
std::ostream & red(std::ostream &os)
All text following this manipulator is red. (e.g. std::cout << red << "text";)
Definition: term_color.h:62
static std::string esc_code_from_str(const std::string &s)
Escape code from string (e.g. "red" -> "\33[31m")
Definition: term_color.h:157
The global namespace for the Goby project.
std::ostream & blue(std::ostream &os)
All text following this manipulator is blue (e.g. std::cout << blue << "text";)
Definition: term_color.h:80
std::ostream & green(std::ostream &os)
All text following this manipulator is green (e.g. std::cout << green << "text";) ...
Definition: term_color.h:68
std::ostream & lt_yellow(std::ostream &os)
All text following this manipulator is light yellow (e.g. std::cout << lt_yellow << "text";) ...
Definition: term_color.h:77
std::ostream & cyan(std::ostream &os)
All text following this manipulator is cyan (e.g. std::cout << cyan << "text";)
Definition: term_color.h:92
Represents the eight available terminal colors (and bold variants)
Definition: term_color.h:108
std::ostream & nocolor(std::ostream &os)
All text following this manipulator is uncolored (e.g. std::cout << green << "green" << nocolor << "u...
Definition: term_color.h:104
static std::string esc_code_from_col(const Colors::Color &c)
Escape code from color enumeration (e.g. red -> "\33[31m")
Definition: term_color.h:151
std::ostream & yellow(std::ostream &os)
All text following this manipulator is yellow (e.g. std::cout << yellow << "text";) ...
Definition: term_color.h:74
std::ostream & lt_magenta(std::ostream &os)
All text following this manipulator is light magenta (e.g. std::cout << lt_magenta << "text";) ...
Definition: term_color.h:89