16#define TERMSTYLE_NODISCARD [[nodiscard]]
18#define TERMSTYLE_ERROR_DEF(parent, name) \
20 name(std::string ename, std::string msg, int exit_code) \
21 : parent(std::move(ename), std::move(msg), exit_code) {} \
22 name(std::string ename, std::string msg, ExitCodes exit_code) \
23 : parent(std::move(ename), std::move(msg), exit_code) {} \
26 name(std::string msg, ExitCodes exit_code) \
27 : parent(#name, std::move(msg), exit_code) {} \
28 name(std::string msg, int exit_code) \
29 : parent(#name, std::move(msg), exit_code) {}
31#define TERMSTYLE_ERROR_SIMPLE(name) \
32 explicit name(std::string msg) : name(#name, msg, ExitCodes::name) {}
34enum class ExitCodes :
int
37 IncorrectConstruction = 100,
53class Error :
public std::runtime_error
56 std::string error_name{
"Error"};
59 TERMSTYLE_NODISCARD
int getExitCode()
const
64 TERMSTYLE_NODISCARD std::string getErrorName()
const
69 Error(std::string name, std::string msg,
int exit_code =
static_cast<int>(ExitCodes::BaseClass))
70 : std::runtime_error(msg), exit_code(exit_code), error_name(name) {}
72 Error(std::string name, std::string msg, ExitCodes exit_code) :
Error(name, msg,
73 static_cast<int>(exit_code)) {}
83 :
PresetNameUsed(
"Preset name \"" + name +
"\" is already used.", ExitCodes::PresetNameUsed) {}
94 :
PresetNotFound(
"Preset \"" + name +
"\" not found.", ExitCodes::PresetNotFound) {}
103#undef TERMSTYLE_ERROR_DEF
104#undef TERMSTYLE_ERROR_SIMPLE
142 UNDERLINE_RESET = 24,
148 FOREGROUND_BLACK = 30,
150 FOREGROUND_GREEN = 32,
151 FOREGROUND_YELLOW = 33,
152 FOREGROUND_BLUE = 34,
153 FOREGROUND_PURPLE = 35,
154 FOREGROUND_CYAN = 36,
155 FOREGROUND_WHITE = 37,
157 FOREGROUND_RESET = 39,
159 BACKGROUND_BLACK = 40,
161 BACKGROUND_GREEN = 42,
162 BACKGROUND_YELLOW = 43,
163 BACKGROUND_BLUE = 44,
164 BACKGROUND_PURPLE = 45,
165 BACKGROUND_CYAN = 46,
166 BACKGROUND_WHITE = 47,
168 BACKGROUND_RESET = 49
188 if (codelist.empty())
return "";
189 std::string res =
"\033[";
190 for (
size_t i = 0; i < codelist.size(); i++)
192 res += std::to_string(
static_cast<int>(codelist[i]));
193 if (i != codelist.size() - 1)
210 return "\033[" + std::to_string(
static_cast<int>(col)) +
"m";
251 return ID >= 0 && ID <= 255;
274 throw BadColorID(
"Invalid color ID: " + std::to_string(
ID));
292 if (codelist.empty())
return "";
293 std::string res =
"\033[";
294 for (
size_t i = 0; i < codelist.size(); i++)
296 res += std::to_string(
static_cast<int>(codelist[i].mode)) +
";5;" + std::to_string(codelist[i].ID);
297 if (i != codelist.size() - 1)
314 std::string res =
"\033[";
315 res += std::to_string(
static_cast<int>(col.mode)) +
";5;";
316 res += std::to_string(col.ID) +
"m";
364 std::string res =
"\033[";
365 res += std::to_string(
static_cast<int>(col.mode)) +
";2;";
366 res += std::to_string(col.r) +
";";
367 res += std::to_string(col.g) +
";";
368 res += std::to_string(col.b) +
"m";
530 std::map<std::string, PresetConfig>
presets = {};
550 if (codelist.empty())
return "";
551 std::string res =
"";
552 for (
size_t i = 0; i < codelist.size(); i++)
554 switch (codelist[i].type)
556 case ColorType::COL16:
559 case ColorType::COL256:
562 case ColorType::COLRGB:
581 if (mode == ParseMode::PREFIX)
591 if (mode == ParseMode::SUFFIX)
600 + (preset.config.trailing_newline ?
"\n" :
"");
602 if (mode == ParseMode::ALL)
620 + (preset.config.trailing_newline ?
"\n" :
"");
650 presets[name] = std::move(preset);
666 void print(std::string preset, std::string text =
"")
673 std::cout <<
parse(config, ParseMode::PREFIX) << text <<
parse(config, ParseMode::SUFFIX);
696 std::cout <<
parse(config, ParseMode::PREFIX);
701 std::cout <<
parse(config, ParseMode::SUFFIX);
705 std::ostream& operator<<(
const T& value)
707 return std::cout << value;
749 std::cout << code2string<Codes>({Codes::RESTORE});
753 OnExit onExitInstance;
Definition termstyle.hpp:98
Base class for all exceptions thrown by the library.
Definition termstyle.hpp:54
Thrown when a preset name is already used.
Definition termstyle.hpp:80
Thrown when a preset is not found.
Definition termstyle.hpp:91
The OnExit class is a helper class that performs an action when it goes out of scope.
Definition termstyle.hpp:739
~OnExit()
Destructor for the OnExit class.
Definition termstyle.hpp:747
A class that provides styled output to the standard output stream.
Definition termstyle.hpp:684
Codes
Enum class for ANSI escape codes.
Definition termstyle.hpp:128
std::string code2string(const std::vector< T > &codelist) noexcept
Definition termstyle.hpp:186
bool validateColorID(int ID)
Validates a color 256 ID.
Definition termstyle.hpp:249
std::string col256_2string(std::vector< Col256 > codelist) noexcept
Definition termstyle.hpp:290
std::string colrgb_2string(const ColRGB &col) noexcept
Definition termstyle.hpp:362
void addPreset(std::string name, PresetConfig preset)
Adds a preset with the given name and configuration.
Definition termstyle.hpp:634
void print(std::string preset, std::string text="")
Definition termstyle.hpp:666
StyledCout style(const std::string &preset)
Definition termstyle.hpp:717
Namespace for the termstyle library.
Definition termstyle.hpp:116
std::string parseColortype(std::vector< Color > codelist) noexcept
Definition termstyle.hpp:548
std::map< std::string, PresetConfig > presets
A map that stores preset configurations.
Definition termstyle.hpp:530
ColorMode
Enum class for color modes.
Definition termstyle.hpp:219
ColorType
Enum class for color types.
Definition termstyle.hpp:228
std::string parse(const PresetConfig &preset, ParseMode mode=ParseMode::ALL) noexcept
Definition termstyle.hpp:579
ParseMode
Enum class for parse modes.
Definition termstyle.hpp:536
Struct for storing 256-color codes.
Definition termstyle.hpp:258
ColorMode mode
Definition termstyle.hpp:264
int ID
Definition termstyle.hpp:270
Struct for storing RGB color codes.
Definition termstyle.hpp:332
ColRGB(ColorMode mode, int r, int g, int b)
Constructs a ColRGB object with the specified color mode and RGB values.
Definition termstyle.hpp:348
ColorMode mode
Definition termstyle.hpp:337
Struct for storing different types of colors.
Definition termstyle.hpp:384
ColRGB colrgb
Definition termstyle.hpp:396
Color(Col256 col256)
Constructs a Color object with the specified 256-color code.
Definition termstyle.hpp:406
Codes col16
Definition termstyle.hpp:392
Color(ColRGB colrgb)
Constructs a Color object with the specified RGB color.
Definition termstyle.hpp:410
ColorType type
Definition termstyle.hpp:389
Color(Codes col16)
Constructs a Color object with the specified 16-color code.
Definition termstyle.hpp:402
Col256 col256
Definition termstyle.hpp:394
Configuration namespace.
Definition termstyle.hpp:486
bool trailing_restore
Definition termstyle.hpp:498
bool trailing_newline
Definition termstyle.hpp:505
bool leading_restore
Definition termstyle.hpp:492
Struct for storing preset configurations.
Definition termstyle.hpp:512
Config config
Definition termstyle.hpp:518
StyleString suffix
Definition termstyle.hpp:516
StyleString prefix
Definition termstyle.hpp:514
Struct for storing styled strings.
Definition termstyle.hpp:417
std::vector< Color > prestyles
Definition termstyle.hpp:428
std::vector< Col256 > poststyle256
Definition termstyle.hpp:479
std::vector< Color > poststyles
Definition termstyle.hpp:435
std::vector< Col256 > prestlye256
Definition termstyle.hpp:468
std::string text
Definition termstyle.hpp:421
std::vector< Codes > prestyle16
Definition termstyle.hpp:446
std::vector< Codes > poststyle16
Definition termstyle.hpp:457