clutchlog 0.17
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 380 of file clutchlog.h.

Public Member Functions

 fmt ()
 Empty constructor, only useful for a no-op formatter. More...
 
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. More...
 
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 (const short f, const short b, typo s=typo::none)
 
 fmt (const short f, typo s=typo::none)
 
 fmt (fg, const short b, typo s=typo::none)
 
 fmt (const short f, bg, 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)
 

Static Public Member Functions

static fmt hash (const std::string &str, const std::vector< fmt > domain={})
 

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. More...
 

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 class  ansi { colors_16 = -1 , colors_256 = 5 , colors_16M = 2 }
 ANSI code configuring the available number of colors. More...
 
enum class  typo {
  reset = 0 , bold = 1 , underline = 4 , inverse = 7 ,
  none = -1
}
 Typographic style codes. More...
 
enum class  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. More...
 
enum class  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. More...
 

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. More...
 
std::ostream & operator<< (std::ostream &os, const typo &s)
 Output stream operator for a typo tag alone, in 16-colors mode. More...
 
std::ostream & operator<< (std::ostream &os, const fmt &fmt)
 Output stream overload. More...
 

Constructor & Destructor Documentation

◆ fmt() [1/19]

clutchlog::fmt::fmt ( )
inline

Empty constructor, only useful for a no-op formatter.

Definition at line 711 of file clutchlog.h.

◆ fmt() [2/19]

clutchlog::fmt::fmt ( fg  f,
bg  b = bg::none,
typo  s = typo::none 
)
inlineexplicit

Definition at line 715 of file clutchlog.h.

◆ fmt() [3/19]

clutchlog::fmt::fmt ( fg  f,
typo  s,
bg  b = bg::none 
)
inlineexplicit

Definition at line 716 of file clutchlog.h.

◆ fmt() [4/19]

clutchlog::fmt::fmt ( bg  b,
fg  f = fg::none,
typo  s = typo::none 
)
inlineexplicit

Definition at line 717 of file clutchlog.h.

◆ fmt() [5/19]

clutchlog::fmt::fmt ( bg  b,
typo  s,
fg  f = fg::none 
)
inlineexplicit

Definition at line 718 of file clutchlog.h.

◆ fmt() [6/19]

clutchlog::fmt::fmt ( typo  s,
fg  f = fg::none,
bg  b = bg::none 
)
inlineexplicit

Definition at line 719 of file clutchlog.h.

◆ fmt() [7/19]

clutchlog::fmt::fmt ( typo  s,
bg  b,
fg  f = fg::none 
)
inlineexplicit

Definition at line 720 of file clutchlog.h.

◆ fmt() [8/19]

clutchlog::fmt::fmt ( const short  f,
const short  b,
typo  s = typo::none 
)
inlineexplicit

Definition at line 725 of file clutchlog.h.

◆ fmt() [9/19]

clutchlog::fmt::fmt ( const short  f,
typo  s = typo::none 
)
inlineexplicit

Definition at line 726 of file clutchlog.h.

◆ fmt() [10/19]

clutchlog::fmt::fmt ( fg  ,
const short  b,
typo  s = typo::none 
)
inlineexplicit

Definition at line 727 of file clutchlog.h.

◆ fmt() [11/19]

clutchlog::fmt::fmt ( const short  f,
bg  ,
typo  s = typo::none 
)
inlineexplicit

Definition at line 728 of file clutchlog.h.

◆ fmt() [12/19]

clutchlog::fmt::fmt ( const short  fr,
const short  fg,
const short  fb,
const short  gr,
const short  gg,
const short  gb,
typo  s = typo::none 
)
inlineexplicit

Definition at line 733 of file clutchlog.h.

◆ fmt() [13/19]

clutchlog::fmt::fmt ( fg  ,
const short  gr,
const short  gg,
const short  gb,
typo  s = typo::none 
)
inlineexplicit

Definition at line 737 of file clutchlog.h.

◆ fmt() [14/19]

clutchlog::fmt::fmt ( const short  fr,
const short  fg,
const short  fb,
bg  ,
typo  s = typo::none 
)
inlineexplicit

Definition at line 741 of file clutchlog.h.

◆ fmt() [15/19]

clutchlog::fmt::fmt ( const short  fr,
const short  fg,
const short  fb,
typo  s = typo::none 
)
inlineexplicit

Definition at line 744 of file clutchlog.h.

◆ fmt() [16/19]

clutchlog::fmt::fmt ( const std::string &  f,
const std::string &  b,
typo  s = typo::none 
)
inlineexplicit

Definition at line 748 of file clutchlog.h.

◆ fmt() [17/19]

clutchlog::fmt::fmt ( fg  ,
const std::string &  b,
typo  s = typo::none 
)
inlineexplicit

Definition at line 750 of file clutchlog.h.

◆ fmt() [18/19]

clutchlog::fmt::fmt ( const std::string &  f,
bg  ,
typo  s = typo::none 
)
inlineexplicit

Definition at line 752 of file clutchlog.h.

◆ fmt() [19/19]

clutchlog::fmt::fmt ( const std::string &  f,
typo  s = typo::none 
)
inlineexplicit

Definition at line 754 of file clutchlog.h.

Member Function Documentation

◆ print_on()

std::ostream & clutchlog::fmt::print_on ( std::ostream &  os) const
inlineprotected

Print the currently encoded format escape code on the given output stream.

Definition at line 761 of file clutchlog.h.

References back, back_16M, back_256, colors_16, colors_16M, colors_256, fore, fore_16M, fore_256, mode, and style.

Referenced by operator()(), and str().

◆ 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;
Color and style formatter for ANSI terminal escape sequences.
Definition: clutchlog.h:380
Note
A formatter called this way WILL output a reset escape code at the end.

Definition at line 810 of file clutchlog.h.

References print_on().

◆ str()

std::string clutchlog::fmt::str ( ) const
inline

Return the formatting code as a string.

Definition at line 822 of file clutchlog.h.

References print_on().

◆ hash()

static fmt clutchlog::fmt::hash ( const std::string &  str,
const std::vector< fmt domain = {} 
)
inlinestatic

Definition at line 829 of file clutchlog.h.

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 795 of file clutchlog.h.

Member Enumeration Documentation

◆ ansi

enum class 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 383 of file clutchlog.h.

◆ typo

enum class clutchlog::fmt::typo
strong

Typographic style codes.

Definition at line 393 of file clutchlog.h.


The documentation for this class was generated from the following file: