C++ Utilities  4.17.0
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 <tuple>
9 
10 namespace EscapeCodes {
11 
12 extern CPP_UTILITIES_EXPORT bool enabled;
13 
14 enum class Color : char { Black = '0', Red, Green, Yellow, Blue, Purple, Cyan, White };
15 
16 enum class ColorContext : char { Foreground = '3', Background = '4' };
17 
18 enum class TextAttribute : char {
19  Reset = '0',
20  Bold = '1',
21  Dim = '2',
22  Italic = '3',
23  Underscore = '4',
24  Blink = '5',
25  ReverseVideo = '7',
26  Concealed = '8',
27  Strikethrough = '9',
28 };
29 
30 enum class Direction : char { Up = 'A', Down = 'B', Forward = 'C', Backward = 'D' };
31 
32 inline void setStyle(std::ostream &stream, TextAttribute displayAttribute = TextAttribute::Reset)
33 {
34  if (enabled) {
35  stream << '\e' << '[' << static_cast<char>(displayAttribute) << 'm';
36  }
37 }
38 
39 inline void setStyle(
40  std::ostream &stream, Color color, ColorContext context = ColorContext::Foreground, TextAttribute displayAttribute = TextAttribute::Reset)
41 {
42  if (enabled) {
43  stream << '\e' << '[' << static_cast<char>(displayAttribute) << ';' << static_cast<char>(context) << static_cast<char>(color) << 'm';
44  }
45 }
46 
47 inline void setStyle(std::ostream &stream, Color foregroundColor, Color backgroundColor, TextAttribute displayAttribute = TextAttribute::Reset)
48 {
49  if (enabled) {
50  stream << '\e' << '[' << static_cast<char>(displayAttribute) << ';' << static_cast<char>(ColorContext::Foreground)
51  << static_cast<char>(foregroundColor) << ';' << static_cast<char>(ColorContext::Background) << static_cast<char>(backgroundColor)
52  << 'm';
53  }
54 }
55 
56 inline void resetStyle(std::ostream &stream)
57 {
58  if (enabled) {
59  stream << '\e' << '[' << static_cast<char>(TextAttribute::Reset) << 'm';
60  }
61 }
62 
63 inline void setCursor(std::ostream &stream, unsigned int row = 0, unsigned int col = 0)
64 {
65  if (enabled) {
66  stream << '\e' << '[' << row << ';' << col << 'H';
67  }
68 }
69 
70 inline void moveCursor(std::ostream &stream, unsigned int cells, Direction direction)
71 {
72  if (enabled) {
73  stream << '\e' << '[' << cells << static_cast<char>(direction);
74  }
75 }
76 
77 inline void saveCursor(std::ostream &stream)
78 {
79  if (enabled) {
80  stream << "\e[s";
81  }
82 }
83 
84 inline void restoreCursor(std::ostream &stream)
85 {
86  if (enabled) {
87  stream << "\e[u";
88  }
89 }
90 
91 inline void eraseDisplay(std::ostream &stream)
92 {
93  if (enabled) {
94  stream << "\e[2J";
95  }
96 }
97 
98 inline void eraseLine(std::ostream &stream)
99 {
100  if (enabled) {
101  stream << "\33[2K";
102  }
103 }
104 
105 inline std::ostream &operator<<(std::ostream &stream, TextAttribute displayAttribute)
106 {
107  setStyle(stream, displayAttribute);
108  return stream;
109 }
110 
111 constexpr auto color(Color foreground, Color background, TextAttribute displayAttribute = TextAttribute::Reset)
112 {
113  return std::make_tuple(foreground, background, displayAttribute);
114 }
115 
116 constexpr auto color(Color foreground, ColorContext context, TextAttribute displayAttribute = TextAttribute::Reset)
117 {
118  return std::make_tuple(foreground, context, displayAttribute);
119 }
120 
121 template <typename TupleType,
123  std::is_same<TupleType, std::tuple<Color, ColorContext, TextAttribute>>> * = nullptr>
124 inline std::ostream &operator<<(std::ostream &stream, TupleType displayAttribute)
125 {
126  setStyle(stream, std::get<0>(displayAttribute), std::get<1>(displayAttribute), std::get<2>(displayAttribute));
127  return stream;
128 }
129 
135 enum class Phrases {
136  Error,
137  Warning,
138  End,
139  PlainMessage,
141  SubMessage,
142  ErrorMessage,
144  EndFlush,
145  Info,
146  Override,
147  SubError,
148  SubWarning,
149 };
150 CPP_UTILITIES_EXPORT std::ostream &operator<<(std::ostream &stream, Phrases phrase);
151 
152 } // namespace EscapeCodes
153 
154 #endif // IOUTILITIES_ANSIESCAPECODES
constexpr auto color(Color foreground, Color background, TextAttribute displayAttribute=TextAttribute::Reset)
Encapsulates functions for formatted terminal output using ANSI escape codes.
#define CPP_UTILITIES_EXPORT
Phrases
The Phrases enum contains standard phrases which can be printed to any std::ostream.
void setStyle(std::ostream &stream, TextAttribute displayAttribute=TextAttribute::Reset)
void moveCursor(std::ostream &stream, unsigned int cells, Direction direction)
void restoreCursor(std::ostream &stream)
void eraseDisplay(std::ostream &stream)
CPP_UTILITIES_EXPORT bool enabled
Controls whether the functions inside the EscapeCodes namespace actually make use of escape codes.
void resetStyle(std::ostream &stream)
void eraseLine(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:53
void saveCursor(std::ostream &stream)
void setCursor(std::ostream &stream, unsigned int row=0, unsigned int col=0)
std::ostream & operator<<(std::ostream &stream, TextAttribute displayAttribute)