Skip to content

A header-only library for in-string color and style tags parsing.

License

Notifications You must be signed in to change notification settings

1410rlH/coltext

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Coltext is an open-source header-only C++ library for applying colors and styles to strings in terminals.

It introduces a new syntax for in-string ANSI effects (styles, background and foreground colors) parsing. With a few simple rules you won't need to care about remembering escape codes or resetting streams!

Table of Contents

Getting Started

Prerequisites

The only things you need to use Coltext is C++ compiler and terminal that supports ANSI escape codes.

Installing

Windows

Currently not supported.

Linux and MacOS

  1. Download latest release or just coltext.hpp file.
  2. Add it to the folder where your code lies.
  3. Add #include "coltext.hpp" to your C++ file.
  4. Compile with -std=c++17.

How to use

In order to use Coltext features you need to acquire syntax of Coltext and cast to its class.

Syntax

Effects should start from a # special symbol, followed by effect's keyword.

Note: if keyword is wrong then Coltext threats it as plain text. NO ERROR TROWN EVER.

4bit Colors

These colors are standard 4bit ANSI colors (8 normal colors + 8 bright). How colors look (and whether they are supported) is up to your terminal specification and settings.

color keywords
black "black", "k"
red "red", "r"
green "green", "g"
yellow "yellow", "y"
blue "blue", "b"
cyan "cyan", "c"
magenta "magenta", "m"
white "white", "w"

Background colors keywords start from big letter. (Example: "Red" and "R")

Their bright versions are accessed by adding "bright_" to long keyword or "b" to short one. (Example: "bright_red" and "br", or "bright_Red" and "bR" for background)

Note: acronyms for colors come from CMYK and RGB color models.

24bit Colors

If your terminal supports 24bit coloring, you can specify color by these:

  • #rgb[r;g;b] for foreground colors

Or

  • #RGB[r;g;b] for background colors.

Where r, g and b are numbers in range [0, 255].

Note: you can NOT add spaces nor change delimiter between [].

Styles

Widly supported styles:

style keywords
bold "bold", "<b>"
faint "faint", "<f>"
italic "italic", "<i>"
underline "underline", "<u>"

Note: # before < is optional. #<b> is equivalent to <b>.

Closing styles by <\> is currently not supported in Coltext. See effect scope.

Not frequently used styles:

style keywords commentary
crossed "crossed" Strikethrough text. May be not supported
blink "blink" Text starts blinking (slowly)
reverse "reverse" Reverse background and foreground color

Usage of those is not so frequent, hence they have no acronym.

Not widely supported styles:

style keywords
double underline "double_underline"
framed "framed"
encircled "encircled"
overlined "overlined"

Note: some of them will work in your terminal, some won't. Deal with it.

Effect scope

Effect scope is controled by 2 ways:

  1. Use parentheses to get as much as you want. (Example: #g(text in green) and not in green)
  2. Use space to get next word. (Example: #r red and not_red)

Escape symbol

There are only 4 symbols in Coltext you may want to escape: #, (, ) and <.

To escape these symbols use \\.

Note: actualy you can leave ( without the escape symbol. It's just made for symmetry.

Examples:

  std::cout
    << "\\#red makes text red\n"_col
    << "\\<bold> makes text bold\n"_col
    << "I love green parentheses! #g( \\(\\) )"_col;

Casting

In order to use features provided by Coltext, you MUST convert std::string to Coltext class.

There are currently 3 ways of doing it:

By constructor:

Coltext ctxt("<b>(Hello, World!)\n");

By dynamic cast:

std::cout << (Coltext) "<b>(Hello, World!)\n";

By literal:

std::cout << "<b>(Hello, World!)\n"_col;

Running the tests

Compile and run tests.cpp file.

Versioning

We use SemVer for versioning. For the versions available, see the releases on this repository.

Authors

License

This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.

Acknowledgments

  • C++ for the difficulties of coloring text
  • CSS for '#' special symbol
  • HTML for style tags
  • Ikalnytskyi/termcolor for initial idea

About

A header-only library for in-string color and style tags parsing.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages