C++ Utilities  4.7.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 
12 namespace EscapeCodes {
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 };
28 
29 enum class Direction : char { Up = 'A', Down = 'B', Forward = 'C', Backward = 'D' };
30 
31 inline void setStyle(std::ostream &stream, TextAttribute displayAttribute = TextAttribute::Reset)
32 {
33  stream << '\e' << '[' << static_cast<char>(displayAttribute) << 'm';
34 }
35 
36 inline void setStyle(
37  std::ostream &stream, Color color, ColorContext context = ColorContext::Foreground, TextAttribute displayAttribute = TextAttribute::Reset)
38 {
39  stream << '\e' << '[' << static_cast<char>(displayAttribute) << ';' << static_cast<char>(context) << static_cast<char>(color) << 'm';
40 }
41 
42 inline void setStyle(std::ostream &stream, Color foregroundColor, Color backgroundColor, TextAttribute displayAttribute = TextAttribute::Reset)
43 {
44  stream << '\e' << '[' << static_cast<char>(displayAttribute) << ';' << static_cast<char>(ColorContext::Foreground)
45  << static_cast<char>(foregroundColor) << ';' << static_cast<char>(ColorContext::Foreground) << static_cast<char>(backgroundColor) << 'm';
46 }
47 
48 inline void resetStyle(std::ostream &stream)
49 {
50  stream << '\e' << '[' << static_cast<char>(TextAttribute::Reset) << 'm';
51 }
52 
53 inline void setCursor(std::ostream &stream, unsigned int row = 0, unsigned int col = 0)
54 {
55  stream << '\e' << '[' << row << ';' << col << 'H';
56 }
57 
58 inline void moveCursor(std::ostream &stream, unsigned int cells, Direction direction)
59 {
60  stream << '\e' << '[' << cells << static_cast<char>(direction);
61 }
62 
63 inline void saveCursor(std::ostream &stream)
64 {
65  stream << "\e[s";
66 }
67 
68 inline void restoreCursor(std::ostream &stream)
69 {
70  stream << "\e[u";
71 }
72 
73 inline void eraseDisplay(std::ostream &stream)
74 {
75  stream << "\e[2J";
76 }
77 
78 inline void eraseLine(std::ostream &stream)
79 {
80  stream << "\33[2K";
81 }
82 }
83 
84 #endif // IOUTILITIES_ANSIESCAPECODES
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)