Browse Source

Add –formattedPhraseString()

charconv
Martchus 2 years ago
parent
commit
c6095fbaf8
  1. 4
      CMakeLists.txt
  2. 45
      io/ansiescapecodes.cpp
  3. 4
      io/ansiescapecodes.h

4
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)

45
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

4
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

Loading…
Cancel
Save