An open-source C++ library that enables developers to easily define and apply customizable style presets for terminal outputs such as warnings, infos, and errors.
Features
- Cross-platform. (Tested on macOS, Windows, and Ubuntu)
- Header-only.
- Customizable style presets.
- Supports ANSI Colors and Styles.
- Supports 256 Color Codes.
- Supports RGB Colors.
Usage
Full documentation here.
Creating a preset
Preset configurations are constructed with termstyle::PresetConfig
.
struct PresetConfig
{
StyleString prefix;
StyleString suffix;
Config config;
};
It contains two termstyle::StyleString
structs - prefix
and suffix
.
- A
prefix
is inserted before the text.
- A
suffix
is inserted after the text.
And a termstyle::config
struct.
A StyleString
struct looks like
struct StyleString
{
std::string text = "";
std::vector<Color> prestyles = {};
std::vector<Color> poststyles = {};
};
Do NOT include a new line character \n
in your suffix. Instead, adjust the config
to your needs.
A Config
struct looks like
struct Config
{
bool leading_restore = true;
bool trailing_restore = true;
bool trailing_newline = true;
};
Example
#include <iostream>
#include "../include/termstyle.hpp"
int main()
{
.poststyles = {
ts::Color(ts::Codes::DIM_RESET)}
}
};
return 0;
}
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::string text
Definition termstyle.hpp:421
Registering a preset
After creating a preset, you nned to register it for use. Simply call
void addPreset(std::string name, PresetConfig preset)
Adds a preset with the given name and configuration.
Definition termstyle.hpp:634
name
is what you want to name the preset. Must be unique, or an error will be thrown.
config
is the preset you just created.
Example
int main()
{
return 0;
}
Header file for the termstyle library.
Using a preset
Finally, you can use the preset you created to print output to the terminal. There are two ways do to it:
- Call
termstyle::print(std::string name, std::string msg);
name
is the name of the preset you registered before.
msg
is the content you want to print.
- Perform
termstyle::style(std::string name) << std::string msg << ...;
name
is the name of the preset you registered before.
- Use it as an
ostream
.
Note that suffix
in this case will be applied after the last msg
of the line.
Example
int main()
{
ts::print(
"debug",
"This is a debug message.");
ts::style(
"debug") <<
"This is another debug message. " <<
"Suffix is applied after me.";
return 0;
}
void print(std::string preset, std::string text="")
Definition termstyle.hpp:666
StyledCout style(const std::string &preset)
Definition termstyle.hpp:717
Examples
Basic usage
int main()
{
.poststyles = {
ts::Color(ts::Codes::DIM_RESET)}
}
};
ts::print(
"debug",
"This is a debug message printed using termstyle::print.");
ts::style(
"debug") <<
"This is a debug message printed using termstyle::style.";
return 0;
}
Output
Color256
#include "../include/termstyle.hpp"
int main()
{
std::vector<ts::PresetConfig> configs(256);
for (int i = 0; i < 256; i++)
{
.text = "[Color #" + std::to_string(i) + "]",
}
};
}
for (int i = 0; i < 256; i++)
{
}
return 0;
}
Struct for storing 256-color codes.
Definition termstyle.hpp:258
std::vector< Color > prestyles
Definition termstyle.hpp:428
Output
RGB Colors
#include "../include/termstyle.hpp"
int main()
{
int r, g, b;
std::cout << "Enter RGB values (0-255): ";
std::cin >> r >> g >> b;
.
text =
"R=" + std::to_string(r) +
" G=" + std::to_string(g) +
" B=" + std::to_string(b) +
": ",
},
.suffix = {
.text = " ",
}
};
return 0;
}
Struct for storing RGB color codes.
Definition termstyle.hpp:332
Output
Fancy Input
#include "../include/termstyle.hpp"
int main()
{
.
text =
"Whatever you type will have a green background",
},
.suffix = {
.text = " >> ",
.prestyles = {ts::Codes::FLASH},
.poststyles = {ts::Codes::FLASH_RESET, ts::Codes::BACKGROUND_GREEN}
},
.config = {
.trailing_restore = false,
.trailing_newline = false
}
};
std::string input;
std::getline(std::cin, input);
std::getline(std::cin, input);
return 0;
}
Output