Goby3 3.2.3
2025.05.13
Loading...
Searching...
No Matches
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
33namespace goby
34{
35namespace util
36{
37const std::string esc_red = "\33[31m";
38const std::string esc_lt_red = "\33[91m";
39const std::string esc_green = "\33[32m";
40const std::string esc_lt_green = "\33[92m";
41const std::string esc_yellow = "\33[33m";
42const std::string esc_lt_yellow = "\33[93m";
43const std::string esc_blue = "\33[34m";
44const std::string esc_lt_blue = "\33[94m";
45const std::string esc_magenta = "\33[35m";
46const std::string esc_lt_magenta = "\33[95m";
47const std::string esc_cyan = "\33[36m";
48const std::string esc_lt_cyan = "\33[96m";
49const std::string esc_white = "\33[37m";
50const std::string esc_lt_white = "\33[97m";
51const std::string esc_nocolor = "\33[0m";
52
54namespace tcolor
55{
59std::ostream& add_escape_code(std::ostream& os, const std::string& esc_code);
60
62inline std::ostream& red(std::ostream& os) { return (add_escape_code(os, esc_red)); }
63
65inline std::ostream& lt_red(std::ostream& os) { return (add_escape_code(os, esc_lt_red)); }
66
68inline std::ostream& green(std::ostream& os) { return (add_escape_code(os, esc_green)); }
69
71inline std::ostream& lt_green(std::ostream& os) { return (add_escape_code(os, esc_lt_green)); }
72
74inline std::ostream& yellow(std::ostream& os) { return (add_escape_code(os, esc_yellow)); }
75
77inline std::ostream& lt_yellow(std::ostream& os) { return (add_escape_code(os, esc_lt_yellow)); }
78
80inline std::ostream& blue(std::ostream& os) { return (add_escape_code(os, esc_blue)); }
81
83inline std::ostream& lt_blue(std::ostream& os) { return (add_escape_code(os, esc_lt_blue)); }
84
86inline std::ostream& magenta(std::ostream& os) { return (add_escape_code(os, esc_magenta)); }
87
89inline std::ostream& lt_magenta(std::ostream& os) { return (add_escape_code(os, esc_lt_magenta)); }
90
92inline std::ostream& cyan(std::ostream& os) { return (add_escape_code(os, esc_cyan)); }
93
95inline std::ostream& lt_cyan(std::ostream& os) { return (add_escape_code(os, esc_lt_cyan)); }
96
98inline std::ostream& white(std::ostream& os) { return (add_escape_code(os, esc_white)); }
99
101inline std::ostream& lt_white(std::ostream& os) { return (add_escape_code(os, esc_lt_white)); }
102
104inline std::ostream& nocolor(std::ostream& os) { return (add_escape_code(os, esc_nocolor)); }
105} // namespace tcolor
106
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
void operator()(TermColor *c)
Definition term_color.h:214
Converts between string, escape code, and enumeration representations of the terminal colors.
Definition term_color.h:133
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
static std::string str_from_col(const Colors::Color &c)
String from color enumeration (e,g, red -> "red")
Definition term_color.h:139
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
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
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_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 & nocolor(std::ostream &os)
All text following this manipulator is uncolored (e.g. std::cout << green << "green" << nocolor << "u...
Definition term_color.h:104
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 & add_escape_code(std::ostream &os, const std::string &esc_code)
std::ostream & magenta(std::ostream &os)
All text following this manipulator is magenta (e.g. std::cout << magenta << "text";)
Definition term_color.h:86
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
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 & yellow(std::ostream &os)
All text following this manipulator is yellow (e.g. std::cout << yellow << "text";)
Definition term_color.h:74
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 & red(std::ostream &os)
All text following this manipulator is red. (e.g. std::cout << red << "text";)
Definition term_color.h:62
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
std::ostream & cyan(std::ostream &os)
All text following this manipulator is cyan (e.g. std::cout << cyan << "text";)
Definition term_color.h:92
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
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_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 & blue(std::ostream &os)
All text following this manipulator is blue (e.g. std::cout << blue << "text";)
Definition term_color.h:80
const std::string esc_green
Definition term_color.h:39
const std::string esc_red
Definition term_color.h:37
const std::string esc_white
Definition term_color.h:49
const std::string esc_lt_red
Definition term_color.h:38
const std::string esc_lt_magenta
Definition term_color.h:46
const std::string esc_cyan
Definition term_color.h:47
const std::string esc_lt_yellow
Definition term_color.h:42
const std::string esc_blue
Definition term_color.h:43
const std::string esc_magenta
Definition term_color.h:45
const std::string esc_yellow
Definition term_color.h:41
const std::string esc_lt_cyan
Definition term_color.h:48
const std::string esc_lt_blue
Definition term_color.h:44
const std::string esc_lt_white
Definition term_color.h:50
const std::string esc_lt_green
Definition term_color.h:40
const std::string esc_nocolor
Definition term_color.h:51
The global namespace for the Goby project.
Represents the eight available terminal colors (and bold variants)
Definition term_color.h:109
Color
The eight terminal colors (and bold or "light" variants)
Definition term_color.h:112