Goby3  3.1.4
2024.02.22
term_color.h
Go to the documentation of this file.
1 // Copyright 2012-2023:
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 // File authors:
6 // Toby Schneider <toby@gobysoft.org>
7 //
8 //
9 // This file is part of the Goby Underwater Autonomy Project Libraries
10 // ("The Goby Libraries").
11 //
12 // The Goby Libraries are free software: you can redistribute them and/or modify
13 // them under the terms of the GNU Lesser General Public License as published by
14 // the Free Software Foundation, either version 2.1 of the License, or
15 // (at your option) any later version.
16 //
17 // The Goby Libraries are distributed in the hope that they will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public License
23 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
24 
25 #ifndef GOBY_UTIL_DEBUG_LOGGER_TERM_COLOR_H
26 #define GOBY_UTIL_DEBUG_LOGGER_TERM_COLOR_H
27 
28 #include <iostream> // for ostream
29 #include <map> // for map
30 #include <string> // for string
31 #include <utility> // for pair
32 
33 namespace goby
34 {
35 namespace util
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  {
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  TermColor();
164  ~TermColor() = default;
165 
166  TermColor(const TermColor&) = delete;
167  TermColor& operator=(const TermColor&) = delete;
168 
169  friend class TermColorDeleter;
170  static std::shared_ptr<TermColor> inst_;
171 
172  static TermColor* get_instance();
173  Colors::Color priv_from_str(const std::string& s) { return colors_map_[s]; }
174 
175  // red -> "red"
176  std::string priv_str_from_col(const Colors::Color& c)
177  {
178  for (const auto& p : colors_map_)
179  {
180  if (p.second == c)
181  return p.first;
182  }
183  return "nocolor";
184  }
185 
186  // "\33[31m" -> red
187  Colors::Color priv_from_esc_code(const std::string& s) { return esc_code_map_[s]; }
188 
189  // red -> "\33[31m"
190  std::string priv_esc_code_from_col(const Colors::Color& c)
191  {
192  for (const auto& p : esc_code_map_)
193  {
194  if (p.second == c)
195  return p.first;
196  }
197  return esc_nocolor;
198  }
199 
200  // "red" -> "\33[31m"
201  std::string priv_esc_code_from_str(const std::string& s)
202  {
203  return esc_code_from_col(from_str(s));
204  }
205 
206  private:
207  std::map<std::string, Colors::Color> colors_map_;
208  std::map<std::string, Colors::Color> esc_code_map_;
209 };
210 
212 {
213  public:
214  void operator()(TermColor* c) { delete c; }
215 };
216 
217 } // namespace util
218 } // namespace goby
219 
220 #endif
goby::util::Colors::lt_red
@ lt_red
Definition: term_color.h:115
goby::util::esc_green
const std::string esc_green
Definition: term_color.h:39
goby::util::esc_lt_yellow
const std::string esc_lt_yellow
Definition: term_color.h:42
goby::util::tcolor::yellow
std::ostream & yellow(std::ostream &os)
All text following this manipulator is yellow (e.g. std::cout << yellow << "text";)
Definition: term_color.h:74
goby::util::esc_yellow
const std::string esc_yellow
Definition: term_color.h:41
goby::util::Colors::Color
Color
The eight terminal colors (and bold or "light" variants)
Definition: term_color.h:111
goby::util::esc_white
const std::string esc_white
Definition: term_color.h:49
goby::util::TermColorDeleter::operator()
void operator()(TermColor *c)
Definition: term_color.h:214
goby::util::tcolor::white
std::ostream & white(std::ostream &os)
All text following this manipulator is white (e.g. std::cout << white << "text";)
Definition: term_color.h:98
goby::util::Colors::magenta
@ magenta
Definition: term_color.h:122
goby::util::TermColor::from_esc_code
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
goby
The global namespace for the Goby project.
Definition: acomms_constants.h:33
goby::util::Colors
Represents the eight available terminal colors (and bold variants)
Definition: term_color.h:108
goby::util::tcolor::magenta
std::ostream & magenta(std::ostream &os)
All text following this manipulator is magenta (e.g. std::cout << magenta << "text";)
Definition: term_color.h:86
goby::util::TermColor::str_from_col
static std::string str_from_col(const Colors::Color &c)
String from color enumeration (e,g, red -> "red")
Definition: term_color.h:139
goby::util::esc_lt_white
const std::string esc_lt_white
Definition: term_color.h:50
goby::util::esc_lt_magenta
const std::string esc_lt_magenta
Definition: term_color.h:46
goby::util::esc_lt_blue
const std::string esc_lt_blue
Definition: term_color.h:44
goby::util::esc_lt_red
const std::string esc_lt_red
Definition: term_color.h:38
goby::util::tcolor::red
std::ostream & red(std::ostream &os)
All text following this manipulator is red. (e.g. std::cout << red << "text";)
Definition: term_color.h:62
goby::util::tcolor::green
std::ostream & green(std::ostream &os)
All text following this manipulator is green (e.g. std::cout << green << "text";)
Definition: term_color.h:68
goby::util::tcolor::lt_cyan
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
goby::util::TermColor::esc_code_from_col
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
goby::util::Colors::cyan
@ cyan
Definition: term_color.h:124
goby::util::Colors::nocolor
@ nocolor
Definition: term_color.h:113
goby::util::TermColor::from_str
static Colors::Color from_str(const std::string &s)
Color enumeration from string (e.g. "blue" -> blue)
Definition: term_color.h:136
goby::util::esc_cyan
const std::string esc_cyan
Definition: term_color.h:47
goby::util::Colors::white
@ white
Definition: term_color.h:126
goby::util::Colors::red
@ red
Definition: term_color.h:114
goby::util::Colors::lt_blue
@ lt_blue
Definition: term_color.h:121
goby::util::tcolor::lt_magenta
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
goby::util::tcolor::add_escape_code
std::ostream & add_escape_code(std::ostream &os, const std::string &esc_code)
goby::util::tcolor::cyan
std::ostream & cyan(std::ostream &os)
All text following this manipulator is cyan (e.g. std::cout << cyan << "text";)
Definition: term_color.h:92
goby::util::Colors::blue
@ blue
Definition: term_color.h:120
goby::util::esc_blue
const std::string esc_blue
Definition: term_color.h:43
goby::util::TermColorDeleter
Definition: term_color.h:211
goby::util::Colors::lt_cyan
@ lt_cyan
Definition: term_color.h:125
goby::util::tcolor::lt_white
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
goby::util::Colors::lt_yellow
@ lt_yellow
Definition: term_color.h:119
goby::util::esc_lt_green
const std::string esc_lt_green
Definition: term_color.h:40
goby::util::esc_nocolor
const std::string esc_nocolor
Definition: term_color.h:51
goby::util::TermColor
Converts between string, escape code, and enumeration representations of the terminal colors.
Definition: term_color.h:132
goby::util::esc_magenta
const std::string esc_magenta
Definition: term_color.h:45
goby::util::esc_red
const std::string esc_red
Definition: term_color.h:37
goby::util::tcolor::nocolor
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
goby::util::TermColor::esc_code_from_str
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
goby::util::esc_lt_cyan
const std::string esc_lt_cyan
Definition: term_color.h:48
goby::util::Colors::green
@ green
Definition: term_color.h:116
goby::util::tcolor::blue
std::ostream & blue(std::ostream &os)
All text following this manipulator is blue (e.g. std::cout << blue << "text";)
Definition: term_color.h:80
goby::util::tcolor::lt_blue
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
goby::util::Colors::lt_white
@ lt_white
Definition: term_color.h:127
goby::util::Colors::yellow
@ yellow
Definition: term_color.h:118
goby::util::tcolor::lt_green
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
goby::util::Colors::lt_green
@ lt_green
Definition: term_color.h:117
goby::util::Colors::lt_magenta
@ lt_magenta
Definition: term_color.h:123
goby::util::tcolor::lt_red
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
goby::util::tcolor::lt_yellow
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