librnr  1.14.5
RoadNarrows Robotics Common Library 1
Rnr_color_ansi

Macros

#define ANSI_CSI   "\033["
 Control Sequence Introducer.
 
#define ANSI_COLOR_PRE   ANSI_CSI
 color escape sequence prefix
 
#define ANSI_COLOR_SEP   ";"
 color values separator
 
#define ANSI_COLOR_MODE   "m"
 color mode
 
#define ANSI_SGR_TEXT_NORMAL   "0"
 color normal and reset to default
 
#define ANSI_SGR_TEXT_BOLD   "1"
 bold or increase intensity
 
#define ANSI_SGR_TEXT_UNDERSCORE   "4"
 underscore
 
#define ANSI_SGR_TEXT_BLINK   "5"
 slow blink
 
#define ANSI_SGR_TEXT_REVERSE   "7"
 swap fg and bg colors
 
#define ANSI_SGR_FG_COLOR_BLACK   "30"
 normal black
 
#define ANSI_SGR_FG_COLOR_RED   "31"
 normal red
 
#define ANSI_SGR_FG_COLOR_GREEN   "32"
 normal green
 
#define ANSI_SGR_FG_COLOR_YELLOW   "33"
 normal yellow (brown)
 
#define ANSI_SGR_FG_COLOR_BLUE   "34"
 normal blue
 
#define ANSI_SGR_FG_COLOR_MAGENTA   "35"
 normal magenta
 
#define ANSI_SGR_FG_COLOR_CYAN   "36"
 normal cyan
 
#define ANSI_SGR_FG_COLOR_WHITE   "37"
 normal white (gray)
 
#define ANSI_SGR_BG_COLOR_BLACK   "40"
 normal black
 
#define ANSI_SGR_BG_COLOR_RED   "41"
 normal red
 
#define ANSI_SGR_BG_COLOR_GREEN   "42"
 normal green
 
#define ANSI_SGR_BG_COLOR_YELLOW   "43"
 normal yellow (brown)
 
#define ANSI_SGR_BG_COLOR_BLUE   "44"
 normal blue
 
#define ANSI_SGR_BG_COLOR_MAGENTA   "45"
 normal magenta
 
#define ANSI_SGR_BG_COLOR_CYAN   "46"
 normal cyan
 
#define ANSI_SGR_BG_COLOR_WHITE   "47"
 normal white (gray)
 
#define ANSI_COLOR(_attr, _fg, _bg)   ANSI_COLOR_PRE _attr ANSI_COLOR_SEP _fg ANSI_COLOR_SEP _bg ANSI_COLOR_MODE
 Macro to build color escape sequence string. More...
 
#define ANSI_FG_COLOR(_fg)   ANSI_COLOR_PRE ANSI_SGR_TEXT_NORMAL ANSI_COLOR_SEP _fg ANSI_COLOR_MODE
 Macro to build normal foreground color escape sequence string. More...
 
#define ANSI_FG_BRIGHT_COLOR(_fg)   ANSI_COLOR_PRE ANSI_SGR_TEXT_BOLD ANSI_COLOR_SEP _fg ANSI_COLOR_MODE
 Macro to build bright foreground color escape sequence string. More...
 
#define ANSI_FG_ATTR_COLOR(_attr, _fg)   ANSI_COLOR_PRE _attr ANSI_COLOR_SEP _fg ANSI_COLOR_MODE
 Macro to build foreground color with the given text attribute escape sequence string. More...
 
#define ANSI_BG_COLOR(_bg)   ANSI_COLOR_PRE _bg ANSI_COLOR_MODE
 Macro to build background color escape sequence string. More...
 
#define ANSI_NORMAL_COLOR(_fg, _bg)   ANSI_COLOR(ANSI_SGR_TEXT_NORMAL, _fg, _bg)
 Macro to build normal color escape sequence string. More...
 
#define ANSI_BRIGHT_COLOR(_fg, _bg)   ANSI_COLOR(ANSI_SGR_TEXT_BOLD, _fg, _bg)
 Macro to build bright color escape sequence string. More...
 
#define ANSI_COLOR_RESET   ANSI_CSI ANSI_SGR_TEXT_NORMAL ANSI_COLOR_MODE
 color reset to default
 

Functions

INLINE_IN_H void set_color (const char *sColor)
 Set foreground or background color. More...
 
INLINE_IN_H void reset_colors ()
 Reset foreground and background colors to defaults.
 
int colorprintf (const char *sFgColor, const char *sFmt,...)
 Print output in the specified foreground color. More...
 
#define ANSI_FG_BLACK   ANSI_FG_COLOR(ANSI_SGR_FG_COLOR_BLACK)
 Foreground colors escape sequences.
 
#define ANSI_FG_RED   ANSI_FG_COLOR(ANSI_SGR_FG_COLOR_RED)
 
#define ANSI_FG_GREEN   ANSI_FG_COLOR(ANSI_SGR_FG_COLOR_GREEN)
 
#define ANSI_FG_YELLOW   ANSI_FG_COLOR(ANSI_SGR_FG_COLOR_YELLOW)
 
#define ANSI_FG_BLUE   ANSI_FG_COLOR(ANSI_SGR_FG_COLOR_BLUE)
 
#define ANSI_FG_MAGENTA   ANSI_FG_COLOR(ANSI_SGR_FG_COLOR_MAGENTA)
 
#define ANSI_FG_CYAN   ANSI_FG_COLOR(ANSI_SGR_FG_COLOR_CYAN)
 
#define ANSI_FG_WHITE   ANSI_FG_COLOR(ANSI_SGR_FG_COLOR_WHITE)
 
#define ANSI_FG_BRIGHT_BLACK   ANSI_FG_BRIGHT_COLOR(ANSI_SGR_FG_COLOR_BLACK)
 
#define ANSI_FG_BRIGHT_RED   ANSI_FG_BRIGHT_COLOR(ANSI_SGR_FG_COLOR_RED)
 
#define ANSI_FG_BRIGHT_GREEN   ANSI_FG_BRIGHT_COLOR(ANSI_SGR_FG_COLOR_GREEN)
 
#define ANSI_FG_BRIGHT_YELLOW   ANSI_FG_BRIGHT_COLOR(ANSI_SGR_FG_COLOR_YELLOW)
 
#define ANSI_FG_BRIGHT_BLUE   ANSI_FG_BRIGHT_COLOR(ANSI_SGR_FG_COLOR_BLUE)
 
#define ANSI_FG_BRIGHT_MAGENTA   ANSI_FG_BRIGHT_COLOR(ANSI_SGR_FG_COLOR_MAGENTA)
 
#define ANSI_FG_BRIGHT_CYAN   ANSI_FG_BRIGHT_COLOR(ANSI_SGR_FG_COLOR_CYAN)
 
#define ANSI_FG_BRIGHT_WHITE   ANSI_FG_BRIGHT_COLOR(ANSI_SGR_FG_COLOR_WHITE)
 
#define ANSI_BG_BLACK   ANSI_FG_COLOR(ANSI_SGR_BG_COLOR_BLACK)
 Background colors escape sequences.
 
#define ANSI_BG_RED   ANSI_FG_COLOR(ANSI_SGR_BG_COLOR_RED)
 
#define ANSI_BG_GREEN   ANSI_FG_COLOR(ANSI_SGR_BG_COLOR_GREEN)
 
#define ANSI_BG_YELLOW   ANSI_FG_COLOR(ANSI_SGR_BG_COLOR_YELLOW)
 
#define ANSI_BG_BLUE   ANSI_FG_COLOR(ANSI_SGR_BG_COLOR_BLUE)
 
#define ANSI_BG_MAGENTA   ANSI_FG_COLOR(ANSI_SGR_BG_COLOR_MAGENTA)
 
#define ANSI_BG_CYAN   ANSI_FG_COLOR(ANSI_SGR_BG_COLOR_CYAN)
 
#define ANSI_BG_WHITE   ANSI_FG_COLOR(ANSI_SGR_BG_COLOR_WHITE)
 

Detailed Description

ANSI escape color codes.

Macro Definition Documentation

#define ANSI_BG_COLOR (   _bg)    ANSI_COLOR_PRE _bg ANSI_COLOR_MODE

Macro to build background color escape sequence string.

Foreground color is left at current setting.

Parameters
_bgForeground color.

Definition at line 137 of file color.h.

#define ANSI_BRIGHT_COLOR (   _fg,
  _bg 
)    ANSI_COLOR(ANSI_SGR_TEXT_BOLD, _fg, _bg)

Macro to build bright color escape sequence string.

Actual colors are dependent on the terminal emulator.

Parameters
_fgForeground color.
_bgBackground color.

Definition at line 157 of file color.h.

#define ANSI_COLOR (   _attr,
  _fg,
  _bg 
)    ANSI_COLOR_PRE _attr ANSI_COLOR_SEP _fg ANSI_COLOR_SEP _bg ANSI_COLOR_MODE

Macro to build color escape sequence string.

Parameters
_attrText attribute.
_fgForeground color.
_bgBackground color.

Definition at line 95 of file color.h.

#define ANSI_FG_ATTR_COLOR (   _attr,
  _fg 
)    ANSI_COLOR_PRE _attr ANSI_COLOR_SEP _fg ANSI_COLOR_MODE

Macro to build foreground color with the given text attribute escape sequence string.

Background color is left at current setting.

Parameters
_attrText attribute.
_fgForeground color.

Definition at line 127 of file color.h.

#define ANSI_FG_BRIGHT_COLOR (   _fg)    ANSI_COLOR_PRE ANSI_SGR_TEXT_BOLD ANSI_COLOR_SEP _fg ANSI_COLOR_MODE

Macro to build bright foreground color escape sequence string.

Background color is left at current setting.

Parameters
_fgForeground color.

Definition at line 115 of file color.h.

#define ANSI_FG_COLOR (   _fg)    ANSI_COLOR_PRE ANSI_SGR_TEXT_NORMAL ANSI_COLOR_SEP _fg ANSI_COLOR_MODE

Macro to build normal foreground color escape sequence string.

Background color is left at current setting.

Parameters
_fgForeground color.

Definition at line 105 of file color.h.

#define ANSI_NORMAL_COLOR (   _fg,
  _bg 
)    ANSI_COLOR(ANSI_SGR_TEXT_NORMAL, _fg, _bg)

Macro to build normal color escape sequence string.

Actual colors are dependent on the terminal emulator.

Parameters
_fgForeground color.
_bgBackground color.

Definition at line 147 of file color.h.

Function Documentation

int colorprintf ( const char *  sFgColor,
const char *  sFmt,
  ... 
)

Print output in the specified foreground color.

After printing, the colors are reset to defaults. Subsequent output will be in the default color.

Parameters
sFgColorANSI SGR foreground color string.
sFmtFormat string following printf(3) syntax.
...Variable argument list to print.
Returns
On success, returns the number of characters printed sans any color escape sequents.
On output error, a negative value is returned.

Definition at line 32 of file color.c.

References reset_colors(), and set_color().

Referenced by reset_colors().

33 {
34  va_list ap;
35  int n;
36 
37  set_color(sFgColor);
38  va_start(ap, sFmt);
39  n = vfprintf(stdout, sFmt, ap);
40  va_end(ap);
41  reset_colors();
42  return n;
43 }
INLINE_IN_H void set_color(const char *sColor)
Set foreground or background color.
Definition: color.h:219
INLINE_IN_H void reset_colors()
Reset foreground and background colors to defaults.
Definition: color.h:227
INLINE_IN_H void set_color ( const char *  sColor)

Set foreground or background color.

The color is in effect until a new foreground/background color is specified or the colors are reset to default values.

Parameters
sColorANSI SGR color string.

Definition at line 219 of file color.h.

References ANSI_COLOR_MODE, and ANSI_COLOR_PRE.

Referenced by colorprintf().

220 {
221  printf("%s%s%s", ANSI_COLOR_PRE, sColor, ANSI_COLOR_MODE);
222 }
#define ANSI_COLOR_MODE
color mode
Definition: color.h:53
#define ANSI_COLOR_PRE
color escape sequence prefix
Definition: color.h:51