Debian Codegraph @

io/ansiescapecodes.h

raw symbols SCIP
1 #ifndef IOUTILITIES_ANSIESCAPECODES
2 #define IOUTILITIES_ANSIESCAPECODES
3
4 #include "../global.h"
5 #include "../misc/traits.h"
6
7 #include <ostream>
8 #include <string_view>
9 #include <tuple>
10
11 namespace CppUtilities {
12 namespace EscapeCodes {
13
14 extern CPP_UTILITIES_EXPORT bool enabled;
15
16 enum class Color : char { Black = '0', Red, Green, Yellow, Blue, Purple, Cyan, White };
17
18 enum class ColorContext : char { Foreground = '3', Background = '4' };
19
20 enum class TextAttribute : char {
21 Reset = '0',
22 Bold = '1',
23 Dim = '2',
24 Italic = '3',
25 Underscore = '4',
26 Blink = '5',
27 ReverseVideo = '7',
28 Concealed = '8',
29 Strikethrough = '9',
30 };
31
32 enum class Direction : char { Up = 'A', Down = 'B', Forward = 'C', Backward = 'D' };
33
34 inline void setStyle(std::ostream &stream, TextAttribute displayAttribute = TextAttribute::Reset)
35 {
36 if (enabled) {
37 stream << '\033' << '[' << static_cast<char>(displayAttribute) << 'm';
38 }
39 }
40
41 inline void setStyle(
42 std::ostream &stream, Color color, ColorContext context = ColorContext::Foreground, TextAttribute displayAttribute = TextAttribute::Reset)
43 {
44 if (enabled) {
45 stream << '\033' << '[' << static_cast<char>(displayAttribute) << ';' << static_cast<char>(context) << static_cast<char>(color) << 'm';
46 }
47 }
48
49 inline void setStyle(std::ostream &stream, Color foregroundColor, Color backgroundColor, TextAttribute displayAttribute = TextAttribute::Reset)
50 {
51 if (enabled) {
52 stream << '\033' << '[' << static_cast<char>(displayAttribute) << ';' << static_cast<char>(ColorContext::Foreground)
53 << static_cast<char>(foregroundColor) << ';' << static_cast<char>(ColorContext::Background) << static_cast<char>(backgroundColor)
54 << 'm';
55 }
56 }
57
58 inline void resetStyle(std::ostream &stream)
59 {
60 if (enabled) {
61 stream << '\033' << '[' << static_cast<char>(TextAttribute::Reset) << 'm';
62 }
63 }
64
65 inline void setCursor(std::ostream &stream, unsigned int row = 0, unsigned int col = 0)
66 {
67 if (enabled) {
68 stream << '\033' << '[' << row << ';' << col << 'H';
69 }
70 }
71
72 inline void moveCursor(std::ostream &stream, unsigned int cells, Direction direction)
73 {
74 if (enabled) {
75 stream << '\033' << '[' << cells << static_cast<char>(direction);
76 }
77 }
78
79 inline void saveCursor(std::ostream &stream)
80 {
81 if (enabled) {
82 stream << "\033[s";
83 }
84 }
85
86 inline void restoreCursor(std::ostream &stream)
87 {
88 if (enabled) {
89 stream << "\033[u";
90 }
91 }
92
93 inline void eraseDisplay(std::ostream &stream)
94 {
95 if (enabled) {
96 stream << "\033[2J";
97 }
98 }
99
100 inline void eraseLine(std::ostream &stream)
101 {
102 if (enabled) {
103 stream << "\33[2K";
104 }
105 }
106
107 inline std::ostream &operator<<(std::ostream &stream, TextAttribute displayAttribute)
108 {
109 setStyle(stream, displayAttribute);
110 return stream;
111 }
112
113 constexpr auto color(Color foreground, Color background, TextAttribute displayAttribute = TextAttribute::Reset)
114 {
115 return std::make_tuple(foreground, background, displayAttribute);
116 }
117
118 constexpr auto color(Color foreground, ColorContext context, TextAttribute displayAttribute = TextAttribute::Reset)
119 {
120 return std::make_tuple(foreground, context, displayAttribute);
121 }
122
123 template <typename TupleType,
124 Traits::EnableIfAny<std::is_same<TupleType, std::tuple<Color, Color, TextAttribute>>,
125 std::is_same<TupleType, std::tuple<Color, ColorContext, TextAttribute>>> * = nullptr>
126 inline std::ostream &operator<<(std::ostream &stream, TupleType displayAttribute)
127 {
128 setStyle(stream, std::get<0>(displayAttribute), std::get<1>(displayAttribute), std::get<2>(displayAttribute));
129 return stream;
130 }
131
132 /*!
133 * \brief The Phrases enum contains standard phrases which can be printed to any std::ostream and obtained as strings
134 * via EscapeCodes::phraseString() and EscapeCodes::formattedPhraseString().
135 *
136 * Example: `std::cerr << Phrases::Error << "Something bad happened." << Phrases::End`
137 */
138 enum class Phrases {
139 Error, /**< bold, red "Error: " */
140 Warning, /**< bold, yellow "Warning: " */
141 End, /**< resets the style */
142 PlainMessage, /**< bold, 4 spaces " " */
143 SuccessMessage, /**< bold, green "==> " */
144 SubMessage, /**< bold, green " -> " */
145 ErrorMessage, /**< bold, red "==> ERROR: " */
146 WarningMessage, /**< bold, yellow "==> WARNING: " */
147 EndFlush, /**< resets the style and flushes the stream */
148 Info, /**< bold, blue "Info: " */
149 Override, /**< erases the current line */
150 SubError, /**< bold, red " -> ERROR: " */
151 SubWarning, /**< bold, yellow " -> WARNING: " */
152 InfoMessage, /**< bold, white "==> " */
153 };
154 CPP_UTILITIES_EXPORT std::ostream &operator<<(std::ostream &stream, Phrases phrase);
155 CPP_UTILITIES_EXPORT std::string_view phraseString(Phrases phrase);
156 CPP_UTILITIES_EXPORT std::string_view formattedPhraseString(Phrases phrase);
157
158 } // namespace EscapeCodes
159 } // namespace CppUtilities
160
161 #endif // IOUTILITIES_ANSIESCAPECODES