C++ Utilities: io/ansiescapecodes.h Source File

C++ Utilities 5.34.0 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 {
12 namespace EscapeCodes {
13
14extern CPP_UTILITIES_EXPORT bool enabled;
15
16 enum class Color : char { Black = '0', Red, Green, Yellow, Blue, Purple, Cyan, White };
17
18 enum class ColorContext : char { Foreground = '3', Background = '4' };
19
20 enum 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
32 enum class Direction : char { Up = 'A', Down = 'B', Forward = 'C', Backward = 'D' };
33
34 inline void setStyle(std::ostream &stream, TextAttribute displayAttribute = TextAttribute::Reset)
35{
36 if (enabled) {
37 stream << '\033' << '[' << static_cast<char>(displayAttribute) << 'm';
38 }
39}
40
41 inline void setStyle(
42 std::ostream &stream, Color color, ColorContext context = ColorContext::Foreground, TextAttribute displayAttribute = TextAttribute::Reset)
43{
44 if (enabled) {
45 stream << '\033' << '[' << static_cast<char>(displayAttribute) << ';' << static_cast<char>(context) << static_cast<char>(color) << 'm';
46 }
47}
48
49 inline void setStyle(std::ostream &stream, Color foregroundColor, Color backgroundColor, TextAttribute displayAttribute = TextAttribute::Reset)
50{
51 if (enabled) {
52 stream << '\033' << '[' << static_cast<char>(displayAttribute) << ';' << static_cast<char>(ColorContext::Foreground)
53 << static_cast<char>(foregroundColor) << ';' << static_cast<char>(ColorContext::Background) << static_cast<char>(backgroundColor)
54 << 'm';
55 }
56}
57
58 inline void resetStyle(std::ostream &stream)
59{
60 if (enabled) {
61 stream << '\033' << '[' << static_cast<char>(TextAttribute::Reset) << 'm';
62 }
63}
64
65 inline 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
72 inline void moveCursor(std::ostream &stream, unsigned int cells, Direction direction)
73{
74 if (enabled) {
75 stream << '\033' << '[' << cells << static_cast<char>(direction);
76 }
77}
78
79 inline void saveCursor(std::ostream &stream)
80{
81 if (enabled) {
82 stream << "\033[s";
83 }
84}
85
86 inline void restoreCursor(std::ostream &stream)
87{
88 if (enabled) {
89 stream << "\033[u";
90 }
91}
92
93 inline void eraseDisplay(std::ostream &stream)
94{
95 if (enabled) {
96 stream << "\033[2J";
97 }
98}
99
100 inline void eraseLine(std::ostream &stream)
101{
102 if (enabled) {
103 stream << "\33[2K";
104 }
105}
106
107 inline std::ostream &operator<<(std::ostream &stream, TextAttribute displayAttribute)
108{
109 setStyle(stream, displayAttribute);
110 return stream;
111}
112
113 constexpr auto color(Color foreground, Color background, TextAttribute displayAttribute = TextAttribute::Reset)
114{
115 return std::make_tuple(foreground, background, displayAttribute);
116}
117
118 constexpr auto color(Color foreground, ColorContext context, TextAttribute displayAttribute = TextAttribute::Reset)
119{
120 return std::make_tuple(foreground, context, displayAttribute);
121}
122
123template <typename TupleType,
124 Traits::EnableIfAny<std::is_same<TupleType, std::tuple<Color, Color, TextAttribute>>,
125 std::is_same<TupleType, std::tuple<Color, ColorContext, TextAttribute>>> * = nullptr>
126 inline 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
138 enum class Phrases {
139 Error,
140 Warning,
141 End,
142 PlainMessage,
143 SuccessMessage,
144 SubMessage,
145 ErrorMessage,
146 WarningMessage,
147 EndFlush,
148 Info,
149 Override,
150 SubError,
151 SubWarning,
152 InfoMessage,
153};
154 CPP_UTILITIES_EXPORT std::ostream &operator<<(std::ostream &stream, Phrases phrase);
155 CPP_UTILITIES_EXPORT std::string_view phraseString(Phrases phrase);
156 CPP_UTILITIES_EXPORT std::string_view formattedPhraseString(Phrases phrase);
157
158} // namespace EscapeCodes
159} // namespace CppUtilities
160
161#endif // IOUTILITIES_ANSIESCAPECODES
global.h
CPP_UTILITIES_EXPORT
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
Definition global.h:14
CppUtilities::EscapeCodes
Encapsulates functions for formatted terminal output using ANSI escape codes.
Definition ansiescapecodes.h:12
CppUtilities::EscapeCodes::moveCursor
void moveCursor(std::ostream &stream, unsigned int cells, Direction direction)
Definition ansiescapecodes.h:72
CppUtilities::EscapeCodes::Color
Color
Definition ansiescapecodes.h:16
CppUtilities::EscapeCodes::Color::Cyan
@ Cyan
Definition ansiescapecodes.h:16
CppUtilities::EscapeCodes::Color::White
@ White
Definition ansiescapecodes.h:16
CppUtilities::EscapeCodes::Color::Yellow
@ Yellow
Definition ansiescapecodes.h:16
CppUtilities::EscapeCodes::Color::Blue
@ Blue
Definition ansiescapecodes.h:16
CppUtilities::EscapeCodes::Color::Purple
@ Purple
Definition ansiescapecodes.h:16
CppUtilities::EscapeCodes::Color::Green
@ Green
Definition ansiescapecodes.h:16
CppUtilities::EscapeCodes::Color::Black
@ Black
Definition ansiescapecodes.h:16
CppUtilities::EscapeCodes::Color::Red
@ Red
Definition ansiescapecodes.h:16
CppUtilities::EscapeCodes::TextAttribute
TextAttribute
Definition ansiescapecodes.h:20
CppUtilities::EscapeCodes::TextAttribute::Bold
@ Bold
Definition ansiescapecodes.h:22
CppUtilities::EscapeCodes::TextAttribute::Italic
@ Italic
Definition ansiescapecodes.h:24
CppUtilities::EscapeCodes::TextAttribute::Underscore
@ Underscore
Definition ansiescapecodes.h:25
CppUtilities::EscapeCodes::TextAttribute::Reset
@ Reset
Definition ansiescapecodes.h:21
CppUtilities::EscapeCodes::TextAttribute::Dim
@ Dim
Definition ansiescapecodes.h:23
CppUtilities::EscapeCodes::TextAttribute::Strikethrough
@ Strikethrough
Definition ansiescapecodes.h:29
CppUtilities::EscapeCodes::TextAttribute::Concealed
@ Concealed
Definition ansiescapecodes.h:28
CppUtilities::EscapeCodes::TextAttribute::ReverseVideo
@ ReverseVideo
Definition ansiescapecodes.h:27
CppUtilities::EscapeCodes::TextAttribute::Blink
@ Blink
Definition ansiescapecodes.h:26
CppUtilities::EscapeCodes::color
constexpr auto color(Color foreground, Color background, TextAttribute displayAttribute=TextAttribute::Reset)
Definition ansiescapecodes.h:113
CppUtilities::EscapeCodes::Direction
Direction
Definition ansiescapecodes.h:32
CppUtilities::EscapeCodes::Direction::Down
@ Down
Definition ansiescapecodes.h:32
CppUtilities::EscapeCodes::Direction::Up
@ Up
Definition ansiescapecodes.h:32
CppUtilities::EscapeCodes::Direction::Forward
@ Forward
Definition ansiescapecodes.h:32
CppUtilities::EscapeCodes::Direction::Backward
@ Backward
Definition ansiescapecodes.h:32
CppUtilities::EscapeCodes::setCursor
void setCursor(std::ostream &stream, unsigned int row=0, unsigned int col=0)
Definition ansiescapecodes.h:65
CppUtilities::EscapeCodes::phraseString
CPP_UTILITIES_EXPORT std::string_view phraseString(Phrases phrase)
Returns a string for the specified phrase without formatting.
Definition ansiescapecodes.cpp:120
CppUtilities::EscapeCodes::eraseLine
void eraseLine(std::ostream &stream)
Definition ansiescapecodes.h:100
CppUtilities::EscapeCodes::eraseDisplay
void eraseDisplay(std::ostream &stream)
Definition ansiescapecodes.h:93
CppUtilities::EscapeCodes::saveCursor
void saveCursor(std::ostream &stream)
Definition ansiescapecodes.h:79
CppUtilities::EscapeCodes::operator<<
std::ostream & operator<<(std::ostream &stream, TextAttribute displayAttribute)
Definition ansiescapecodes.h:107
CppUtilities::EscapeCodes::enabled
CPP_UTILITIES_EXPORT bool enabled
Controls whether the functions inside the EscapeCodes namespace actually make use of escape codes.
Definition ansiescapecodes.cpp:22
CppUtilities::EscapeCodes::setStyle
void setStyle(std::ostream &stream, TextAttribute displayAttribute=TextAttribute::Reset)
Definition ansiescapecodes.h:34
CppUtilities::EscapeCodes::restoreCursor
void restoreCursor(std::ostream &stream)
Definition ansiescapecodes.h:86
CppUtilities::EscapeCodes::formattedPhraseString
CPP_UTILITIES_EXPORT std::string_view formattedPhraseString(Phrases phrase)
Returns a string for the specified phrase which is formatted using ANSI escape codes.
Definition ansiescapecodes.cpp:159
CppUtilities::EscapeCodes::Phrases
Phrases
The Phrases enum contains standard phrases which can be printed to any std::ostream and obtained as s...
Definition ansiescapecodes.h:138
CppUtilities::EscapeCodes::Phrases::SubError
@ SubError
Definition ansiescapecodes.h:150
CppUtilities::EscapeCodes::Phrases::Warning
@ Warning
Definition ansiescapecodes.h:140
CppUtilities::EscapeCodes::Phrases::InfoMessage
@ InfoMessage
Definition ansiescapecodes.h:152
CppUtilities::EscapeCodes::Phrases::Info
@ Info
Definition ansiescapecodes.h:148
CppUtilities::EscapeCodes::Phrases::PlainMessage
@ PlainMessage
Definition ansiescapecodes.h:142
CppUtilities::EscapeCodes::Phrases::Override
@ Override
Definition ansiescapecodes.h:149
CppUtilities::EscapeCodes::Phrases::End
@ End
Definition ansiescapecodes.h:141
CppUtilities::EscapeCodes::Phrases::Error
@ Error
Definition ansiescapecodes.h:139
CppUtilities::EscapeCodes::Phrases::SubMessage
@ SubMessage
Definition ansiescapecodes.h:144
CppUtilities::EscapeCodes::Phrases::SuccessMessage
@ SuccessMessage
Definition ansiescapecodes.h:143
CppUtilities::EscapeCodes::Phrases::SubWarning
@ SubWarning
Definition ansiescapecodes.h:151
CppUtilities::EscapeCodes::Phrases::ErrorMessage
@ ErrorMessage
Definition ansiescapecodes.h:145
CppUtilities::EscapeCodes::Phrases::EndFlush
@ EndFlush
Definition ansiescapecodes.h:147
CppUtilities::EscapeCodes::Phrases::WarningMessage
@ WarningMessage
Definition ansiescapecodes.h:146
CppUtilities::EscapeCodes::resetStyle
void resetStyle(std::ostream &stream)
Definition ansiescapecodes.h:58
CppUtilities::EscapeCodes::ColorContext
ColorContext
Definition ansiescapecodes.h:18
CppUtilities::EscapeCodes::ColorContext::Foreground
@ Foreground
Definition ansiescapecodes.h:18
CppUtilities::EscapeCodes::ColorContext::Background
@ Background
Definition ansiescapecodes.h:18
CppUtilities::Traits::EnableIfAny
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
CppUtilities
Contains all utilities provided by the c++utilities library.
Definition argumentparser.h:18
traits.h