Sista v3.1.0
C++ lightweight OS-agnostic library for terminal animations and videogames
Loading...
Searching...
No Matches
ansi.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2022-2026 FLAK-ZOSO <mattia.marchese.2006@gmail.com>
3 * SPDX-License-Identifier: GPL-3.0-only
4 */
5
24#pragma once
25
26#include <variant>
27#include <bitset>
28#include <string>
29
30
44#define ESC "\x1b"
57#define CSI "\x1b["
69#define CLS "\x1b[2J"
81#define SSB "\x1b[3J"
92#define HIDE_CURSOR "\x1b[?25l"
103#define SHOW_CURSOR "\x1b[?25h"
115#define TL "\x1b[H"
116
117
118namespace sista {
127 enum class ForegroundColor: int {
128 BLACK = 30,
129 RED = 31,
130 GREEN = 32,
131 YELLOW = 33,
132 BLUE = 34,
133 MAGENTA = 35,
134 CYAN = 36,
135 WHITE = 37
136 };
145 enum class BackgroundColor: int {
146 BLACK = 40,
147 RED = 41,
148 GREEN = 42,
149 YELLOW = 43,
150 BLUE = 44,
151 MAGENTA = 45,
152 CYAN = 46,
153 WHITE = 47
154 };
164 enum class Attribute: int {
165 RESET = 0,
166 BRIGHT = 1,
167 FAINT = 2,
168 ITALIC = 3,
169 UNDERSCORE = 4,
170 BLINK = 5,
171 RAPID_BLINK = 6,
172 REVERSE = 7,
173 HIDDEN = 8,
174 STRIKETHROUGH = 9
175 };
186 inline std::bitset<10> make_attr_bitset(std::initializer_list<Attribute> attrs) {
187 std::bitset<10> bits;
188 for (auto attr : attrs) {
189 bits.set(static_cast<int>(attr));
190 }
191 return bits;
192 }
193
201 struct RGBColor {
202 unsigned char red;
203 unsigned char green;
204 unsigned char blue;
207 RGBColor();
213 RGBColor(unsigned char, unsigned char, unsigned char);
214 };
215
256
272 void resetAnsi();
273
283 void setForegroundColor(const RGBColor&);
293 void setBackgroundColor(const RGBColor&);
304 void setForegroundColor(unsigned char, unsigned char, unsigned char);
315 void setBackgroundColor(unsigned char, unsigned char, unsigned char);
325 void setForegroundColor(unsigned char);
335 void setBackgroundColor(unsigned char);
336
347 std::string fgColorStr(ForegroundColor);
358 std::string bgColorStr(BackgroundColor);
369 std::string attrStr(Attribute);
380 std::string fgColorStr(const RGBColor&);
393 std::string bgColorStr(const RGBColor&);
406 std::string fgColorStr(unsigned char, unsigned char, unsigned char);
419 std::string bgColorStr(unsigned char, unsigned char, unsigned char);
420
446
467
482 void printHyperlink(std::string, std::string);
483
502 std::variant<ForegroundColor, RGBColor> foregroundColor;
512 std::variant<BackgroundColor, RGBColor> backgroundColor;
537 std::variant<Attribute, std::bitset<10>> attribute;
538
540 ANSISettings();
546 ANSISettings(const RGBColor&, const RGBColor&, const Attribute&);
558 ANSISettings(const RGBColor&, const BackgroundColor&, const Attribute&);
564 ANSISettings(const ForegroundColor&, const RGBColor&, const Attribute&);
570 ANSISettings(const RGBColor&, const RGBColor&, const std::bitset<10>&);
576 ANSISettings(const ForegroundColor&, const RGBColor&, const std::bitset<10>&);
582 ANSISettings(const RGBColor&, const BackgroundColor&, const std::bitset<10>&);
588 ANSISettings(const ForegroundColor&, const BackgroundColor&, std::initializer_list<Attribute>);
594 ANSISettings(const ForegroundColor&, const RGBColor&, std::initializer_list<Attribute>);
600 ANSISettings(const RGBColor&, const BackgroundColor&, std::initializer_list<Attribute>);
606 ANSISettings(const ForegroundColor&, const BackgroundColor&, const std::bitset<10>&);
612 ANSISettings(const RGBColor&, const RGBColor&, std::initializer_list<Attribute>);
618 ANSISettings(const std::variant<ForegroundColor, RGBColor>&,
619 const std::variant<BackgroundColor, RGBColor>&,
620 const std::variant<Attribute, std::bitset<10>>&);
621
634 void apply() const;
645 void reset() const;
646 };
647};
Sista library namespace.
Definition ansi.cpp:31
void resetAnsi()
Resets all ANSI settings to default values.
Definition ansi.cpp:52
void resetAttribute(Attribute attribute)
Resets a text attribute using a predefined Attribute enum.
Definition ansi.cpp:44
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:186
void printHyperlink(std::string text, std::string url)
Prints text as a hyperlink.
Definition ansi.cpp:126
Attribute
Enumeration of text attributes for ANSI escape codes.
Definition ansi.hpp:164
std::string attrStr(Attribute attribute)
Converts an Attribute enum to its corresponding ANSI escape code string.
Definition ansi.cpp:87
ForegroundColor
Enumeration of foreground colors for ANSI escape codes.
Definition ansi.hpp:127
ScreenMode
Represents various ANSI screen modes.
Definition ansi.hpp:429
@ 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:84
void unsetScreenMode(ScreenMode mode)
Unsets a specific screen mode.
Definition ansi.cpp:122
void setBackgroundColor(BackgroundColor color)
Sets the background color using a predefined BackgroundColor enum.
Definition ansi.cpp:38
std::string fgColorStr(ForegroundColor color)
Converts a ForegroundColor enum to its corresponding ANSI escape code string.
Definition ansi.cpp:81
void setForegroundColor(ForegroundColor color)
Sets the foreground color using a predefined ForegroundColor enum.
Definition ansi.cpp:35
BackgroundColor
Enumeration of background colors for ANSI escape codes.
Definition ansi.hpp:145
void setScreenMode(ScreenMode mode)
Sets a specific screen mode.
Definition ansi.cpp:119
void setAttribute(Attribute attribute)
Sets a text attribute using a predefined Attribute enum.
Definition ansi.cpp:41
Represents a set of ANSI settings including colors and attributes.
Definition ansi.hpp:492
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:161
std::variant< ForegroundColor, RGBColor > foregroundColor
Foreground color, can be ForegroundColor or RGBColor.
Definition ansi.hpp:502
std::variant< BackgroundColor, RGBColor > backgroundColor
Background color, can be BackgroundColor or RGBColor.
Definition ansi.hpp:512
ANSISettings()
Definition ansi.cpp:131
void reset() const
Resets all ANSI settings to default values.
Definition ansi.cpp:184
std::variant< Attribute, std::bitset< 10 > > attribute
Text attribute setting.
Definition ansi.hpp:537
Represents an RGB color with red, green, and blue components in True Color (24-bit).
Definition ansi.hpp:201
unsigned char green
Definition ansi.hpp:203
RGBColor()
Default constructor initializing color to black (0,0,0).
Definition ansi.cpp:32
unsigned char red
Definition ansi.hpp:202
unsigned char blue
Definition ansi.hpp:204