CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
Style

Data Structures

struct  ConStyle
 

Macros

#define CON_ColorDefault   0u
 
#define CON_Idx(n)   (0x01000000u | (uint32)(uint8)(n))
 
#define CON_RGB(r, g, b)    (0x02000000u | ((uint32)(uint8)(r) << 16) | ((uint32)(uint8)(g) << 8) | (uint32)(uint8)(b))
 
#define CONSTYLE(fg, attr)   ((ConStyle) { (fg), CON_ColorDefault, (attr) })
 
#define CONSTYLE2(fg, bg, attr)   ((ConStyle) { (fg), (bg), (attr) })
 

Typedefs

typedef struct ConStyle ConStyle
 

Enumerations

enum  CON_ATTR_FLAGS
 

Functions

void conSetStyle (ConStream *con, ConStyle style)
 
void conResetStyle (ConStream *con)
 
void conGetStyle (ConStream *con, ConStyle *out)
 
bool conWriteS (ConStream *con, ConStyle style, const void *buf, size_t sz)
 
bool conPutsS (ConStream *con, ConStyle style, strref s)
 
bool conPutszS (ConStream *con, ConStyle style, const char *sz)
 
bool conPutcS (ConStream *con, ConStyle style, int32 codepoint)
 
bool _conFmtS (ConStream *con, ConStyle style, strref fmt, int n, stvar *args)
 

Detailed Description

Macro Definition Documentation

◆ CON_ColorDefault

#define CON_ColorDefault   0u

The stream's own default foreground/background color – i.e. no color escape is emitted for this half of a ConStyle.

Definition at line 16 of file constyle.h.

◆ CON_Idx

#define CON_Idx (   n)    (0x01000000u | (uint32)(uint8)(n))

uint32 CON_Idx(uint8 n);

A palette color, 0-255. 0-15 are the standard + bright ANSI 16-color entries; 16-255 are the 256-color cube and grayscale ramp popularized by the xterm terminal emulator and now supported by most terminals. Always representable exactly on a CON_Color256 or CON_ColorTrue stream; downgraded to the nearest of the 16 base colors on a CON_Color16 stream.

Definition at line 25 of file constyle.h.

◆ CON_RGB

#define CON_RGB (   r,
  g,
 
)     (0x02000000u | ((uint32)(uint8)(r) << 16) | ((uint32)(uint8)(g) << 8) | (uint32)(uint8)(b))

uint32 CON_RGB(uint8 r, uint8 g, uint8 b);

A 24-bit RGB color. Downgraded to the nearest representable color on streams below CON_ColorTrue.

Definition at line 31 of file constyle.h.

◆ CONSTYLE

#define CONSTYLE (   fg,
  attr 
)    ((ConStyle) { (fg), CON_ColorDefault, (attr) })

ConStyle CONSTYLE(uint32 fg, flags_t attr);

Builds a ConStyle with a foreground color and attributes, default background.

Definition at line 77 of file constyle.h.

◆ CONSTYLE2

#define CONSTYLE2 (   fg,
  bg,
  attr 
)    ((ConStyle) { (fg), (bg), (attr) })

ConStyle CONSTYLE2(uint32 fg, uint32 bg, flags_t attr);

Builds a ConStyle with both a foreground and background color, plus attributes.

Definition at line 82 of file constyle.h.

Typedef Documentation

◆ ConStyle

typedef struct ConStyle ConStyle

A text style: foreground color, background color, and attribute flags. Passed by value – 12 bytes, no lifetime of its own.

Enumeration Type Documentation

◆ CON_ATTR_FLAGS

Text attribute flags for ConStyle::attr. Attributes unsupported by a stream (e.g. italic on the Windows legacy console) are dropped silently rather than emitting garbage.

Definition at line 56 of file constyle.h.

Function Documentation

◆ _conFmtS()

bool _conFmtS ( ConStream *  con,
ConStyle  style,
strref  fmt,
int  n,
stvar args 
)

bool conFmtS(ConStream *con, ConStyle style, strref fmt, ...);

Formats arguments with cx's type-safe formatter (see Formatting) and writes the result with a style applied for the duration of the write, then restores whatever style was active beforehand.

Parameters
conDestination stream
styleStyle to apply for this write
fmtFormat string
Returns
true if formatting and the write both succeeded

Example:

conFmtS(conOut(), CONSTYLE(CON_Red, CON_Bold), _SL("error: ${string}"), stvar(string, msg));
ConStream * conOut(void)
#define CONSTYLE(fg, attr)
Definition constyle.h:77
#define _SL(s)
Inline ASCII string literal with compile-time embedded length (STR_LEN8). Content must be < 200 bytes...
Definition strliteral.h:207
#define stvar(typen, val)
Definition stvar.h:162

◆ conGetStyle()

void conGetStyle ( ConStream *  con,
ConStyle out 
)

Retrieves the style most recently passed to conSetStyle() or conResetStyle() – exactly as requested, not the capability-downgraded form actually emitted.

Parameters
conStream to query
outReceives a copy of the current style

◆ conPutcS()

bool conPutcS ( ConStream *  con,
ConStyle  style,
int32  codepoint 
)

Writes a single Unicode code point with a style applied for the duration of the write, then restores whatever style was active beforehand.

Parameters
conDestination stream
styleStyle to apply for this write
codepointUnicode code point to write
Returns
true on success

◆ conPutsS()

bool conPutsS ( ConStream *  con,
ConStyle  style,
strref  s 
)

Writes a cx string with a style applied for the duration of the write, then restores whatever style was active beforehand. Rope-shaped strings are walked chunk by chunk with no flattening allocation.

Parameters
conDestination stream
styleStyle to apply for this write
sString to write (NULL or empty writes nothing and returns true)
Returns
true on success

◆ conPutszS()

bool conPutszS ( ConStream *  con,
ConStyle  style,
const char *  sz 
)

Writes a NUL-terminated C string with a style applied for the duration of the write, then restores whatever style was active beforehand.

Parameters
conDestination stream
styleStyle to apply for this write
szString to write (NULL or empty writes nothing and returns true)
Returns
true on success

◆ conResetStyle()

void conResetStyle ( ConStream *  con)

Restores a stream to its default (unstyled) appearance. Equivalent to conSetStyle(con, CONSTYLE(CON_ColorDefault, 0)).

Parameters
conStream to reset

◆ conSetStyle()

void conSetStyle ( ConStream *  con,
ConStyle  style 
)

Sets the stream's current style, downgrading colors and dropping unsupported attributes according to the stream's capabilities. Stays in effect for subsequent unstyled writes until the next conSetStyle()/conResetStyle() call.

Parameters
conStream to style
styleStyle to apply

◆ conWriteS()

bool conWriteS ( ConStream *  con,
ConStyle  style,
const void *  buf,
size_t  sz 
)

Writes raw bytes with a style applied for the duration of the write, then restores whatever style was active beforehand. Atomic with respect to other threads.

Parameters
conDestination stream
styleStyle to apply for this write
bufBytes to write
szNumber of bytes
Returns
true on success