libghostty
Loading...
Searching...
No Matches
Grid Reference

Detailed Description

A grid reference is a resolved reference to a specific cell position in the terminal's internal page structure. Obtain a grid reference from ghostty_terminal_grid_ref(), then extract the cell or row via ghostty_grid_ref_cell() and ghostty_grid_ref_row().

A grid reference is only valid until the next update to the terminal instance. There is no guarantee that a grid reference will remain valid after ANY operation, even if a seemingly unrelated part of the grid is changed, so any information related to the grid reference should be read and cached immediately after obtaining the grid reference.

This API is not meant to be used as the core of render loop. It isn't built to sustain the framerates needed for rendering large screens. Use the render state API for that.

Example

int main() {
// Create a small terminal
GhosttyTerminal terminal;
.cols = 10,
.rows = 3,
.max_scrollback = 0,
};
GhosttyResult result = ghostty_terminal_new(NULL, &terminal, opts);
assert(result == GHOSTTY_SUCCESS);
// Write some content so the grid has interesting data
const char *text = "Hello!\r\n" // Row 0: H e l l o !
"World\r\n" // Row 1: W o r l d
"\033[1mBold"; // Row 2: B o l d (bold style)
terminal, (const uint8_t *)text, strlen(text));
// Get terminal dimensions
uint16_t cols, rows;
// Traverse the entire grid using grid refs
for (uint16_t row = 0; row < rows; row++) {
printf("Row %u: ", row);
for (uint16_t col = 0; col < cols; col++) {
// Resolve the point to a grid reference
GhosttyPoint pt = {
.value = { .coordinate = { .x = col, .y = row } },
};
result = ghostty_terminal_grid_ref(terminal, pt, &ref);
assert(result == GHOSTTY_SUCCESS);
// Read the cell from the grid ref
result = ghostty_grid_ref_cell(&ref, &cell);
assert(result == GHOSTTY_SUCCESS);
// Check if the cell has text
bool has_text = false;
if (has_text) {
uint32_t codepoint = 0;
printf("%c", (char)codepoint);
} else {
printf(".");
}
}
// Also inspect the row for wrap state
GhosttyPoint pt = {
.value = { .coordinate = { .x = 0, .y = row } },
};
ghostty_terminal_grid_ref(terminal, pt, &ref);
GhosttyRow grid_row;
ghostty_grid_ref_row(&ref, &grid_row);
bool wrap = false;
printf(" (wrap=%s", wrap ? "true" : "false");
// Check the style of the first cell with text
ghostty_grid_ref_style(&ref, &style);
printf(", bold=%s)\n", style.bold ? "true" : "false");
}
return 0;
}

Functions

GhosttyResult ghostty_grid_ref_cell (const GhosttyGridRef *ref, GhosttyCell *out_cell)
GhosttyResult ghostty_grid_ref_row (const GhosttyGridRef *ref, GhosttyRow *out_row)
GhosttyResult ghostty_grid_ref_graphemes (const GhosttyGridRef *ref, uint32_t *buf, size_t buf_len, size_t *out_len)
GhosttyResult ghostty_grid_ref_style (const GhosttyGridRef *ref, GhosttyStyle *out_style)

Data Structures

struct  GhosttyGridRef

Function Documentation

◆ ghostty_grid_ref_cell()

GhosttyResult ghostty_grid_ref_cell ( const GhosttyGridRef * ref,
GhosttyCell * out_cell )

Get the cell from a grid reference.

Parameters
refPointer to the grid reference
[out]out_cellOn success, set to the cell at the ref's position (may be NULL)
Returns
GHOSTTY_SUCCESS on success, GHOSTTY_INVALID_VALUE if the ref's node is NULL
Examples
c-vt-grid-traverse/src/main.c.

◆ ghostty_grid_ref_graphemes()

GhosttyResult ghostty_grid_ref_graphemes ( const GhosttyGridRef * ref,
uint32_t * buf,
size_t buf_len,
size_t * out_len )

Get the grapheme cluster codepoints for the cell at the grid reference's position.

Writes the full grapheme cluster (the cell's primary codepoint followed by any combining codepoints) into the provided buffer. If the cell has no text, out_len is set to 0 and GHOSTTY_SUCCESS is returned.

If the buffer is too small (or NULL), the function returns GHOSTTY_OUT_OF_SPACE and writes the required number of codepoints to out_len. The caller can then retry with a sufficiently sized buffer.

Parameters
refPointer to the grid reference
bufOutput buffer of uint32_t codepoints (may be NULL)
buf_lenNumber of uint32_t elements in the buffer
[out]out_lenOn success, the number of codepoints written. On GHOSTTY_OUT_OF_SPACE, the required buffer size in codepoints.
Returns
GHOSTTY_SUCCESS on success, GHOSTTY_INVALID_VALUE if the ref's node is NULL, GHOSTTY_OUT_OF_SPACE if the buffer is too small

◆ ghostty_grid_ref_row()

GhosttyResult ghostty_grid_ref_row ( const GhosttyGridRef * ref,
GhosttyRow * out_row )

Get the row from a grid reference.

Parameters
refPointer to the grid reference
[out]out_rowOn success, set to the row at the ref's position (may be NULL)
Returns
GHOSTTY_SUCCESS on success, GHOSTTY_INVALID_VALUE if the ref's node is NULL
Examples
c-vt-grid-traverse/src/main.c.

◆ ghostty_grid_ref_style()

GhosttyResult ghostty_grid_ref_style ( const GhosttyGridRef * ref,
GhosttyStyle * out_style )

Get the style of the cell at the grid reference's position.

Parameters
refPointer to the grid reference
[out]out_styleOn success, set to the cell's style (may be NULL)
Returns
GHOSTTY_SUCCESS on success, GHOSTTY_INVALID_VALUE if the ref's node is NULL
Examples
c-vt-grid-traverse/src/main.c.