C++ Utilities  4.6.1
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 
12 namespace EscapeCodes {
13 
14 enum class Color : char
15 {
16  Black = '0',
17  Red,
18  Green,
19  Yellow,
20  Blue,
21  Purple,
22  Cyan,
23  White
24 };
25 
26 enum class ColorContext : char
27 {
28  Foreground = '3',
29  Background = '4'
30 };
31 
32 enum class TextAttribute : char
33 {
34  Reset = '0',
35  Bold = '1',
36  Dim = '2',
37  Italic = '3',
38  Underscore = '4',
39  Blink = '5',
40  ReverseVideo = '7',
41  Concealed = '8'
42 };
43 
44 enum class Direction : char
45 {
46  Up = 'A',
47  Down = 'B',
48  Forward = 'C',
49  Backward = 'D'
50 };
51 
52 inline void setStyle(std::ostream &stream, TextAttribute displayAttribute = TextAttribute::Reset)
53 {
54  stream << '\e' << '[' << static_cast<char>(displayAttribute) << 'm';
55 }
56 
57 inline void setStyle(std::ostream &stream, Color color,
59  TextAttribute displayAttribute = TextAttribute::Reset)
60 {
61  stream << '\e' << '[' << static_cast<char>(displayAttribute) << ';'
62  << static_cast<char>(context) << static_cast<char>(color) << 'm';
63 }
64 
65 inline void setStyle(std::ostream &stream, Color foregroundColor, Color backgroundColor,
66  TextAttribute displayAttribute = TextAttribute::Reset)
67 {
68  stream << '\e' << '[' << static_cast<char>(displayAttribute) << ';'
69  << static_cast<char>(ColorContext::Foreground) << static_cast<char>(foregroundColor) << ';'
70  << static_cast<char>(ColorContext::Foreground) << static_cast<char>(backgroundColor) << 'm';
71 }
72 
73 inline void resetStyle(std::ostream &stream)
74 {
75  stream << '\e' << '[' << static_cast<char>(TextAttribute::Reset) << 'm';
76 }
77 
78 inline void setCursor(std::ostream &stream, unsigned int row = 0, unsigned int col = 0)
79 {
80  stream << '\e' << '[' << row << ';' << col << 'H';
81 }
82 
83 inline void moveCursor(std::ostream &stream, unsigned int cells, Direction direction)
84 {
85  stream << '\e' << '[' << cells << static_cast<char>(direction);
86 }
87 
88 inline void saveCursor(std::ostream &stream)
89 {
90  stream << "\e[s";
91 }
92 
93 inline void restoreCursor(std::ostream &stream)
94 {
95  stream << "\e[u";
96 }
97 
98 inline void eraseDisplay(std::ostream &stream)
99 {
100  stream << "\e[2J";
101 }
102 
103 inline void eraseLine(std::ostream &stream)
104 {
105  stream << "\33[2K";
106 }
107 
108 }
109 
110 #endif // IOUTILITIES_ANSIESCAPECODES
111 
Encapsulates functions for formatted terminal output using ANSI escape codes.
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)