C++ Utilities: io/ansiescapecodes.cpp Source File

C++ Utilities 5.32.1 Useful C++ classes and routines such as argument parser, IO and conversion utilities

Loading...
Searching...
No Matches
ansiescapecodes.cpp
Go to the documentation of this file.
1#include "./ansiescapecodes.h"
2
3namespace CppUtilities {
4
8namespace EscapeCodes {
9
22 bool enabled =
23#ifdef CPP_UTILITIES_ESCAPE_CODES_ENABLED_BY_DEFAULT
24 true
25#else
26 false
27#endif
28 ;
29
33std::ostream &operator<<(std::ostream &stream, Phrases phrase)
34{
35 switch (phrase) {
36 case Phrases::Error:
37 setStyle(stream, Color::Red, ColorContext::Foreground, TextAttribute::Bold);
38 stream << "Error: ";
39 setStyle(stream, TextAttribute::Reset);
40 setStyle(stream, TextAttribute::Bold);
41 break;
42 case Phrases::Warning:
43 setStyle(stream, Color::Yellow, ColorContext::Foreground, TextAttribute::Bold);
44 stream << "Warning: ";
45 setStyle(stream, TextAttribute::Reset);
46 setStyle(stream, TextAttribute::Bold);
47 break;
48 case Phrases::End:
49 setStyle(stream, TextAttribute::Reset);
50 stream << '\n';
51 break;
52 case Phrases::PlainMessage:
53 stream << " ";
54 setStyle(stream, TextAttribute::Reset);
55 setStyle(stream, TextAttribute::Bold);
56 break;
57 case Phrases::SuccessMessage:
58 setStyle(stream, Color::Green, ColorContext::Foreground, TextAttribute::Bold);
59 stream << "==> ";
60 setStyle(stream, TextAttribute::Reset);
61 setStyle(stream, TextAttribute::Bold);
62 break;
63 case Phrases::SubMessage:
64 setStyle(stream, Color::Green, ColorContext::Foreground, TextAttribute::Bold);
65 stream << " -> ";
66 setStyle(stream, TextAttribute::Reset);
67 setStyle(stream, TextAttribute::Bold);
68 break;
69 case Phrases::ErrorMessage:
70 setStyle(stream, Color::Red, ColorContext::Foreground, TextAttribute::Bold);
71 stream << "==> ERROR: ";
72 setStyle(stream, TextAttribute::Reset);
73 setStyle(stream, TextAttribute::Bold);
74 break;
75 case Phrases::WarningMessage:
76 setStyle(stream, Color::Yellow, ColorContext::Foreground, TextAttribute::Bold);
77 stream << "==> WARNING: ";
78 setStyle(stream, TextAttribute::Reset);
79 setStyle(stream, TextAttribute::Bold);
80 break;
81 case Phrases::EndFlush:
82 setStyle(stream, TextAttribute::Reset);
83 stream << std::endl;
84 break;
85 case Phrases::Info:
86 setStyle(stream, Color::Blue, ColorContext::Foreground, TextAttribute::Bold);
87 stream << "Info: ";
88 setStyle(stream, TextAttribute::Reset);
89 setStyle(stream, TextAttribute::Bold);
90 break;
91 case Phrases::Override:
92 eraseLine(stream);
93 stream << '\r';
94 break;
95 case Phrases::SubError:
96 setStyle(stream, Color::Red, ColorContext::Foreground, TextAttribute::Bold);
97 stream << " -> ERROR: ";
98 setStyle(stream, TextAttribute::Reset);
99 setStyle(stream, TextAttribute::Bold);
100 break;
101 case Phrases::SubWarning:
102 setStyle(stream, Color::Yellow, ColorContext::Foreground, TextAttribute::Bold);
103 stream << " -> WARNING: ";
104 setStyle(stream, TextAttribute::Reset);
105 setStyle(stream, TextAttribute::Bold);
106 break;
107 case Phrases::InfoMessage:
108 setStyle(stream, Color::White, ColorContext::Foreground, TextAttribute::Bold);
109 stream << "==> ";
110 setStyle(stream, TextAttribute::Reset);
111 setStyle(stream, TextAttribute::Bold);
112 break;
113 }
114 return stream;
115}
116
120std::string_view phraseString(Phrases phrase)
121{
122 using namespace std::string_view_literals;
123 switch (phrase) {
124 case Phrases::Error:
125 return "Error: "sv;
126 case Phrases::Warning:
127 return "Warning: "sv;
128 case Phrases::PlainMessage:
129 return " "sv;
130 case Phrases::SuccessMessage:
131 return "==> "sv;
132 case Phrases::SubMessage:
133 return " -> "sv;
134 case Phrases::ErrorMessage:
135 return "==> ERROR: "sv;
136 case Phrases::WarningMessage:
137 return "==> WARNING: ";
138 case Phrases::Info:
139 return "Info: "sv;
140 case Phrases::SubError:
141 return " -> ERROR: "sv;
142 case Phrases::SubWarning:
143 return " -> WARNING: "sv;
144 case Phrases::InfoMessage:
145 return "==> "sv;
146 case Phrases::End:
147 case Phrases::EndFlush:
148 return "\n";
149 default:
150 return std::string_view{};
151 }
152}
153
159std::string_view formattedPhraseString(Phrases phrase)
160{
161 if (!enabled) {
162 return phraseString(phrase);
163 }
164 using namespace std::string_view_literals;
165 switch (phrase) {
166 case Phrases::Error:
167 return "\033[1;31mError: \033[0m\033[1m"sv;
168 case Phrases::Warning:
169 return "\033[1;33mWarning: \033[0m\033[1m"sv;
170 case Phrases::PlainMessage:
171 return " \033[0m\033[1m"sv;
172 case Phrases::SuccessMessage:
173 return "\033[1;32m==> \033[0m\033[1m"sv;
174 case Phrases::SubMessage:
175 return "\033[1;32m -> \033[0m\033[1m"sv;
176 case Phrases::ErrorMessage:
177 return "\033[1;31m==> ERROR: \033[0m\033[1m"sv;
178 case Phrases::WarningMessage:
179 return "\033[1;33m==> WARNING: \033[0m\033[1m";
180 case Phrases::Info:
181 return "\033[1;34mInfo: \033[0m\033[1m"sv;
182 case Phrases::SubError:
183 return "\033[1;31m -> ERROR: \033[0m\033[1m"sv;
184 case Phrases::SubWarning:
185 return "\033[1;33m -> WARNING: \033[0m\033[1m"sv;
186 case Phrases::InfoMessage:
187 return "\033[1;37m==> \033[0m\033[1m"sv;
188 case Phrases::End:
189 case Phrases::EndFlush:
190 return "\033[0m\n";
191 default:
192 return std::string_view{};
193 }
194}
195
196} // namespace EscapeCodes
197
198} // namespace CppUtilities
ansiescapecodes.h
CppUtilities::EscapeCodes
Encapsulates functions for formatted terminal output using ANSI escape codes.
Definition ansiescapecodes.h:12
CppUtilities::EscapeCodes::Color::White
@ White
Definition ansiescapecodes.h:16
CppUtilities::EscapeCodes::Color::Yellow
@ Yellow
Definition ansiescapecodes.h:16
CppUtilities::EscapeCodes::Color::Blue
@ Blue
Definition ansiescapecodes.h:16
CppUtilities::EscapeCodes::Color::Green
@ Green
Definition ansiescapecodes.h:16
CppUtilities::EscapeCodes::Color::Red
@ Red
Definition ansiescapecodes.h:16
CppUtilities::EscapeCodes::TextAttribute::Bold
@ Bold
Definition ansiescapecodes.h:22
CppUtilities::EscapeCodes::TextAttribute::Reset
@ Reset
Definition ansiescapecodes.h:21
CppUtilities::EscapeCodes::phraseString
CPP_UTILITIES_EXPORT std::string_view phraseString(Phrases phrase)
Returns a string for the specified phrase without formatting.
Definition ansiescapecodes.cpp:120
CppUtilities::EscapeCodes::eraseLine
void eraseLine(std::ostream &stream)
Definition ansiescapecodes.h:100
CppUtilities::EscapeCodes::operator<<
std::ostream & operator<<(std::ostream &stream, TextAttribute displayAttribute)
Definition ansiescapecodes.h:107
CppUtilities::EscapeCodes::enabled
CPP_UTILITIES_EXPORT bool enabled
Controls whether the functions inside the EscapeCodes namespace actually make use of escape codes.
Definition ansiescapecodes.cpp:22
CppUtilities::EscapeCodes::setStyle
void setStyle(std::ostream &stream, TextAttribute displayAttribute=TextAttribute::Reset)
Definition ansiescapecodes.h:34
CppUtilities::EscapeCodes::formattedPhraseString
CPP_UTILITIES_EXPORT std::string_view formattedPhraseString(Phrases phrase)
Returns a string for the specified phrase which is formatted using ANSI escape codes.
Definition ansiescapecodes.cpp:159
CppUtilities::EscapeCodes::Phrases
Phrases
The Phrases enum contains standard phrases which can be printed to any std::ostream and obtained as s...
Definition ansiescapecodes.h:138
CppUtilities::EscapeCodes::Phrases::SubError
@ SubError
Definition ansiescapecodes.h:150
CppUtilities::EscapeCodes::Phrases::Warning
@ Warning
Definition ansiescapecodes.h:140
CppUtilities::EscapeCodes::Phrases::InfoMessage
@ InfoMessage
Definition ansiescapecodes.h:152
CppUtilities::EscapeCodes::Phrases::Info
@ Info
Definition ansiescapecodes.h:148
CppUtilities::EscapeCodes::Phrases::PlainMessage
@ PlainMessage
Definition ansiescapecodes.h:142
CppUtilities::EscapeCodes::Phrases::Override
@ Override
Definition ansiescapecodes.h:149
CppUtilities::EscapeCodes::Phrases::End
@ End
Definition ansiescapecodes.h:141
CppUtilities::EscapeCodes::Phrases::Error
@ Error
Definition ansiescapecodes.h:139
CppUtilities::EscapeCodes::Phrases::SubMessage
@ SubMessage
Definition ansiescapecodes.h:144
CppUtilities::EscapeCodes::Phrases::SuccessMessage
@ SuccessMessage
Definition ansiescapecodes.h:143
CppUtilities::EscapeCodes::Phrases::SubWarning
@ SubWarning
Definition ansiescapecodes.h:151
CppUtilities::EscapeCodes::Phrases::ErrorMessage
@ ErrorMessage
Definition ansiescapecodes.h:145
CppUtilities::EscapeCodes::Phrases::EndFlush
@ EndFlush
Definition ansiescapecodes.h:147
CppUtilities::EscapeCodes::Phrases::WarningMessage
@ WarningMessage
Definition ansiescapecodes.h:146
CppUtilities::EscapeCodes::ColorContext::Foreground
@ Foreground
Definition ansiescapecodes.h:18
CppUtilities
Contains all utilities provided by the c++utilities library.
Definition argumentparser.h:18