Sista v3.0.0-alpha.1
Sista is a C++ lightweight OS-agnostic library for terminal animations and videogames
Loading...
Searching...
No Matches
ansi.hpp
Go to the documentation of this file.
1
19#pragma once
20
21#include <variant>
22#include <string>
23
24
38#define ESC "\x1b"
51#define CSI "\x1b["
63#define CLS "\x1b[2J"
75#define SSB "\x1b[3J"
86#define HIDE_CURSOR "\x1b[?25l"
97#define SHOW_CURSOR "\x1b[?25h"
109#define TL "\x1b[H"
110
111
112namespace sista {
121 enum class ForegroundColor: int {
122 BLACK = 30,
123 RED = 31,
124 GREEN = 32,
125 YELLOW = 33,
126 BLUE = 34,
127 MAGENTA = 35,
128 CYAN = 36,
129 WHITE = 37
130 };
139 enum class BackgroundColor: int {
140 BLACK = 40,
141 RED = 41,
142 GREEN = 42,
143 YELLOW = 43,
144 BLUE = 44,
145 MAGENTA = 45,
146 CYAN = 46,
147 WHITE = 47
148 };
158 enum class Attribute: int {
159 RESET = 0,
160 BRIGHT = 1,
161 FAINT = 2,
162 ITALIC = 3,
163 UNDERSCORE = 4,
164 BLINK = 5,
165 RAPID_BLINK = 6,
166 REVERSE = 7,
167 HIDDEN = 8,
168 STRIKETHROUGH = 9
169 };
177 struct RGBColor {
178 unsigned char red;
179 unsigned char green;
180 unsigned char blue;
183 RGBColor();
189 RGBColor(unsigned char, unsigned char, unsigned char);
190 };
191
232
248 void resetAnsi();
249
259 void setForegroundColor(const RGBColor&);
269 void setBackgroundColor(const RGBColor&);
280 void setForegroundColor(unsigned char, unsigned char, unsigned char);
291 void setBackgroundColor(unsigned char, unsigned char, unsigned char);
301 void setForegroundColor(unsigned char);
311 void setBackgroundColor(unsigned char);
312
323 std::string fgColorStr(ForegroundColor);
334 std::string bgColorStr(BackgroundColor);
345 std::string attrStr(Attribute);
356 std::string fgColorStr(const RGBColor&);
369 std::string bgColorStr(const RGBColor&);
382 std::string fgColorStr(unsigned char, unsigned char, unsigned char);
395 std::string bgColorStr(unsigned char, unsigned char, unsigned char);
396
405 enum class ScreenMode: int {
413 LINE_WRAPPING = 7,
421 };
422
443
462 std::variant<ForegroundColor, RGBColor> foregroundColor;
472 std::variant<BackgroundColor, RGBColor> backgroundColor;
475
477 ANSISettings();
483 ANSISettings(const RGBColor&, const RGBColor&, const Attribute&);
495 ANSISettings(const RGBColor&, const BackgroundColor&, const Attribute&);
501 ANSISettings(const ForegroundColor&, const RGBColor&, const Attribute&);
502
515 void apply() const;
516 };
517};
Sista library namespace.
Definition: ansi.cpp:26
void resetAnsi()
Resets all ANSI settings to default values.
Definition: ansi.cpp:47
void resetAttribute(Attribute attribute)
Resets a text attribute using a predefined Attribute enum.
Definition: ansi.cpp:39
Attribute
Enumeration of text attributes for ANSI escape codes.
Definition: ansi.hpp:158
std::string attrStr(Attribute attribute)
Converts an Attribute enum to its corresponding ANSI escape code string.
Definition: ansi.cpp:82
ForegroundColor
Enumeration of foreground colors for ANSI escape codes.
Definition: ansi.hpp:121
ScreenMode
Represents various ANSI screen modes.
Definition: ansi.hpp:405
@ COLOR_16_COLORS_GRAPHICS_640_350
@ COLOR_16_COLORS_GRAPHICS_640_480
@ MONOCROME_2_COLORS_GRAPHICS_640_480
@ MONOCROME_2_COLORS_GRAPHICS_640_350
@ COLOR_256_COLORS_GRAPHICS_320_200
@ COLOR_16_COLORS_GRAPHICS_640_200
std::string bgColorStr(BackgroundColor color)
Converts a BackgroundColor enum to its corresponding ANSI escape code string.
Definition: ansi.cpp:79
void unsetScreenMode(ScreenMode mode)
Unsets a specific screen mode.
Definition: ansi.cpp:117
void setBackgroundColor(BackgroundColor color)
Sets the background color using a predefined BackgroundColor enum.
Definition: ansi.cpp:33
std::string fgColorStr(ForegroundColor color)
Converts a ForegroundColor enum to its corresponding ANSI escape code string.
Definition: ansi.cpp:76
void setForegroundColor(ForegroundColor color)
Sets the foreground color using a predefined ForegroundColor enum.
Definition: ansi.cpp:30
BackgroundColor
Enumeration of background colors for ANSI escape codes.
Definition: ansi.hpp:139
void setScreenMode(ScreenMode mode)
Sets a specific screen mode.
Definition: ansi.cpp:114
void setAttribute(Attribute attribute)
Sets a text attribute using a predefined Attribute enum.
Definition: ansi.cpp:36
Represents a set of ANSI settings including colors and attributes.
Definition: ansi.hpp:452
Attribute attribute
Text attribute setting.
Definition: ansi.hpp:474
void apply() const
Applies the ANSI settings to the terminal.
Definition: ansi.cpp:135
std::variant< ForegroundColor, RGBColor > foregroundColor
Foreground color, can be ForegroundColor or RGBColor.
Definition: ansi.hpp:462
std::variant< BackgroundColor, RGBColor > backgroundColor
Background color, can be BackgroundColor or RGBColor.
Definition: ansi.hpp:472
ANSISettings()
Definition: ansi.cpp:121
Represents an RGB color with red, green, and blue components in True Color (24-bit).
Definition: ansi.hpp:177
unsigned char green
Definition: ansi.hpp:179
RGBColor()
Default constructor initializing color to black (0,0,0).
Definition: ansi.cpp:27
unsigned char red
Definition: ansi.hpp:178
unsigned char blue
Definition: ansi.hpp:180