rlib
Convenience library for useful things
Loading...
Searching...
No Matches
rtty.h File Reference

TTY-aware printing: ANSI SGR escape sequences, isatty detection and overridable stdout / stderr sinks. More...

#include <rlib/rtypes.h>
#include <rlib/rclr.h>
#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>

Go to the source code of this file.

Macros

#define R_TTY_MAX_CC   32
 Maximum length of a colour-control escape produced by r_tty_clr_to_str.
 
#define R_TTY_CSI   "\x1B["
 ANSI Control Sequence Introducer: "ESC[".
 
#define R_TTY_SGR1(param)   R_TTY_CSI param "m"
 Build a 1-parameter SGR escape.
 
#define R_TTY_SGR_RESET   R_TTY_SGR1 (R_TTY_SGR_CLS_ARG)
 Reset all SGR attributes.
 
#define R_TTY_SGR(P1)   R_TTY_SGR1 (P1)
 Alias for R_TTY_SGR1.
 
#define R_TTY_SGR2(P1, P2)   R_TTY_SGR1 (P1";"P2)
 2-parameter SGR escape.
 
#define R_TTY_SGR3(P1, P2, P3)   R_TTY_SGR1 (P1";"P2";"P3)
 3-parameter SGR escape.
 
#define R_TTY_SGR4(P1, P2, P3, P4)   R_TTY_SGR1 (P1";"P2";"P3";"P4)
 4-parameter SGR escape.
 
#define r_isatty   isatty
 
#define r_fileno   fileno
 
#define r_printf   printf
 Alias for C-library printf.
 
#define r_vprintf   vprintf
 Alias for C-library vprintf.
 
#define r_fprintf   fprintf
 Alias for C-library fprintf.
 
#define r_vfprintf   vfprintf
 Alias for C-library vfprintf.
 
SGR parameter tokens

String literals that name the standard SGR codes; combine into escape sequences with R_TTY_SGR* macros.

#define R_TTY_SGR_CLS_ARG   "0"
 
#define R_TTY_SGR_BOLD_ARG   "1"
 
#define R_TTY_SGR_NOBOLD_ARG   "21"
 
#define R_TTY_SGR_FAINT_ARG   "2"
 
#define R_TTY_SGR_NORMAL_ARG   "22"
 
#define R_TTY_SGR_ITALIC_ARG   "3"
 
#define R_TTY_SGR_NOITALIC_ARG   "23"
 
#define R_TTY_SGR_UNDERLINE_ARG   "4"
 
#define R_TTY_SGR_NOUNDERLINE_ARG   "24"
 
#define R_TTY_SGR_BLINK_ARG   "5"
 
#define R_TTY_SGR_NOBLINK_ARG   "25"
 
#define R_TTY_SGR_INVERSE_ARG   "7"
 
#define R_TTY_SGR_NOINVERSE_ARG   "27"
 
#define R_TTY_SGR_CONCEAL_ARG   "8"
 
#define R_TTY_SGR_REVEAL_ARG   "28"
 
#define R_TTY_SGR_FG_ARG(CLR)   "3"R_STRINGIFY (CLR)
 Build a foreground colour token (30-39).
 
#define R_TTY_SGR_BG_ARG(CLR)   "4"R_STRINGIFY (CLR)
 Build a background colour token (40-49).
 

Typedefs

typedef rboolean(* RPrintFunc) (const rchar *str, rsize size, rpointer data)
 Print-sink signature: receive a chunk of text and decide how to handle it (write to stdio, append to a buffer, ...).
 

Functions

rchar * r_tty_clr_to_str (RColorFlags clr, rchar str[R_TTY_MAX_CC])
 Render a colour-flag bitmask into a CSI escape sequence.
 
int r_print (const rchar *fmt,...)
 Print to stdout via the currently-installed print handler.
 
int r_printerr (const rchar *fmt,...)
 Print to stderr via the currently-installed printerr handler.
 
void r_override_print_handler (RPrintFunc func, rpointer data, RPrintFunc *oldfunc, rpointer *olddata)
 Install a new sink for r_print.
 
void r_override_printerr_handler (RPrintFunc func, rpointer data, RPrintFunc *oldfunc, rpointer *olddata)
 Like r_override_print_handler but for r_printerr.
 
rboolean r_print_default (const rchar *str, rsize size, rpointer data)
 Default stdout sink (writes via the C library).
 
rboolean r_printerr_default (const rchar *str, rsize size, rpointer data)
 Default stderr sink (writes via the C library).
 

Detailed Description

TTY-aware printing: ANSI SGR escape sequences, isatty detection and overridable stdout / stderr sinks.