text_style.h
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement
4  *
5  * Copyright (c) 2020,
6  * TU Dortmund - Institute of Control Theory and Systems Engineering.
7  * All rights reserved.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  *
22  * Authors: Christoph Rösmann
23  *********************************************************************/
24 
25 #ifndef SRC_CORE_INCLUDE_CORBO_CORE_TEXT_STYLE_H_
26 #define SRC_CORE_INCLUDE_CORBO_CORE_TEXT_STYLE_H_
27 
28 #include <iostream>
29 #include <sstream>
30 #include <string>
31 
32 namespace corbo {
33 
40 enum class TextColorCode {
41  FG_DEFAULT = 39,
42  FG_RED = 31,
43  FG_GREEN = 32,
44  FG_BLUE = 34,
45  FG_BLACK = 30,
46  FG_YELLOW = 33,
47  FG_MAGENTA = 35,
48  FG_CYAN = 36,
49  FG_LIGHT_GRAY = 37,
50  FG_DARK_GRAY = 90,
51  FG_LIGHT_RED = 91,
52  FG_LIGHT_GREEN = 92,
53  FG_LIGHT_YELLOW = 93,
54  FG_LIGHT_BLUE = 94,
55  FG_LIGHT_MAGENTA = 95,
56  FG_LIGHT_CYAN = 96,
57  FG_WHITE = 97,
58  BG_RED = 41,
59  BG_GREEN = 42,
60  BG_BLUE = 44,
61  BG_DEFAULT = 49
62 };
63 
65 inline std::ostream& operator<<(std::ostream& os, TextColorCode code)
66 {
67 #ifdef __linux__
68  return os << "\033[" << static_cast<int>(code) << "m";
69 #else // not supported
70  return os;
71 #endif
72 }
73 
75 inline std::string coloredText(const std::string& text, TextColorCode color)
76 {
77 #ifdef __linux__
78  std::stringstream ss;
79  ss << color << text << TextColorCode::FG_DEFAULT;
80  return ss.str();
81 #else // not supported
82  return text;
83 #endif
84 }
85 } // namespace corbo
86 
87 #endif // SRC_CORE_INCLUDE_CORBO_CORE_TEXT_STYLE_H_
TextColorCode
Color codes for colored text in consoles/terminals (if supported)
Definition: text_style.h:40
std::string coloredText(const std::string &text, TextColorCode color)
Return input text as colored text for terminal output (note: the terminal must support colored text) ...
Definition: text_style.h:75
std::ostream & operator<<(std::ostream &os, TextColorCode code)
Output stream operator to change the color of subsequent stream output.
Definition: text_style.h:65


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Mon Feb 28 2022 22:07:52