From c6095fbaf8737e344a12301a7300c19253c1b66a Mon Sep 17 00:00:00 2001 From: Martchus Date: Wed, 18 Nov 2020 00:37:43 +0100 Subject: [PATCH] =?UTF-8?q?Add=20=C2=96formattedPhraseString()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 4 ++-- io/ansiescapecodes.cpp | 45 +++++++++++++++++++++++++++++++++++++++++- io/ansiescapecodes.h | 4 +++- 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6244b1b..9e320ed 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/io/ansiescapecodes.cpp b/io/ansiescapecodes.cpp index 5b041df..c0a484f 100644 --- a/io/ansiescapecodes.cpp +++ b/io/ansiescapecodes.cpp @@ -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 diff --git a/io/ansiescapecodes.h b/io/ansiescapecodes.h index f6dd74f..8898ecf 100644 --- a/io/ansiescapecodes.h +++ b/io/ansiescapecodes.h @@ -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