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
termstyle

banner
GitHub License GitHub Release Date GitHub Tag

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"
namespace ts = termstyle;
int main()
{
ts::PresetConfig debug_preset = {
.prefix = {
.text = "[DEBUG] ",
.prestyles = {ts::Color(ts::Codes::DIM), ts::Color(ts::Codes::FOREGROUND_CYAN)},
.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

namespace ts = termstyle;
int main()
{
ts::PresetConfig debug_preset = { ... };
ts::addPreset("debug", debug_preset);
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:

  1. 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.
  2. 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

namespace ts = termstyle;
int main()
{
ts::PresetConfig debug_preset = { ... };
ts::addPreset("debug", debug_preset);
// Method 1
ts::print("debug", "This is a debug message.");
// Method 2
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

namespace ts = termstyle;
int main()
{
ts::PresetConfig debug_preset = {
.prefix = {
.text = "[DEBUG] ",
.prestyles = {ts::Color(ts::Codes::DIM), ts::Color(ts::Codes::FOREGROUND_CYAN)},
.poststyles = {ts::Color(ts::Codes::DIM_RESET)}
}
};
ts::addPreset("debug", debug_preset);
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

basic-usage-output

Color256

#include "../include/termstyle.hpp"
namespace ts = termstyle;
int main()
{
std::vector<ts::PresetConfig> configs(256);
for (int i = 0; i < 256; i++)
{
ts::PresetConfig config = {
.prefix = {
.prestyles = {ts::Color(ts::Col256(ts::ColorMode::FOREGROUND, i))},
.text = "[Color #" + std::to_string(i) + "]",
.poststyles = {ts::Color(ts::Codes::RESTORE), ts::Color(ts::Col256(ts::ColorMode::BACKGROUND, i))}
}
};
ts::addPreset("color" + std::to_string(i), config);
}
for (int i = 0; i < 256; i++)
{
ts::print("color" + std::to_string(i), " ");
}
return 0;
}
Struct for storing 256-color codes.
Definition termstyle.hpp:258
std::vector< Color > prestyles
Definition termstyle.hpp:428

Output

color256-output

RGB Colors

#include "../include/termstyle.hpp"
namespace ts = termstyle;
int main()
{
int r, g, b;
std::cout << "Enter RGB values (0-255): ";
std::cin >> r >> g >> b;
ts::PresetConfig rgb_preset = {
.prefix = {
.text = "R=" + std::to_string(r) + " G=" + std::to_string(g) + " B=" + std::to_string(b) + ": ",
.prestyles = {ts::Color(ts::ColRGB(ts::ColorMode::FOREGROUND, r, g, b))}
},
.suffix = {
.text = " ",
.prestyles = {ts::Color(ts::ColRGB(ts::ColorMode::BACKGROUND, r, g, b))}
}
};
ts::addPreset("rgb", rgb_preset);
ts::print("rgb");
return 0;
}
Struct for storing RGB color codes.
Definition termstyle.hpp:332

Output

colrgb-output

Fancy Input

#include "../include/termstyle.hpp"
namespace ts = termstyle;
int main()
{
ts::PresetConfig input_preset = {
.prefix = {
.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
}
};
ts::addPreset("input", input_preset);
std::string input;
ts::print("input");
std::getline(std::cin, input);
ts::style("input");
std::getline(std::cin, input);
return 0;
}

Output

fancy-input-output