Avoid use of non-standard escape character to avoid MSVC warning about it

This commit is contained in:
Martchus 2023-03-26 21:49:54 +02:00
parent bcd5816d23
commit 2a123df86d
4 changed files with 38 additions and 38 deletions

View File

@ -164,30 +164,30 @@ std::string_view formattedPhraseString(Phrases phrase)
using namespace std::string_view_literals; using namespace std::string_view_literals;
switch (phrase) { switch (phrase) {
case Phrases::Error: case Phrases::Error:
return "\e[1;31mError: \e[0m\e[1m"sv; return "\033[1;31mError: \033[0m\033[1m"sv;
case Phrases::Warning: case Phrases::Warning:
return "\e[1;33mWarning: \e[0m\e[1m"sv; return "\033[1;33mWarning: \033[0m\033[1m"sv;
case Phrases::PlainMessage: case Phrases::PlainMessage:
return " \e[0m\e[1m"sv; return " \033[0m\033[1m"sv;
case Phrases::SuccessMessage: case Phrases::SuccessMessage:
return "\e[1;32m==> \e[0m\e[1m"sv; return "\033[1;32m==> \033[0m\033[1m"sv;
case Phrases::SubMessage: case Phrases::SubMessage:
return "\e[1;32m -> \e[0m\e[1m"sv; return "\033[1;32m -> \033[0m\033[1m"sv;
case Phrases::ErrorMessage: case Phrases::ErrorMessage:
return "\e[1;31m==> ERROR: \e[0m\e[1m"sv; return "\033[1;31m==> ERROR: \033[0m\033[1m"sv;
case Phrases::WarningMessage: case Phrases::WarningMessage:
return "\e[1;33m==> WARNING: \e[0m\e[1m"; return "\033[1;33m==> WARNING: \033[0m\033[1m";
case Phrases::Info: case Phrases::Info:
return "\e[1;34mInfo: \e[0m\e[1m"sv; return "\033[1;34mInfo: \033[0m\033[1m"sv;
case Phrases::SubError: case Phrases::SubError:
return "\e[1;31m -> ERROR: \e[0m\e[1m"sv; return "\033[1;31m -> ERROR: \033[0m\033[1m"sv;
case Phrases::SubWarning: case Phrases::SubWarning:
return "\e[1;33m -> WARNING: \e[0m\e[1m"sv; return "\033[1;33m -> WARNING: \033[0m\033[1m"sv;
case Phrases::InfoMessage: case Phrases::InfoMessage:
return "\e[1;37m==> \e[0m\e[1m"sv; return "\033[1;37m==> \033[0m\033[1m"sv;
case Phrases::End: case Phrases::End:
case Phrases::EndFlush: case Phrases::EndFlush:
return "\e[0m\n"; return "\033[0m\n";
default: default:
return std::string_view{}; return std::string_view{};
} }

View File

@ -34,7 +34,7 @@ enum class Direction : char { Up = 'A', Down = 'B', Forward = 'C', Backward = 'D
inline void setStyle(std::ostream &stream, TextAttribute displayAttribute = TextAttribute::Reset) inline void setStyle(std::ostream &stream, TextAttribute displayAttribute = TextAttribute::Reset)
{ {
if (enabled) { if (enabled) {
stream << '\e' << '[' << static_cast<char>(displayAttribute) << 'm'; stream << '\033' << '[' << static_cast<char>(displayAttribute) << 'm';
} }
} }
@ -42,14 +42,14 @@ inline void setStyle(
std::ostream &stream, Color color, ColorContext context = ColorContext::Foreground, TextAttribute displayAttribute = TextAttribute::Reset) std::ostream &stream, Color color, ColorContext context = ColorContext::Foreground, TextAttribute displayAttribute = TextAttribute::Reset)
{ {
if (enabled) { if (enabled) {
stream << '\e' << '[' << static_cast<char>(displayAttribute) << ';' << static_cast<char>(context) << static_cast<char>(color) << 'm'; stream << '\033' << '[' << static_cast<char>(displayAttribute) << ';' << static_cast<char>(context) << static_cast<char>(color) << 'm';
} }
} }
inline void setStyle(std::ostream &stream, Color foregroundColor, Color backgroundColor, TextAttribute displayAttribute = TextAttribute::Reset) inline void setStyle(std::ostream &stream, Color foregroundColor, Color backgroundColor, TextAttribute displayAttribute = TextAttribute::Reset)
{ {
if (enabled) { if (enabled) {
stream << '\e' << '[' << static_cast<char>(displayAttribute) << ';' << static_cast<char>(ColorContext::Foreground) stream << '\033' << '[' << static_cast<char>(displayAttribute) << ';' << static_cast<char>(ColorContext::Foreground)
<< static_cast<char>(foregroundColor) << ';' << static_cast<char>(ColorContext::Background) << static_cast<char>(backgroundColor) << static_cast<char>(foregroundColor) << ';' << static_cast<char>(ColorContext::Background) << static_cast<char>(backgroundColor)
<< 'm'; << 'm';
} }
@ -58,42 +58,42 @@ inline void setStyle(std::ostream &stream, Color foregroundColor, Color backgrou
inline void resetStyle(std::ostream &stream) inline void resetStyle(std::ostream &stream)
{ {
if (enabled) { if (enabled) {
stream << '\e' << '[' << static_cast<char>(TextAttribute::Reset) << 'm'; stream << '\033' << '[' << static_cast<char>(TextAttribute::Reset) << 'm';
} }
} }
inline void setCursor(std::ostream &stream, unsigned int row = 0, unsigned int col = 0) inline void setCursor(std::ostream &stream, unsigned int row = 0, unsigned int col = 0)
{ {
if (enabled) { if (enabled) {
stream << '\e' << '[' << row << ';' << col << 'H'; stream << '\033' << '[' << row << ';' << col << 'H';
} }
} }
inline void moveCursor(std::ostream &stream, unsigned int cells, Direction direction) inline void moveCursor(std::ostream &stream, unsigned int cells, Direction direction)
{ {
if (enabled) { if (enabled) {
stream << '\e' << '[' << cells << static_cast<char>(direction); stream << '\033' << '[' << cells << static_cast<char>(direction);
} }
} }
inline void saveCursor(std::ostream &stream) inline void saveCursor(std::ostream &stream)
{ {
if (enabled) { if (enabled) {
stream << "\e[s"; stream << "\033[s";
} }
} }
inline void restoreCursor(std::ostream &stream) inline void restoreCursor(std::ostream &stream)
{ {
if (enabled) { if (enabled) {
stream << "\e[u"; stream << "\033[u";
} }
} }
inline void eraseDisplay(std::ostream &stream) inline void eraseDisplay(std::ostream &stream)
{ {
if (enabled) { if (enabled) {
stream << "\e[2J"; stream << "\033[2J";
} }
} }

View File

@ -816,30 +816,30 @@ void ArgumentParserTests::testHelp()
// parse args and assert output // parse args and assert output
const char *const argv[] = { "app", "-h" }; const char *const argv[] = { "app", "-h" };
{ {
const OutputCheck c("\e[1m" APP_NAME ", version " APP_VERSION "\n" const OutputCheck c("\033[1m" APP_NAME ", version " APP_VERSION "\n"
"\n" "\n"
"\e[0m" APP_DESCRIPTION "\n" "\033[0m" APP_DESCRIPTION "\n"
"\n" "\n"
"Available operations:\n" "Available operations:\n"
"\e[1mverbose, -v\e[0m\n" "\033[1mverbose, -v\033[0m\n"
" be verbose\n" " be verbose\n"
" example: actually not an operation\n" " example: actually not an operation\n"
"\n" "\n"
"Available top-level options:\n" "Available top-level options:\n"
"\e[1m--files, -f\e[0m\n" "\033[1m--files, -f\033[0m\n"
" specifies the path of the file(s) to be opened\n" " specifies the path of the file(s) to be opened\n"
" \e[1m--sub\e[0m\n" " \033[1m--sub\033[0m\n"
" sub arg\n" " sub arg\n"
" particularities: mandatory if parent argument is present\n" " particularities: mandatory if parent argument is present\n"
" \e[1m--nested-sub\e[0m [value1] [value2] ...\n" " \033[1m--nested-sub\033[0m [value1] [value2] ...\n"
" nested sub arg\n" " nested sub arg\n"
" example: sub arg example\n" " example: sub arg example\n"
"\n" "\n"
"\e[1m--env\e[0m [file] [value 2]\n" "\033[1m--env\033[0m [file] [value 2]\n"
" env\n" " env\n"
" default environment variable: FILES\n" " default environment variable: FILES\n"
"\n" "\n"
"\e[1m--no-color\e[0m\n" "\033[1m--no-color\033[0m\n"
" disables formatted/colorized output\n" " disables formatted/colorized output\n"
" default environment variable: ENABLE_ESCAPE_CODES\n" " default environment variable: ENABLE_ESCAPE_CODES\n"
"\n" "\n"

View File

@ -650,15 +650,15 @@ void IoTests::testAnsiEscapeCodes()
<< "blue, blinking text on red background" << EscapeCodes::TextAttribute::Reset << '\n'; << "blue, blinking text on red background" << EscapeCodes::TextAttribute::Reset << '\n';
cout << "\noutput for formatting with ANSI escape codes:\n" << ss1.str() << "---------------------------------------------\n"; cout << "\noutput for formatting with ANSI escape codes:\n" << ss1.str() << "---------------------------------------------\n";
fstream("/tmp/test.txt", ios_base::out | ios_base::trunc) << ss1.str(); fstream("/tmp/test.txt", ios_base::out | ios_base::trunc) << ss1.str();
CPPUNIT_ASSERT_EQUAL("\e[1;31mError: \e[0m\e[1msome error\e[0m\n" CPPUNIT_ASSERT_EQUAL("\033[1;31mError: \033[0m\033[1msome error\033[0m\n"
"\e[1;33mWarning: \e[0m\e[1msome warning\e[0m\n" "\033[1;33mWarning: \033[0m\033[1msome warning\033[0m\n"
"\e[1;34mInfo: \e[0m\e[1msome info\e[0m\n" "\033[1;34mInfo: \033[0m\033[1msome info\033[0m\n"
"\e[1;31m==> ERROR: \e[0m\e[1mArch-style error\e[0m\n" "\033[1;31m==> ERROR: \033[0m\033[1mArch-style error\033[0m\n"
"\e[1;33m==> WARNING: \e[0m\e[1mArch-style warning\e[0m\n" "\033[1;33m==> WARNING: \033[0m\033[1mArch-style warning\033[0m\n"
" \e[0m\e[1mArch-style message\e[0m\n" " \033[0m\033[1mArch-style message\033[0m\n"
"\e[1;32m==> \e[0m\e[1mArch-style success\e[0m\n" "\033[1;32m==> \033[0m\033[1mArch-style success\033[0m\n"
"\e[1;32m -> \e[0m\e[1mArch-style sub-message\e[0m\n" "\033[1;32m -> \033[0m\033[1mArch-style sub-message\033[0m\n"
"\e[5;34;41mblue, blinking text on red background\e[0m\n"s, "\033[5;34;41mblue, blinking text on red background\033[0m\n"s,
ss1.str()); ss1.str());
stringstream ss2; stringstream ss2;