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 <bitset>
23#include <string>
24
25
39#define ESC "\x1b"
52#define CSI "\x1b["
64#define CLS "\x1b[2J"
76#define SSB "\x1b[3J"
87#define HIDE_CURSOR "\x1b[?25l"
98#define SHOW_CURSOR "\x1b[?25h"
110#define TL "\x1b[H"
111
112
113namespace sista {
122 enum class ForegroundColor: int {
123 BLACK = 30,
124 RED = 31,
125 GREEN = 32,
126 YELLOW = 33,
127 BLUE = 34,
128 MAGENTA = 35,
129 CYAN = 36,
130 WHITE = 37
131 };
140 enum class BackgroundColor: int {
141 BLACK = 40,
142 RED = 41,
143 GREEN = 42,
144 YELLOW = 43,
145 BLUE = 44,
146 MAGENTA = 45,
147 CYAN = 46,
148 WHITE = 47
149 };
159 enum class Attribute: int {
160 RESET = 0,
161 BRIGHT = 1,
162 FAINT = 2,
163 ITALIC = 3,
164 UNDERSCORE = 4,
165 BLINK = 5,
166 RAPID_BLINK = 6,
167 REVERSE = 7,
168 HIDDEN = 8,
169 STRIKETHROUGH = 9
170 };
181 inline std::bitset<10> make_attr_bitset(std::initializer_list<Attribute> attrs) {
182 std::bitset<10> bits;
183 for (auto attr : attrs) {
184 bits.set(static_cast<int>(attr));
185 }
186 return bits;
187 }
188
196 struct RGBColor {
197 unsigned char red;
198 unsigned char green;
199 unsigned char blue;
202 RGBColor();
208 RGBColor(unsigned char, unsigned char, unsigned char);
209 };
210
251
267 void resetAnsi();
268
278 void setForegroundColor(const RGBColor&);
288 void setBackgroundColor(const RGBColor&);
299 void setForegroundColor(unsigned char, unsigned char, unsigned char);
310 void setBackgroundColor(unsigned char, unsigned char, unsigned char);
320 void setForegroundColor(unsigned char);
330 void setBackgroundColor(unsigned char);
331
342 std::string fgColorStr(ForegroundColor);
353 std::string bgColorStr(BackgroundColor);
364 std::string attrStr(Attribute);
375 std::string fgColorStr(const RGBColor&);
388 std::string bgColorStr(const RGBColor&);
401 std::string fgColorStr(unsigned char, unsigned char, unsigned char);
414 std::string bgColorStr(unsigned char, unsigned char, unsigned char);
415
424 enum class ScreenMode: int {
432 LINE_WRAPPING = 7,
440 };
441
462
481 std::variant<ForegroundColor, RGBColor> foregroundColor;
491 std::variant<BackgroundColor, RGBColor> backgroundColor;
516 std::variant<Attribute, std::bitset<10>> attribute;
517
519 ANSISettings();
525 ANSISettings(const RGBColor&, const RGBColor&, const Attribute&);
537 ANSISettings(const RGBColor&, const BackgroundColor&, const Attribute&);
543 ANSISettings(const ForegroundColor&, const RGBColor&, const Attribute&);
549 ANSISettings(const RGBColor&, const RGBColor&, const std::bitset<10>&);
555 ANSISettings(const ForegroundColor&, const RGBColor&, const std::bitset<10>&);
561 ANSISettings(const RGBColor&, const BackgroundColor&, const std::bitset<10>&);
567 ANSISettings(const ForegroundColor&, const BackgroundColor&, std::initializer_list<Attribute>);
573 ANSISettings(const ForegroundColor&, const RGBColor&, std::initializer_list<Attribute>);
579 ANSISettings(const RGBColor&, const BackgroundColor&, std::initializer_list<Attribute>);
585 ANSISettings(const ForegroundColor&, const BackgroundColor&, const std::bitset<10>&);
591 ANSISettings(const RGBColor&, const RGBColor&, std::initializer_list<Attribute>);
597 ANSISettings(const std::variant<ForegroundColor, RGBColor>&,
598 const std::variant<BackgroundColor, RGBColor>&,
599 const std::variant<Attribute, std::bitset<10>>&);
600
613 void apply() const;
624 void reset() const;
625 };
626};
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
std::bitset< 10 > make_attr_bitset(std::initializer_list< Attribute > attrs)
Helper function to create a bitset from an initializer list of Attributes.
Definition: ansi.hpp:181
Attribute
Enumeration of text attributes for ANSI escape codes.
Definition: ansi.hpp:159
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:122
ScreenMode
Represents various ANSI screen modes.
Definition: ansi.hpp:424
@ 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:140
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:471
ANSISettings(const std::variant< ForegroundColor, RGBColor > &, const std::variant< BackgroundColor, RGBColor > &, const std::variant< Attribute, std::bitset< 10 > > &)
Parametrized constructor with all variants.
void apply() const
Applies the ANSI settings to the terminal.
Definition: ansi.cpp:151
std::variant< ForegroundColor, RGBColor > foregroundColor
Foreground color, can be ForegroundColor or RGBColor.
Definition: ansi.hpp:481
std::variant< BackgroundColor, RGBColor > backgroundColor
Background color, can be BackgroundColor or RGBColor.
Definition: ansi.hpp:491
ANSISettings()
Definition: ansi.cpp:121
void reset() const
Resets all ANSI settings to default values.
Definition: ansi.cpp:174
std::variant< Attribute, std::bitset< 10 > > attribute
Text attribute setting.
Definition: ansi.hpp:516
Represents an RGB color with red, green, and blue components in True Color (24-bit).
Definition: ansi.hpp:196
unsigned char green
Definition: ansi.hpp:198
RGBColor()
Default constructor initializing color to black (0,0,0).
Definition: ansi.cpp:27
unsigned char red
Definition: ansi.hpp:197
unsigned char blue
Definition: ansi.hpp:199