libghostty
Loading...
Searching...
No Matches
c-vt-grid-traverse/src/main.c

This example demonstrates how to traverse the entire terminal grid using grid refs to inspect cell codepoints, row wrap state, and cell styles.

#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <ghostty/vt.h>
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;
}
GhosttyResult ghostty_grid_ref_row(const GhosttyGridRef *ref, GhosttyRow *out_row)
GhosttyResult ghostty_grid_ref_style(const GhosttyGridRef *ref, GhosttyStyle *out_style)
GhosttyResult ghostty_grid_ref_cell(const GhosttyGridRef *ref, GhosttyCell *out_cell)
@ GHOSTTY_POINT_TAG_ACTIVE
Definition point.h:47
GhosttyResult ghostty_row_get(GhosttyRow row, GhosttyRowData data, void *out)
GhosttyResult ghostty_cell_get(GhosttyCell cell, GhosttyCellData data, void *out)
uint64_t GhosttyRow
Definition screen.h:50
uint64_t GhosttyCell
Definition screen.h:39
@ GHOSTTY_CELL_DATA_HAS_TEXT
Definition screen.h:151
@ GHOSTTY_CELL_DATA_CODEPOINT
Definition screen.h:130
@ GHOSTTY_ROW_DATA_WRAP
Definition screen.h:225
struct GhosttyTerminal * GhosttyTerminal
Definition terminal.h:43
GhosttyResult ghostty_terminal_new(const GhosttyAllocator *allocator, GhosttyTerminal *terminal, GhosttyTerminalOptions options)
GhosttyResult ghostty_terminal_grid_ref(GhosttyTerminal terminal, GhosttyPoint point, GhosttyGridRef *out_ref)
GhosttyResult ghostty_terminal_get(GhosttyTerminal terminal, GhosttyTerminalData data, void *out)
void ghostty_terminal_vt_write(GhosttyTerminal terminal, const uint8_t *data, size_t len)
void ghostty_terminal_free(GhosttyTerminal terminal)
@ GHOSTTY_TERMINAL_DATA_COLS
Definition terminal.h:154
@ GHOSTTY_TERMINAL_DATA_ROWS
Definition terminal.h:161
GhosttyResult
Definition types.h:13
@ GHOSTTY_SUCCESS
Definition types.h:15
#define GHOSTTY_INIT_SIZED(type)
Definition types.h:42