C++ Utilities 5.27.0
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
22bool 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:
38 stream << "Error: ";
41 break;
44 stream << "Warning: ";
47 break;
48 case Phrases::End:
50 stream << '\n';
51 break;
53 stream << " ";
56 break;
59 stream << "==> ";
62 break;
65 stream << " -> ";
68 break;
71 stream << "==> ERROR: ";
74 break;
77 stream << "==> WARNING: ";
80 break;
83 stream << std::endl;
84 break;
85 case Phrases::Info:
87 stream << "Info: ";
90 break;
92 eraseLine(stream);
93 stream << '\r';
94 break;
97 stream << " -> ERROR: ";
100 break;
103 stream << " -> WARNING: ";
106 break;
109 stream << "==> ";
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;
129 return " "sv;
131 return "==> "sv;
133 return " -> "sv;
135 return "==> ERROR: "sv;
137 return "==> WARNING: ";
138 case Phrases::Info:
139 return "Info: "sv;
141 return " -> ERROR: "sv;
143 return " -> WARNING: "sv;
145 return "==> "sv;
146 case Phrases::End:
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;
171 return " \033[0m\033[1m"sv;
173 return "\033[1;32m==> \033[0m\033[1m"sv;
175 return "\033[1;32m -> \033[0m\033[1m"sv;
177 return "\033[1;31m==> ERROR: \033[0m\033[1m"sv;
179 return "\033[1;33m==> WARNING: \033[0m\033[1m";
180 case Phrases::Info:
181 return "\033[1;34mInfo: \033[0m\033[1m"sv;
183 return "\033[1;31m -> ERROR: \033[0m\033[1m"sv;
185 return "\033[1;33m -> WARNING: \033[0m\033[1m"sv;
187 return "\033[1;37m==> \033[0m\033[1m"sv;
188 case Phrases::End:
190 return "\033[0m\n";
191 default:
192 return std::string_view{};
193 }
194}
195
196} // namespace EscapeCodes
197
198} // namespace CppUtilities
CPP_UTILITIES_EXPORT std::string_view phraseString(Phrases phrase)
Returns a string for the specified phrase without formatting.
void eraseLine(std::ostream &stream)
std::ostream & operator<<(std::ostream &stream, TextAttribute displayAttribute)
CPP_UTILITIES_EXPORT bool enabled
Controls whether the functions inside the EscapeCodes namespace actually make use of escape codes.
void setStyle(std::ostream &stream, TextAttribute displayAttribute=TextAttribute::Reset)
CPP_UTILITIES_EXPORT std::string_view formattedPhraseString(Phrases phrase)
Returns a string for the specified phrase which is formatted using ANSI escape codes.
Phrases
The Phrases enum contains standard phrases which can be printed to any std::ostream and obtained as s...
Contains all utilities provides by the c++utilities library.