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

This example demonstrates how to track a grid ref as the terminal scrolls, detect when it loses its value, and move it to a new point.

#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <ghostty/vt.h>
static uint32_t codepoint_at_tracked_ref(GhosttyTrackedGridRef tracked) {
GhosttyResult result = ghostty_tracked_grid_ref_snapshot(tracked, &snapshot);
assert(result == GHOSTTY_SUCCESS);
result = ghostty_grid_ref_cell(&snapshot, &cell);
assert(result == GHOSTTY_SUCCESS);
bool has_text = false;
assert(has_text);
uint32_t codepoint = 0;
return codepoint;
}
int main() {
GhosttyTerminal terminal;
.cols = 8,
.rows = 3,
.max_scrollback = 100,
};
GhosttyResult result = ghostty_terminal_new(NULL, &terminal, opts);
assert(result == GHOSTTY_SUCCESS);
const char *text = "alpha\r\n"
"bravo\r\n"
"charlie";
terminal, (const uint8_t *)text, strlen(text));
GhosttyTrackedGridRef tracked = NULL;
GhosttyPoint alpha = {
.value = { .coordinate = { .x = 0, .y = 0 } },
};
result = ghostty_terminal_grid_ref_track(terminal, alpha, &tracked);
assert(result == GHOSTTY_SUCCESS);
// Writing another line scrolls the original "alpha" row into scrollback.
// The tracked ref still follows the same cell.
const char *more = "\r\ndelta";
terminal, (const uint8_t *)more, strlen(more));
printf("tracked codepoint after scroll: %c\n",
(char)codepoint_at_tracked_ref(tracked));
GhosttyPointCoordinate screen = {0};
tracked, GHOSTTY_POINT_TAG_SCREEN, &screen);
assert(result == GHOSTTY_SUCCESS);
printf("tracked screen point: %u,%u\n", screen.x, screen.y);
// Resetting the terminal discards the old grid contents. The tracked
// handle remains valid, but no longer has a meaningful location.
result = ghostty_tracked_grid_ref_snapshot(tracked, &discarded);
assert(result == GHOSTTY_NO_VALUE);
// The same handle can be moved to a new point after it loses its value.
const char *replacement = "echo";
terminal, (const uint8_t *)replacement, strlen(replacement));
GhosttyPoint echo = {
.value = { .coordinate = { .x = 0, .y = 0 } },
};
result = ghostty_tracked_grid_ref_set(tracked, terminal, echo);
assert(result == GHOSTTY_SUCCESS);
printf("tracked codepoint after reset/set: %c\n",
(char)codepoint_at_tracked_ref(tracked));
return 0;
}
GHOSTTY_API GhosttyResult ghostty_tracked_grid_ref_snapshot(GhosttyTrackedGridRef ref, GhosttyGridRef *out_ref)
GHOSTTY_API GhosttyResult ghostty_tracked_grid_ref_point(GhosttyTrackedGridRef ref, GhosttyPointTag tag, GhosttyPointCoordinate *out_point)
GHOSTTY_API bool ghostty_tracked_grid_ref_has_value(GhosttyTrackedGridRef ref)
GHOSTTY_API GhosttyResult ghostty_grid_ref_cell(const GhosttyGridRef *ref, GhosttyCell *out_cell)
GHOSTTY_API GhosttyResult ghostty_tracked_grid_ref_set(GhosttyTrackedGridRef ref, GhosttyTerminal terminal, GhosttyPoint point)
GHOSTTY_API void ghostty_tracked_grid_ref_free(GhosttyTrackedGridRef ref)
struct GhosttyTrackedGridRefImpl * GhosttyTrackedGridRef
Definition types.h:105
@ GHOSTTY_POINT_TAG_ACTIVE
Definition point.h:47
@ GHOSTTY_POINT_TAG_SCREEN
Definition point.h:53
GHOSTTY_API GhosttyResult ghostty_cell_get(GhosttyCell cell, GhosttyCellData data, void *out)
uint64_t GhosttyCell
Definition screen.h:40
@ GHOSTTY_CELL_DATA_HAS_TEXT
Definition screen.h:155
@ GHOSTTY_CELL_DATA_CODEPOINT
Definition screen.h:134
GHOSTTY_API void ghostty_terminal_reset(GhosttyTerminal terminal)
GHOSTTY_API GhosttyResult ghostty_terminal_grid_ref_track(GhosttyTerminal terminal, GhosttyPoint point, GhosttyTrackedGridRef *out_ref)
struct GhosttyTerminalImpl * GhosttyTerminal
Definition types.h:95
GHOSTTY_API GhosttyResult ghostty_terminal_new(const GhosttyAllocator *allocator, GhosttyTerminal *terminal, GhosttyTerminalOptions options)
GHOSTTY_API void ghostty_terminal_free(GhosttyTerminal terminal)
GHOSTTY_API void ghostty_terminal_vt_write(GhosttyTerminal terminal, const uint8_t *data, size_t len)
GhosttyResult
Definition types.h:74
@ GHOSTTY_NO_VALUE
Definition types.h:84
@ GHOSTTY_SUCCESS
Definition types.h:76
#define GHOSTTY_INIT_SIZED(type)
Definition types.h:229