C++ Utilities  5.10.5
Useful C++ classes and routines such as argument parser, IO and conversion utilities
ansiescapecodes.h
Go to the documentation of this file.
1 #ifndef IOUTILITIES_ANSIESCAPECODES
2 #define IOUTILITIES_ANSIESCAPECODES
3 
4 #include "../global.h"
5 #include "../misc/traits.h"
6 
7 #include <ostream>
8 #include <string_view>
9 #include <tuple>
10 
11 namespace CppUtilities {
12 namespace EscapeCodes {
13 
14 extern CPP_UTILITIES_EXPORT bool enabled;
15 
16 enum class Color : char { Black = '0', Red, Green, Yellow, Blue, Purple, Cyan, White };
17 
18 enum class ColorContext : char { Foreground = '3', Background = '4' };
19 
20 enum class TextAttribute : char {
21  Reset = '0',
22  Bold = '1',
23  Dim = '2',
24  Italic = '3',
25  Underscore = '4',
26  Blink = '5',
27  ReverseVideo = '7',
28  Concealed = '8',
29  Strikethrough = '9',
30 };
31 
32 enum class Direction : char { Up = 'A', Down = 'B', Forward = 'C', Backward = 'D' };
33 
34 inline void setStyle(std::ostream &stream, TextAttribute displayAttribute = TextAttribute::Reset)
35 {
36  if (enabled) {
37  stream << '\e' << '[' << static_cast<char>(displayAttribute) << 'm';
38  }
39 }
40 
41 inline void setStyle(
42  std::ostream &stream, Color color, ColorContext context = ColorContext::Foreground, TextAttribute displayAttribute = TextAttribute::Reset)
43 {
44  if (enabled) {
45  stream << '\e' << '[' << static_cast<char>(displayAttribute) << ';' << static_cast<char>(context) << static_cast<char>(color) << 'm';
46  }
47 }
48 
49 inline void setStyle(std::ostream &stream, Color foregroundColor, Color backgroundColor, TextAttribute displayAttribute = TextAttribute::Reset)
50 {
51  if (enabled) {
52  stream << '\e' << '[' << static_cast<char>(displayAttribute) << ';' << static_cast<char>(ColorContext::Foreground)
53  << static_cast<char>(foregroundColor) << ';' << static_cast<char>(ColorContext::Background) << static_cast<char>(backgroundColor)
54  << 'm';
55  }
56 }
57 
58 inline void resetStyle(std::ostream &stream)
59 {
60  if (enabled) {
61  stream << '\e' << '[' << static_cast<char>(TextAttribute::Reset) << 'm';
62  }
63 }
64 
65 inline void setCursor(std::ostream &stream, unsigned int row = 0, unsigned int col = 0)
66 {
67  if (enabled) {
68  stream << '\e' << '[' << row << ';' << col << 'H';
69  }
70 }
71 
72 inline void moveCursor(std::ostream &stream, unsigned int cells, Direction direction)
73 {
74  if (enabled) {
75  stream << '\e' << '[' << cells << static_cast<char>(direction);
76  }
77 }
78 
79 inline void saveCursor(std::ostream &stream)
80 {
81  if (enabled) {
82  stream << "\e[s";
83  }
84 }
85 
86 inline void restoreCursor(std::ostream &stream)
87 {
88  if (enabled) {
89  stream << "\e[u";
90  }
91 }
92 
93 inline void eraseDisplay(std::ostream &stream)
94 {
95  if (enabled) {
96  stream << "\e[2J";
97  }
98 }
99 
100 inline void eraseLine(std::ostream &stream)
101 {
102  if (enabled) {
103  stream << "\33[2K";
104  }
105 }
106 
107 inline std::ostream &operator<<(std::ostream &stream, TextAttribute displayAttribute)
108 {
109  setStyle(stream, displayAttribute);
110  return stream;
111 }
112 
113 constexpr auto color(Color foreground, Color background, TextAttribute displayAttribute = TextAttribute::Reset)
114 {
115  return std::make_tuple(foreground, background, displayAttribute);
116 }
117 
118 constexpr auto color(Color foreground, ColorContext context, TextAttribute displayAttribute = TextAttribute::Reset)
119 {
120  return std::make_tuple(foreground, context, displayAttribute);
121 }
122 
123 template <typename TupleType,
125  std::is_same<TupleType, std::tuple<Color, ColorContext, TextAttribute>>> * = nullptr>
126 inline std::ostream &operator<<(std::ostream &stream, TupleType displayAttribute)
127 {
128  setStyle(stream, std::get<0>(displayAttribute), std::get<1>(displayAttribute), std::get<2>(displayAttribute));
129  return stream;
130 }
131 
138 enum class Phrases {
139  Error,
140  Warning,
141  End,
142  PlainMessage,
144  SubMessage,
145  ErrorMessage,
147  EndFlush,
148  Info,
149  Override,
150  SubError,
151  SubWarning,
152  InfoMessage,
153 };
154 CPP_UTILITIES_EXPORT std::ostream &operator<<(std::ostream &stream, Phrases phrase);
155 CPP_UTILITIES_EXPORT std::string_view phraseString(Phrases phrase);
156 CPP_UTILITIES_EXPORT std::string_view formattedPhraseString(Phrases phrase);
157 
158 } // namespace EscapeCodes
159 } // namespace CppUtilities
160 
161 #endif // IOUTILITIES_ANSIESCAPECODES
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
void moveCursor(std::ostream &stream, unsigned int cells, Direction direction)
constexpr auto color(Color foreground, Color background, TextAttribute displayAttribute=TextAttribute::Reset)
void setCursor(std::ostream &stream, unsigned int row=0, unsigned int col=0)
CPP_UTILITIES_EXPORT std::string_view phraseString(Phrases phrase)
Returns a string for the specified phrase without formatting.
void eraseLine(std::ostream &stream)
void eraseDisplay(std::ostream &stream)
void saveCursor(std::ostream &stream)
CPP_UTILITIES_EXPORT bool enabled
Controls whether the functions inside the EscapeCodes namespace actually make use of escape codes.
void setStyle(std::ostream &stream, TextAttribute displayAttribute=TextAttribute::Reset)
void restoreCursor(std::ostream &stream)
CPP_UTILITIES_EXPORT std::string_view formattedPhraseString(Phrases phrase)
Returns a string for the specified phrase which is formatted using ANSI escape codes.
std::ostream & operator<<(std::ostream &stream, TextAttribute displayAttribute)
Phrases
The Phrases enum contains standard phrases which can be printed to any std::ostream and obtained as s...
void resetStyle(std::ostream &stream)
typename std::enable_if< Any< Condition... >::value, Detail::Enabler >::type EnableIfAny
Shortcut for std::enable_if to apply Traits::Any and omit ::value and ::type.
Definition: traits.h:56
Contains all utilities provides by the c++utilities library.