C++ Utilities  4.10.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 
13 namespace EscapeCodes {
14 
15 enum class Color : char { Black = '0', Red, Green, Yellow, Blue, Purple, Cyan, White };
16 
17 enum class ColorContext : char { Foreground = '3', Background = '4' };
18 
19 enum class TextAttribute : char {
20  Reset = '0',
21  Bold = '1',
22  Dim = '2',
23  Italic = '3',
24  Underscore = '4',
25  Blink = '5',
26  ReverseVideo = '7',
27  Concealed = '8'
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  stream << '\e' << '[' << static_cast<char>(displayAttribute) << 'm';
35 }
36 
37 inline void setStyle(
38  std::ostream &stream, Color color, ColorContext context = ColorContext::Foreground, TextAttribute displayAttribute = TextAttribute::Reset)
39 {
40  stream << '\e' << '[' << static_cast<char>(displayAttribute) << ';' << static_cast<char>(context) << static_cast<char>(color) << 'm';
41 }
42 
43 inline void setStyle(std::ostream &stream, Color foregroundColor, Color backgroundColor, TextAttribute displayAttribute = TextAttribute::Reset)
44 {
45  stream << '\e' << '[' << static_cast<char>(displayAttribute) << ';' << static_cast<char>(ColorContext::Foreground)
46  << static_cast<char>(foregroundColor) << ';' << static_cast<char>(ColorContext::Foreground) << static_cast<char>(backgroundColor) << 'm';
47 }
48 
49 inline void resetStyle(std::ostream &stream)
50 {
51  stream << '\e' << '[' << static_cast<char>(TextAttribute::Reset) << 'm';
52 }
53 
54 inline void setCursor(std::ostream &stream, unsigned int row = 0, unsigned int col = 0)
55 {
56  stream << '\e' << '[' << row << ';' << col << 'H';
57 }
58 
59 inline void moveCursor(std::ostream &stream, unsigned int cells, Direction direction)
60 {
61  stream << '\e' << '[' << cells << static_cast<char>(direction);
62 }
63 
64 inline void saveCursor(std::ostream &stream)
65 {
66  stream << "\e[s";
67 }
68 
69 inline void restoreCursor(std::ostream &stream)
70 {
71  stream << "\e[u";
72 }
73 
74 inline void eraseDisplay(std::ostream &stream)
75 {
76  stream << "\e[2J";
77 }
78 
79 inline void eraseLine(std::ostream &stream)
80 {
81  stream << "\33[2K";
82 }
83 
84 inline std::ostream &operator<<(std::ostream &stream, TextAttribute displayAttribute)
85 {
86  setStyle(stream, displayAttribute);
87  return stream;
88 }
89 
90 inline auto color(Color foreground, Color background, TextAttribute displayAttribute = TextAttribute::Reset)
91 {
92  return std::make_tuple(foreground, background, displayAttribute);
93 }
94 
95 inline std::ostream &operator<<(std::ostream &stream, std::tuple<Color, Color, TextAttribute> displayAttribute)
96 {
97  setStyle(stream, std::get<0>(displayAttribute), std::get<1>(displayAttribute), std::get<2>(displayAttribute));
98  return stream;
99 }
100 
106 enum class Phrases {
107  Error,
108  Warning,
109  End,
110  PlainMessage,
112  SubMessage,
113  ErrorMessage,
115 };
116 CPP_UTILITIES_EXPORT std::ostream &operator<<(std::ostream &stream, Phrases phrase);
117 
118 } // namespace EscapeCodes
119 
120 #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)
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)