C++ Utilities 5.24.7
Useful C++ classes and routines such as argument parser, IO and conversion utilities
Loading...
Searching...
No Matches
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 <string_view>
9#include <tuple>
10
11namespace CppUtilities {
12namespace EscapeCodes {
13
15
16enum class Color : char { Black = '0', Red, Green, Yellow, Blue, Purple, Cyan, White };
17
18enum class ColorContext : char { Foreground = '3', Background = '4' };
19
20enum class TextAttribute : char {
21 Reset = '0',
22 Bold = '1',
23 Dim = '2',
24 Italic = '3',
25 Underscore = '4',
26 Blink = '5',
27 ReverseVideo = '7',
28 Concealed = '8',
29 Strikethrough = '9',
30};
31
32enum class Direction : char { Up = 'A', Down = 'B', Forward = 'C', Backward = 'D' };
33
40
41inline void setStyle(
43{
44 if (enabled) {
45 stream << '\033' << '[' << static_cast<char>(displayAttribute) << ';' << static_cast<char>(context) << static_cast<char>(color) << 'm';
46 }
47}
48
50{
51 if (enabled) {
53 << static_cast<char>(foregroundColor) << ';' << static_cast<char>(ColorContext::Background) << static_cast<char>(backgroundColor)
54 << 'm';
55 }
56}
57
58inline void resetStyle(std::ostream &stream)
59{
60 if (enabled) {
62 }
63}
64
65inline void setCursor(std::ostream &stream, unsigned int row = 0, unsigned int col = 0)
66{
67 if (enabled) {
68 stream << '\033' << '[' << row << ';' << col << 'H';
69 }
70}
71
72inline void moveCursor(std::ostream &stream, unsigned int cells, Direction direction)
73{
74 if (enabled) {
76 }
77}
78
79inline void saveCursor(std::ostream &stream)
80{
81 if (enabled) {
82 stream << "\033[s";
83 }
84}
85
86inline void restoreCursor(std::ostream &stream)
87{
88 if (enabled) {
89 stream << "\033[u";
90 }
91}
92
93inline void eraseDisplay(std::ostream &stream)
94{
95 if (enabled) {
96 stream << "\033[2J";
97 }
98}
99
100inline void eraseLine(std::ostream &stream)
101{
102 if (enabled) {
103 stream << "\33[2K";
104 }
105}
106
107inline std::ostream &operator<<(std::ostream &stream, TextAttribute displayAttribute)
108{
109 setStyle(stream, displayAttribute);
110 return stream;
111}
112
117
122
123template <typename TupleType,
125 std::is_same<TupleType, std::tuple<Color, ColorContext, TextAttribute>>> * = nullptr>
126inline std::ostream &operator<<(std::ostream &stream, TupleType displayAttribute)
127{
128 setStyle(stream, std::get<0>(displayAttribute), std::get<1>(displayAttribute), std::get<2>(displayAttribute));
129 return stream;
130}
131
138enum class Phrases {
139 Error,
140 Warning,
141 End,
144 SubMessage,
147 EndFlush,
148 Info,
149 Override,
150 SubError,
151 SubWarning,
153};
154CPP_UTILITIES_EXPORT std::ostream &operator<<(std::ostream &stream, Phrases phrase);
157
158} // namespace EscapeCodes
159} // namespace CppUtilities
160
161#endif // IOUTILITIES_ANSIESCAPECODES
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
Definition global.h:14
void moveCursor(std::ostream &stream, unsigned int cells, Direction direction)
constexpr auto color(Color foreground, Color background, TextAttribute displayAttribute=TextAttribute::Reset)
void setCursor(std::ostream &stream, unsigned int row=0, unsigned int col=0)
CPP_UTILITIES_EXPORT std::string_view phraseString(Phrases phrase)
Returns a string for the specified phrase without formatting.
void eraseLine(std::ostream &stream)
void eraseDisplay(std::ostream &stream)
void saveCursor(std::ostream &stream)
std::ostream & operator<<(std::ostream &stream, TextAttribute displayAttribute)
CPP_UTILITIES_EXPORT bool enabled
Controls whether the functions inside the EscapeCodes namespace actually make use of escape codes.
void setStyle(std::ostream &stream, TextAttribute displayAttribute=TextAttribute::Reset)
void restoreCursor(std::ostream &stream)
CPP_UTILITIES_EXPORT std::string_view formattedPhraseString(Phrases phrase)
Returns a string for the specified phrase which is formatted using ANSI escape codes.
Phrases
The Phrases enum contains standard phrases which can be printed to any std::ostream and obtained as s...
void resetStyle(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:49
Contains all utilities provides by the c++utilities library.
IntegralType stringToNumber(const StringType &string, BaseType base=10)
Converts the given string to an unsigned/signed number assuming string uses the specified base.