Add –formattedPhraseString()

This commit is contained in:
Martchus 2020-11-18 00:37:43 +01:00
parent a40ee3aa7e
commit c6095fbaf8
3 changed files with 49 additions and 4 deletions

View File

@ -113,8 +113,8 @@ set(META_APP_URL "https://github.com/${META_APP_AUTHOR}/${META_PROJECT_NAME}")
set(META_APP_DESCRIPTION "Useful C++ classes and routines such as argument parser, IO and conversion utilities")
set(META_FEATURES_FOR_COMPILER_DETECTION_HEADER cxx_thread_local)
set(META_VERSION_MAJOR 5)
set(META_VERSION_MINOR 8)
set(META_VERSION_PATCH 1)
set(META_VERSION_MINOR 9)
set(META_VERSION_PATCH 0)
# find required 3rd party libraries
include(3rdParty)

View File

@ -28,7 +28,7 @@ bool enabled =
;
/*!
* \brief Prints the specified \a phrase.
* \brief Prints the specified \a phrase in a formatted manner using ANSI escape codes.
*/
std::ostream &operator<<(std::ostream &stream, Phrases phrase)
{
@ -114,6 +114,9 @@ std::ostream &operator<<(std::ostream &stream, Phrases phrase)
return stream;
}
/*!
* \brief Returns a string for the specified \a phrase *without* formatting.
*/
std::string_view phraseString(Phrases phrase)
{
using namespace std::string_view_literals;
@ -148,6 +151,46 @@ std::string_view phraseString(Phrases phrase)
}
}
/*!
* \brief Returns a string for the specified \a phrase which is formatted using ANSI escape codes.
*/
std::string_view formattedPhraseString(Phrases phrase)
{
if (!enabled) {
return phraseString(phrase);
}
using namespace std::string_view_literals;
switch (phrase) {
case Phrases::Error:
return "\e[1;31mError: \e[0m\e[1m"sv;
case Phrases::Warning:
return "\e[1;33mWarning: \e[0m\e[1m"sv;
case Phrases::PlainMessage:
return " \e[0m\e[1m"sv;
case Phrases::SuccessMessage:
return "\e[1;32m==> \e[0m\e[1m"sv;
case Phrases::SubMessage:
return "\e[1;32m -> \e[0m\e[1m"sv;
case Phrases::ErrorMessage:
return "\e[1;31m==> ERROR: \e[0m\e[1m"sv;
case Phrases::WarningMessage:
return "\e[1;33m==> WARNING: \e[0m\e[1m";
case Phrases::Info:
return "\e[1;34mInfo: \e[0m\e[1m"sv;
case Phrases::SubError:
return "\e[1;31m -> ERROR: \e[0m\e[1m"sv;
case Phrases::SubWarning:
return "\e[1;33m -> WARNING: \e[0m\e[1m"sv;
case Phrases::InfoMessage:
return "\e[1;37m==> \e[0m\e[1m"sv;
case Phrases::End:
case Phrases::EndFlush:
return "\e[0m\n";
default:
return std::string_view{};
}
}
} // namespace EscapeCodes
} // namespace CppUtilities

View File

@ -130,7 +130,8 @@ inline std::ostream &operator<<(std::ostream &stream, TupleType displayAttribute
}
/*!
* \brief The Phrases enum contains standard phrases which can be printed to any std::ostream.
* \brief The Phrases enum contains standard phrases which can be printed to any std::ostream and obtained as strings
* via EscapeCodes::phraseString() and EscapeCodes::formattedPhraseString().
*
* Example: `std::cerr << Phrases::Error << "Something bad happened." << Phrases::End`
*/
@ -152,6 +153,7 @@ enum class Phrases {
};
CPP_UTILITIES_EXPORT std::ostream &operator<<(std::ostream &stream, Phrases phrase);
CPP_UTILITIES_EXPORT std::string_view phraseString(Phrases phrase);
CPP_UTILITIES_EXPORT std::string_view formattedPhraseString(Phrases phrase);
} // namespace EscapeCodes
} // namespace CppUtilities