C++ Utilities  4.11.0
Common C++ classes and routines used by my applications 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 
6 #include <ostream>
7 #include <tuple>
8 
9 namespace EscapeCodes {
10 
11 extern CPP_UTILITIES_EXPORT bool enabled;
12 
13 enum class Color : char { Black = '0', Red, Green, Yellow, Blue, Purple, Cyan, White };
14 
15 enum class ColorContext : char { Foreground = '3', Background = '4' };
16 
17 enum class TextAttribute : char {
18  Reset = '0',
19  Bold = '1',
20  Dim = '2',
21  Italic = '3',
22  Underscore = '4',
23  Blink = '5',
24  ReverseVideo = '7',
25  Concealed = '8'
26 };
27 
28 enum class Direction : char { Up = 'A', Down = 'B', Forward = 'C', Backward = 'D' };
29 
30 inline void setStyle(std::ostream &stream, TextAttribute displayAttribute = TextAttribute::Reset)
31 {
32  if (enabled) {
33  stream << '\e' << '[' << static_cast<char>(displayAttribute) << 'm';
34  }
35 }
36 
37 inline void setStyle(
38  std::ostream &stream, Color color, ColorContext context = ColorContext::Foreground, TextAttribute displayAttribute = TextAttribute::Reset)
39 {
40  if (enabled) {
41  stream << '\e' << '[' << static_cast<char>(displayAttribute) << ';' << static_cast<char>(context) << static_cast<char>(color) << 'm';
42  }
43 }
44 
45 inline void setStyle(std::ostream &stream, Color foregroundColor, Color backgroundColor, TextAttribute displayAttribute = TextAttribute::Reset)
46 {
47  if (enabled) {
48  stream << '\e' << '[' << static_cast<char>(displayAttribute) << ';' << static_cast<char>(ColorContext::Foreground)
49  << static_cast<char>(foregroundColor) << ';' << static_cast<char>(ColorContext::Foreground) << static_cast<char>(backgroundColor)
50  << 'm';
51  }
52 }
53 
54 inline void resetStyle(std::ostream &stream)
55 {
56  if (enabled) {
57  stream << '\e' << '[' << static_cast<char>(TextAttribute::Reset) << 'm';
58  }
59 }
60 
61 inline void setCursor(std::ostream &stream, unsigned int row = 0, unsigned int col = 0)
62 {
63  if (enabled) {
64  stream << '\e' << '[' << row << ';' << col << 'H';
65  }
66 }
67 
68 inline void moveCursor(std::ostream &stream, unsigned int cells, Direction direction)
69 {
70  if (enabled) {
71  stream << '\e' << '[' << cells << static_cast<char>(direction);
72  }
73 }
74 
75 inline void saveCursor(std::ostream &stream)
76 {
77  if (enabled) {
78  stream << "\e[s";
79  }
80 }
81 
82 inline void restoreCursor(std::ostream &stream)
83 {
84  if (enabled) {
85  stream << "\e[u";
86  }
87 }
88 
89 inline void eraseDisplay(std::ostream &stream)
90 {
91  if (enabled) {
92  stream << "\e[2J";
93  }
94 }
95 
96 inline void eraseLine(std::ostream &stream)
97 {
98  if (enabled) {
99  stream << "\33[2K";
100  }
101 }
102 
103 inline std::ostream &operator<<(std::ostream &stream, TextAttribute displayAttribute)
104 {
105  setStyle(stream, displayAttribute);
106  return stream;
107 }
108 
109 inline auto color(Color foreground, Color background, TextAttribute displayAttribute = TextAttribute::Reset)
110 {
111  return std::make_tuple(foreground, background, displayAttribute);
112 }
113 
114 inline std::ostream &operator<<(std::ostream &stream, std::tuple<Color, Color, TextAttribute> displayAttribute)
115 {
116  setStyle(stream, std::get<0>(displayAttribute), std::get<1>(displayAttribute), std::get<2>(displayAttribute));
117  return stream;
118 }
119 
125 enum class Phrases {
126  Error,
127  Warning,
128  End,
129  PlainMessage,
131  SubMessage,
132  ErrorMessage,
134  EndFlush,
135 };
136 CPP_UTILITIES_EXPORT std::ostream &operator<<(std::ostream &stream, Phrases phrase);
137 
138 } // namespace EscapeCodes
139 
140 #endif // IOUTILITIES_ANSIESCAPECODES
auto color(Color foreground, Color background, TextAttribute displayAttribute=TextAttribute::Reset)
Encapsulates functions for formatted terminal output using ANSI escape codes.
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)
void saveCursor(std::ostream &stream)
void setCursor(std::ostream &stream, unsigned int row=0, unsigned int col=0)
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
std::ostream & operator<<(std::ostream &stream, TextAttribute displayAttribute)