termstyle 1.0.0-pre.3
An open-source C++ header file that enables developers to easily define and apply customizable style presets for terminal outputs such as warnings, infos, and errors.
Loading...
Searching...
No Matches
tests/demo.cpp

Another simple example.

#include <iostream>
#include "../include/termstyle.hpp"
namespace ts = termstyle;
void createAndAddPresets()
{
// Preset for errors
ts::PresetConfig error_preset;
error_preset.prefix.prestyles = {ts::Color(ts::Codes::BRIGHT), ts::Color(ts::Codes::FOREGROUND_RED)};
error_preset.prefix.text = "[ERROR] ";
ts::addPreset("error", error_preset);
// Preset for warnings
ts::PresetConfig warning_preset;
warning_preset.prefix.prestyles = {ts::Color(ts::Codes::BRIGHT), ts::Color(ts::Codes::FOREGROUND_YELLOW)};
warning_preset.prefix.text = "[WARNING] ";
ts::addPreset("warning", warning_preset);
// Preset for info messages
ts::PresetConfig info_preset;
info_preset.prefix.prestyles = {ts::Color(ts::Codes::BRIGHT), ts::Color(ts::Codes::FOREGROUND_CYAN)};
info_preset.prefix.text = "[INFO] ";
ts::addPreset("info", info_preset);
// Preset for success messages
ts::PresetConfig success_preset;
success_preset.prefix.prestyles = {ts::Color(ts::Codes::BRIGHT), ts::Color(ts::Codes::FOREGROUND_GREEN)};
success_preset.prefix.text = "[SUCCESS] ";
ts::addPreset("success", success_preset);
}
int main()
{
createAndAddPresets();
// Print styled messages
ts::print("error", "An error has occurred while processing your request.");
ts::print("warning", "This action will overwrite the existing file.");
ts::print("info", "Updates are available for your system.");
ts::print("success", "The operation completed successfully.");
// Styled output using style() and stream operator
ts::style("error") << "Styled stream: This action is not allowed.";
ts::style("warning") << "Styled stream: Proceed with caution.";
ts::style("info") << "Styled stream: Press 'Y' to continue.";
ts::style("success") << "Styled stream: File saved successfully.";
return 0;
}
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
Struct for storing different types of colors.
Definition termstyle.hpp:384
Struct for storing preset configurations.
Definition termstyle.hpp:512
StyleString prefix
Definition termstyle.hpp:514
std::vector< Color > prestyles
Definition termstyle.hpp:428
std::string text
Definition termstyle.hpp:421