CPPConsole
Loading...
Searching...
No Matches
graphics.hpp
Go to the documentation of this file.
1#ifndef __CPP_CONSOLE_GRAPHICS_HPP__
2#define __CPP_CONSOLE_GRAPHICS_HPP__
3
4#include <initializer_list>
5#include <iostream>
6#include <string>
7
8#include "ansi.hpp"
9
13namespace console {
63
67std::string bold(const std::string& str) {
68 if (isANSIEnabled()) return "\033[1m" + str + "\033[22m";
69 return str;
70}
71
75std::string faint(const std::string& str) {
76 if (isANSIEnabled()) return "\033[2m" + str + "\033[22m";
77 return str;
78}
79
83std::string italic(const std::string& str) {
84 if (isANSIEnabled()) return "\033[3m" + str + "\033[23m";
85 return str;
86}
87
91std::string underline(const std::string& str) {
92 if (isANSIEnabled()) return "\033[4m" + str + "\033[24m";
93 return str;
94}
95
99std::string blink(const std::string& str) {
100 if (isANSIEnabled()) return "\033[5m" + str + "\033[25m";
101 return str;
102}
103
107std::string inverse(const std::string& str) {
108 if (isANSIEnabled()) return "\033[7m" + str + "\033[27m";
109 return str;
110}
111
115std::string hidden(const std::string& str) {
116 if (isANSIEnabled()) return "\033[8m" + str + "\033[28m";
117 return str;
118}
119
123std::string strikethrough(const std::string& str) {
124 if (isANSIEnabled()) return "\033[9m" + str + "\033[29m";
125 return str;
126}
127
133void setBold(bool set) {
134 if (isANSIEnabled()) {
135 std::cout << (set ? "\033[1m" : "\033[22m");
136 return;
137 }
138}
139
145void setFaint(bool set) {
146 if (isANSIEnabled()) {
147 std::cout << (set ? "\033[2m" : "\033[22m");
148 return;
149 }
150#ifdef __WIN32
151 SetConsoleTextAttribute(
152 GetStdHandle(STD_OUTPUT_HANDLE),
153 set ? FOREGROUND_INTENSITY
154 : (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE));
155#endif
156}
157
163void setItalic(bool set) {
164 if (isANSIEnabled()) {
165 std::cout << (set ? "\033[3m" : "\033[23m");
166 return;
167 }
168}
169
175void setUnderline(bool set) {
176 if (isANSIEnabled()) {
177 std::cout << (set ? "\033[4m" : "\033[24m");
178 return;
179 }
180}
181
187void setBlink(bool set) {
188 if (isANSIEnabled()) {
189 std::cout << (set ? "\033[5m" : "\033[25m");
190 return;
191 }
192}
193
199void setInverse(bool set) {
200 if (isANSIEnabled()) {
201 std::cout << (set ? "\033[7m" : "\033[27m");
202 return;
203 }
204}
205
211void setHidden(bool set) {
212 if (isANSIEnabled()) {
213 std::cout << (set ? "\033[8m" : "\033[28m");
214 return;
215 }
216}
217
224 if (isANSIEnabled()) {
225 std::cout << (set ? "\033[9m" : "\033[29m");
226 return;
227 }
228}
229
230void set(std::initializer_list<Graphics> list) {
231 std::string itemStr = "";
232 for (const auto& item : list) {
233 itemStr += std::to_string(item);
234 if (item != *(list.end() - 1)) itemStr += ";";
235 }
236 if (isANSIEnabled()) {
237 std::cout << "\033[" << itemStr << "m";
238 return;
239 }
240}
241
242std::string set(const std::string& str, std::initializer_list<Graphics> list) {
243 std::string itemStr = "";
244 for (const auto& item : list) {
245 itemStr += std::to_string(item);
246 if (item != *(list.end() - 1)) itemStr += ";";
247 }
248 if (isANSIEnabled()) return "\033[" + itemStr + "m" + str + "\033[0m";
249 return str;
250}
251
253 if (isANSIEnabled()) {
254 std::cout << "\033[0m";
255 return;
256 }
257}
258} // namespace console
259
260#endif
Namespace Containing all console commands.
Definition ansi.hpp:16
std::string bold(const std::string &str)
Returns a bolded string if ANSI is enabled.
Definition graphics.hpp:67
void setInverse(bool set)
Sets the text to be inversed/reversed or not.
Definition graphics.hpp:199
void setUnderline(bool set)
Sets the text to be underlined or not.
Definition graphics.hpp:175
std::string inverse(const std::string &str)
Returns a inversed/reversed string if ANSI is enabled.
Definition graphics.hpp:107
void setHidden(bool set)
Sets the text to be hidden or not.
Definition graphics.hpp:211
void setFaint(bool set)
Sets the text to be faint/dim or not.
Definition graphics.hpp:145
std::string blink(const std::string &str)
Returns a blinking string if ANSI is enabled.
Definition graphics.hpp:99
std::string faint(const std::string &str)
Returns a faint/dim string if ANSI is enabled.
Definition graphics.hpp:75
void setBlink(bool set)
Sets the text to be blinking or not.
Definition graphics.hpp:187
Graphics
Enumeration of builtin colors for ANSI escape codes, along with other graphics modes.
Definition graphics.hpp:19
@ BRIGHT_YELLOW
Definition graphics.hpp:49
@ BG_GREEN
Definition graphics.hpp:39
@ WHITE
Definition graphics.hpp:35
@ BRIGHT_GREEN
Definition graphics.hpp:48
@ BG_RED
Definition graphics.hpp:38
@ BRIGHT_BLACK
Definition graphics.hpp:46
@ BLUE
Definition graphics.hpp:32
@ BOLD
Definition graphics.hpp:20
@ HIDDEN
Definition graphics.hpp:26
@ BLINK
Definition graphics.hpp:24
@ BG_BRIGHT_GREEN
Definition graphics.hpp:56
@ BRIGHT_MAGENTA
Definition graphics.hpp:51
@ BG_BRIGHT_YELLOW
Definition graphics.hpp:57
@ MAGENTA
Definition graphics.hpp:33
@ BG_BRIGHT_CYAN
Definition graphics.hpp:60
@ DIM
Definition graphics.hpp:21
@ BG_BRIGHT_BLACK
Definition graphics.hpp:54
@ BG_BLACK
Definition graphics.hpp:37
@ BRIGHT_BLUE
Definition graphics.hpp:50
@ BRIGHT_WHITE
Definition graphics.hpp:53
@ DEFAULT
Definition graphics.hpp:36
@ UNDERLINE
Definition graphics.hpp:23
@ BG_CYAN
Definition graphics.hpp:43
@ BG_BRIGHT_RED
Definition graphics.hpp:55
@ BG_BLUE
Definition graphics.hpp:41
@ BRIGHT_RED
Definition graphics.hpp:47
@ GREEN
Definition graphics.hpp:30
@ CYAN
Definition graphics.hpp:34
@ INVERT
Definition graphics.hpp:25
@ ITALIC
Definition graphics.hpp:22
@ BG_MAGENTA
Definition graphics.hpp:42
@ BG_BRIGHT_WHITE
Definition graphics.hpp:61
@ BG_BRIGHT_MAGENTA
Definition graphics.hpp:59
@ BG_YELLOW
Definition graphics.hpp:40
@ BG_BRIGHT_BLUE
Definition graphics.hpp:58
@ YELLOW
Definition graphics.hpp:31
@ BG_WHITE
Definition graphics.hpp:44
@ BG_DEFAULT
Definition graphics.hpp:45
@ BLACK
Definition graphics.hpp:28
@ RED
Definition graphics.hpp:29
@ STRIKETHROUGH
Definition graphics.hpp:27
@ BRIGHT_CYAN
Definition graphics.hpp:52
std::string hidden(const std::string &str)
Returns a hidden string if ANSI is enabled.
Definition graphics.hpp:115
void set(std::initializer_list< Graphics > list)
Definition graphics.hpp:230
std::string underline(const std::string &str)
Returns an underlined string if ANSI is enabled.
Definition graphics.hpp:91
std::string strikethrough(const std::string &str)
Returns a strikethough string if ANSI is enabled.
Definition graphics.hpp:123
void setStrikethrough(bool set)
Sets the text to be strikethrough or not.
Definition graphics.hpp:223
void setItalic(bool set)
Sets the text to be italicized or not.
Definition graphics.hpp:163
std::string italic(const std::string &str)
Returns an italicized string if ANSI is enabled.
Definition graphics.hpp:83
void clearGraphics()
Definition graphics.hpp:252
bool isANSIEnabled()
Checks if ANSI Codes are enable in windows.
Definition ansi.hpp:22
void setBold(bool set)
Sets the text to be bold or not.
Definition graphics.hpp:133