cpp-utilities/io/ansiescapecodes.h

162 lines
4.8 KiB
C
Raw Permalink Normal View History

#ifndef IOUTILITIES_ANSIESCAPECODES
#define IOUTILITIES_ANSIESCAPECODES
2015-04-22 18:36:40 +02:00
#include "../global.h"
#include "../misc/traits.h"
2015-04-22 18:36:40 +02:00
#include <ostream>
#include <string_view>
2017-09-22 00:22:08 +02:00
#include <tuple>
2015-04-22 18:36:40 +02:00
namespace CppUtilities {
2015-04-22 18:36:40 +02:00
namespace EscapeCodes {
2017-10-17 00:00:46 +02:00
extern CPP_UTILITIES_EXPORT bool enabled;
2017-05-01 03:13:11 +02:00
enum class Color : char { Black = '0', Red, Green, Yellow, Blue, Purple, Cyan, White };
2015-04-22 18:36:40 +02:00
2017-05-01 03:13:11 +02:00
enum class ColorContext : char { Foreground = '3', Background = '4' };
2015-04-22 18:36:40 +02:00
2017-05-01 03:13:11 +02:00
enum class TextAttribute : char {
2015-04-22 18:36:40 +02:00
Reset = '0',
Bold = '1',
Dim = '2',
Italic = '3',
Underscore = '4',
Blink = '5',
ReverseVideo = '7',
2018-03-25 20:00:01 +02:00
Concealed = '8',
Strikethrough = '9',
2015-04-22 18:36:40 +02:00
};
2017-05-01 03:13:11 +02:00
enum class Direction : char { Up = 'A', Down = 'B', Forward = 'C', Backward = 'D' };
2015-04-22 18:36:40 +02:00
inline void setStyle(std::ostream &stream, TextAttribute displayAttribute = TextAttribute::Reset)
{
2017-10-17 00:00:46 +02:00
if (enabled) {
stream << '\033' << '[' << static_cast<char>(displayAttribute) << 'm';
2017-10-17 00:00:46 +02:00
}
}
2017-05-01 03:13:11 +02:00
inline void setStyle(
std::ostream &stream, Color color, ColorContext context = ColorContext::Foreground, TextAttribute displayAttribute = TextAttribute::Reset)
2015-04-22 18:36:40 +02:00
{
2017-10-17 00:00:46 +02:00
if (enabled) {
stream << '\033' << '[' << static_cast<char>(displayAttribute) << ';' << static_cast<char>(context) << static_cast<char>(color) << 'm';
2017-10-17 00:00:46 +02:00
}
2015-04-22 18:36:40 +02:00
}
2017-05-01 03:13:11 +02:00
inline void setStyle(std::ostream &stream, Color foregroundColor, Color backgroundColor, TextAttribute displayAttribute = TextAttribute::Reset)
2015-04-22 18:36:40 +02:00
{
2017-10-17 00:00:46 +02:00
if (enabled) {
stream << '\033' << '[' << static_cast<char>(displayAttribute) << ';' << static_cast<char>(ColorContext::Foreground)
2018-01-29 16:26:28 +01:00
<< static_cast<char>(foregroundColor) << ';' << static_cast<char>(ColorContext::Background) << static_cast<char>(backgroundColor)
2017-10-17 00:00:46 +02:00
<< 'm';
}
2015-04-22 18:36:40 +02:00
}
inline void resetStyle(std::ostream &stream)
{
2017-10-17 00:00:46 +02:00
if (enabled) {
stream << '\033' << '[' << static_cast<char>(TextAttribute::Reset) << 'm';
2017-10-17 00:00:46 +02:00
}
2015-04-22 18:36:40 +02:00
}
inline void setCursor(std::ostream &stream, unsigned int row = 0, unsigned int col = 0)
{
2017-10-17 00:00:46 +02:00
if (enabled) {
stream << '\033' << '[' << row << ';' << col << 'H';
2017-10-17 00:00:46 +02:00
}
2015-04-22 18:36:40 +02:00
}
inline void moveCursor(std::ostream &stream, unsigned int cells, Direction direction)
{
2017-10-17 00:00:46 +02:00
if (enabled) {
stream << '\033' << '[' << cells << static_cast<char>(direction);
2017-10-17 00:00:46 +02:00
}
2015-04-22 18:36:40 +02:00
}
inline void saveCursor(std::ostream &stream)
{
2017-10-17 00:00:46 +02:00
if (enabled) {
stream << "\033[s";
2017-10-17 00:00:46 +02:00
}
2015-04-22 18:36:40 +02:00
}
inline void restoreCursor(std::ostream &stream)
{
2017-10-17 00:00:46 +02:00
if (enabled) {
stream << "\033[u";
2017-10-17 00:00:46 +02:00
}
2015-04-22 18:36:40 +02:00
}
inline void eraseDisplay(std::ostream &stream)
{
2017-10-17 00:00:46 +02:00
if (enabled) {
stream << "\033[2J";
2017-10-17 00:00:46 +02:00
}
2015-04-22 18:36:40 +02:00
}
inline void eraseLine(std::ostream &stream)
{
2017-10-17 00:00:46 +02:00
if (enabled) {
stream << "\33[2K";
}
2015-04-22 18:36:40 +02:00
}
2017-09-22 00:22:08 +02:00
inline std::ostream &operator<<(std::ostream &stream, TextAttribute displayAttribute)
{
setStyle(stream, displayAttribute);
return stream;
}
constexpr auto color(Color foreground, Color background, TextAttribute displayAttribute = TextAttribute::Reset)
2017-09-22 00:22:08 +02:00
{
return std::make_tuple(foreground, background, displayAttribute);
}
constexpr auto color(Color foreground, ColorContext context, TextAttribute displayAttribute = TextAttribute::Reset)
{
return std::make_tuple(foreground, context, displayAttribute);
}
2018-04-01 23:08:41 +02:00
template <typename TupleType,
Traits::EnableIfAny<std::is_same<TupleType, std::tuple<Color, Color, TextAttribute>>,
2018-05-08 00:35:51 +02:00
std::is_same<TupleType, std::tuple<Color, ColorContext, TextAttribute>>> * = nullptr>
inline std::ostream &operator<<(std::ostream &stream, TupleType displayAttribute)
2017-09-22 00:22:08 +02:00
{
setStyle(stream, std::get<0>(displayAttribute), std::get<1>(displayAttribute), std::get<2>(displayAttribute));
return stream;
}
/*!
2020-11-18 00:37:43 +01:00
* \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`
*/
enum class Phrases {
Error, /**< bold, red "Error: " */
Warning, /**< bold, yellow "Warning: " */
End, /**< resets the style */
PlainMessage, /**< bold, 4 spaces " " */
SuccessMessage, /**< bold, green "==> " */
SubMessage, /**< bold, green " -> " */
ErrorMessage, /**< bold, red "==> ERROR: " */
WarningMessage, /**< bold, yellow "==> WARNING: " */
2017-10-09 20:36:18 +02:00
EndFlush, /**< resets the style and flushes the stream */
2017-11-27 10:24:44 +01:00
Info, /**< bold, blue "Info: " */
2018-04-01 23:08:31 +02:00
Override, /**< erases the current line */
SubError, /**< bold, red " -> ERROR: " */
SubWarning, /**< bold, yellow " -> WARNING: " */
2019-11-17 22:26:13 +01:00
InfoMessage, /**< bold, white "==> " */
};
CPP_UTILITIES_EXPORT std::ostream &operator<<(std::ostream &stream, Phrases phrase);
CPP_UTILITIES_EXPORT std::string_view phraseString(Phrases phrase);
2020-11-18 00:37:43 +01:00
CPP_UTILITIES_EXPORT std::string_view formattedPhraseString(Phrases phrase);
} // namespace EscapeCodes
} // namespace CppUtilities
2015-04-22 18:36:40 +02:00
#endif // IOUTILITIES_ANSIESCAPECODES