Skip to main content

Cells and clusters

int lt_set_cell(int x, int y, lt_uchar ch, lt_attr fg, lt_attr bg);
int lt_get_cell(int x, int y, struct lt_cell *out);
int lt_set_cell_ex(int x, int y, const lt_uchar *chs, size_t nch, lt_attr fg, lt_attr bg);
int lt_extend_cell(int x, int y, lt_uchar ch);
int lt_has_egc(void);

A cell holds one codepoint, or one grapheme cluster, with a foreground and a background. Coordinates are 0-based from the top-left.

lt_set_cell​

int lt_set_cell(int x, int y, lt_uchar ch, lt_attr fg, lt_attr bg);

Sets the back-buffer cell at (x, y) and marks its row dirty. The change appears on the next lt_present. For a multi-codepoint grapheme cluster use lt_set_cell_ex.

Parameters​

NameTypeMeaning
xintcolumn, 0 at the left
yintrow, 0 at the top
chlt_ucharthe Unicode codepoint to show
fglt_attrforeground colour, attribute flags allowed
bglt_attrbackground colour

Returns​

ValueWhen
LT_OKthe cell is set
LT_ERR_OUT_OF_BOUNDS(x, y) is outside the buffer
LT_ERR_NOT_INITbefore lt_init

Example​

lt_set_cell(5, 3, '@', LT_YELLOW | LT_BOLD, LT_DEFAULT); /* a bold yellow hero */
lt_set_cell(6, 3, ' ', LT_DEFAULT, LT_RED); /* a red block */
lt_set_cell(7, 3, 0x2500, LT_CYAN, LT_DEFAULT); /* a box-drawing line */

See also​

lt_print, lt_get_cell, Colors and attributes.

lt_get_cell​

int lt_get_cell(int x, int y, struct lt_cell *out);

Copies the back-buffer cell at (x, y) into *out. This is the buffer your own calls write to, not the frame currently on screen.

Parameters​

NameTypeMeaning
xintcolumn
yintrow
outstruct lt_cell *receives the cell

Returns​

ValueWhen
LT_OK*out holds the cell
LT_ERR_OUT_OF_BOUNDS(x, y) is outside the buffer
LT_ERR_NOT_INITbefore lt_init
LT_ERRout is NULL

Example​

struct lt_cell c;
if (lt_get_cell(x, y, &c) == LT_OK && c.ch == ' ' && c.bg == LT_DEFAULT)
/* the cell is empty */;

See also​

struct lt_cell.

lt_set_cell_ex​

int lt_set_cell_ex(int x, int y, const lt_uchar *chs, size_t nch, lt_attr fg, lt_attr bg);

Writes a grapheme cluster into one cell: a base codepoint followed by combining or joined codepoints, such as e with U+0301 or a ZWJ emoji sequence. The cluster advances the width of its base codepoint. With nch == 1 the call equals lt_set_cell.

Parameters​

NameTypeMeaning
x, yintthe cell
chsconst lt_uchar *chs[0] is the base, chs[1..nch-1] the continuations
nchsize_tnumber of codepoints in chs, at least 1
fg, bglt_attrcolours for the cell

Returns​

ValueWhen
LT_OKthe cluster is set
LT_ERR_OUT_OF_BOUNDS(x, y) is outside the buffer
LT_ERR_NOT_INITbefore lt_init
LT_ERRchs is NULL or nch is 0
LT_ERR_MEMthe cluster table is exhausted

Example​

const lt_uchar e_acute[] = {'e', 0x0301}; /* e + combining acute */
lt_set_cell_ex(0, 0, e_acute, 2, LT_DEFAULT, LT_DEFAULT);

const lt_uchar flag[] = {0x1F1EF, 0x1F1F5}; /* regional indicators J P */
lt_set_cell_ex(2, 0, flag, 2, LT_DEFAULT, LT_DEFAULT);

See also​

lt_extend_cell, Text and Unicode.

lt_extend_cell​

int lt_extend_cell(int x, int y, lt_uchar ch);

Appends one combining or joined codepoint to the cluster already in the cell at (x, y), which is a shortcut for growing a cluster one codepoint at a time. The cell's base must already be set.

Parameters​

NameTypeMeaning
x, yintthe cell
chlt_ucharthe codepoint to append

Returns​

The same codes as lt_set_cell_ex.

Example​

lt_set_cell(0, 0, 'a', LT_DEFAULT, LT_DEFAULT);
lt_extend_cell(0, 0, 0x0308); /* now a with diaeresis */

See also​

lt_set_cell_ex.

lt_has_egc​

int lt_has_egc(void);

Reports whether libterm was built with grapheme-cluster support. It always is, so the function returns 1. It exists for termbox2 parity with tb_has_egc.

Parameters​

None.

Returns​

Non-zero, always 1.

Example​

if (lt_has_egc())
/* lt_set_cell_ex and lt_extend_cell are available */;

See also​

Porting from termbox2.