clutchlog  0.13
clutchlog::fmt Class Reference

Color and style formatter for ANSI terminal escape sequences. More...

#include <clutchlog.h>

+ Collaboration diagram for clutchlog::fmt:

Detailed Description

Color and style formatter for ANSI terminal escape sequences.

The formatter supports typographic "styles" and colors. Typographic styles are available as named tag in fmt::typo.

The formatter supports the three ANSI modes, which are automatically selected depending the argument types:

  • 16 colors (using named tags),
  • 256 colors (using integers),
  • 16 millions ("true") colors (using RGB integer triplets or web hex strings).

For 16-colors mode, colors are named tag in:

Note
The order in which you pass foreground, background and style does not matter in 16-colors mode.

The following colors are available for both foregrounds (see fmt::fg) and backgrounds (see fmt::bg):

  • none,
  • black,
  • red,
  • green,
  • yellow,
  • blue,
  • magenta,
  • cyan,
  • white,
  • bright_black (i.e. grey),
  • bright_red,
  • bright_green,
  • bright_yellow,
  • bright_blue,
  • bright_magenta,
  • bright_cyan,
  • bright_white.
Note
Some terminal are configured to display colored text set in bold using the bright color counterpart.

For 256-colors mode, colors are expected to be passed as integers in [-1,255] or the fg::none and bg::none tags.

Note
In 256-colors mode, if you want to only encode the background color, you cannot just omit the foreground color, you have to bass a fg::none tag as first argument.

For 16M-colors mode, colors can be encoded as:

  • three integer arguments,
  • a "web color" hexadecimal triplet string, starting with a leading number sign (e.g. "#0055ff").
  • the fg::none and bg::none tags.
Note
In 16M-colors mode, if you want to only encode the background color, you cannot just omit the foreground color, you have to bass a fg::none tag as first argument.
All styles may not be supported by a given terminal/operating system.

Definition at line 366 of file clutchlog.h.

Public Member Functions

 fmt ()
 Empty constructor, only useful for a no-op formatter.
 
std::string operator() (const std::string &msg) const
 Format the given string with the currently encoded format. More...
 
std::string str () const
 Return the formatting code as a string.
 
All combination of 16-colors mode constructors with different parameters orders.
 fmt (fg f, bg b=bg::none, typo s=typo::none)
 
 fmt (fg f, typo s, bg b=bg::none)
 
 fmt (bg b, fg f=fg::none, typo s=typo::none)
 
 fmt (bg b, typo s, fg f=fg::none)
 
 fmt (typo s, fg f=fg::none, bg b=bg::none)
 
 fmt (typo s, bg b, fg f=fg::none)
 
All combination of 256-colors mode constructors with different parameters orders.
 fmt (fg_256 f, bg_256 b, typo s=typo::none)
 
 fmt (fg_256 f, typo s=typo::none)
 
 fmt (fg, bg_256 b, typo s=typo::none)
 
All combination of 16M-colors mode constructors with different parameters orders.
 fmt (const short fr, const short fg, const short fb, const short gr, const short gg, const short gb, typo s=typo::none)
 
 fmt (fg, const short gr, const short gg, const short gb, typo s=typo::none)
 
 fmt (const short fr, const short fg, const short fb, bg, typo s=typo::none)
 
 fmt (const short fr, const short fg, const short fb, typo s=typo::none)
 
 fmt (const std::string &f, const std::string &b, typo s=typo::none)
 
 fmt (fg, const std::string &b, typo s=typo::none)
 
 fmt (const std::string &f, bg, typo s=typo::none)
 
 fmt (const std::string &f, typo s=typo::none)
 

Public Attributes

enum clutchlog::fmt::ansi mode
 Current ANSI color mode.
 
enum clutchlog::fmt::typo style
 Typographic style.
 
enum clutchlog::fmt::fg fore
 Foreground color.
 
enum clutchlog::fmt::bg back
 Background color.
 

Protected Member Functions

std::ostream & print_on (std::ostream &os) const
 Print the currently encoded format escape code on the given output stream.
 

Protected Attributes

clutchlog::fmt::fg_256 fore_256
 Current foreground in 256-colors mode.
 
clutchlog::fmt::bg_256 back_256
 Current background in 256-colors mode.
 
clutchlog::fmt::fg_16M fore_16M
 Current foreground in 16M-colors mode.
 
clutchlog::fmt::bg_16M back_16M
 Current background in 16M-colors mode.
 

Classes

struct  bg_16M
 background in 256-colors mode. More...
 
struct  bg_256
 Background in 256-colors mode. More...
 
struct  color
 Interface class for colors representation. More...
 
struct  color_16M
 Abstract base class for 16M colors objects (24-bits ANSI). More...
 
struct  color_256
 Abstract base class for 256 colors objects (8-bits ANSI). More...
 
struct  fg_16M
 Foreground in 256-colors mode. More...
 
struct  fg_256
 Foreground in 256-colors mode. More...
 

Public Types

enum  ansi { ansi::colors_16 = -1, ansi::colors_256 = 5, ansi::colors_16M = 2 }
 ANSI code configuring the available number of colors. More...
 
enum  typo {
  reset = 0, bold = 1, underline = 4, inverse = 7,
  none = -1
}
 Typographic style codes.
 
enum  fg {
  black = 30, red = 31, green = 32, yellow = 33,
  blue = 34, magenta = 35, cyan = 36, white = 37,
  bright_black = 90, bright_red = 91, bright_green = 92, bright_yellow = 93,
  bright_blue = 94, bright_magenta = 95, bright_cyan = 96, bright_white = 97,
  none = -1
}
 Foreground color codes.
 
enum  bg {
  black = 40, red = 41, green = 42, yellow = 43,
  blue = 44, magenta = 45, cyan = 46, white = 47,
  bright_black = 100, bright_red = 101, bright_green = 102, bright_yellow = 103,
  bright_blue = 104, bright_magenta = 105, bright_cyan = 106, bright_white = 107,
  none = -1
}
 Background color codes.
 

Friends

std::ostream & operator<< (std::ostream &os, const std::tuple< fg, bg, typo > &fbs)
 Output stream operator for a 3-tuple of 16-colors mode tags.
 
std::ostream & operator<< (std::ostream &os, const typo &s)
 Output stream operator for a typo tag alone, in 16-colors mode.
 
std::ostream & operator<< (std::ostream &os, const fmt &fmt)
 Output stream overload. More...
 

Member Function Documentation

◆ operator()()

std::string clutchlog::fmt::operator() ( const std::string &  msg) const
inline

Format the given string with the currently encoded format.

Allow to use a formatter as a function:

clutchlog::fmt error(clutchlog::fmt::fg::red, clutchlog::fmt::typo::bold);
std::cout << error("ERROR") << std::endl;
Note
A formatter called this way WILL output a reset escape code at the end.

Definition at line 795 of file clutchlog.h.

References print_on().

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  os,
const fmt fmt 
)
friend

Output stream overload.

Allow to use a formatter as a tag within a stream:

clutchlog::fmt end(clutchlog::fmt::typo::reset);
clutchlog::fmt error(clutchlog::fmt::fg::red, clutchlog::fmt::typo::bold);
std::cout << error << "ERROR" << end << std::endl;
Note
An formatter called this way will NOT output a reset escape code.

Definition at line 780 of file clutchlog.h.

Member Enumeration Documentation

◆ ansi

enum clutchlog::fmt::ansi
strong

ANSI code configuring the available number of colors.

Enumerator
colors_16 

16 colors mode.

colors_256 

256 colors mode.

colors_16M 

16 millions ("true") colors mode.

Definition at line 369 of file clutchlog.h.


The documentation for this class was generated from the following file:
clutchlog::fmt
Color and style formatter for ANSI terminal escape sequences.
Definition: clutchlog.h:366