This example demonstrates how to search terminal contents for a string, navigate between the matches like a find bar, and read the viewport matches used to draw highlights.
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
int main() {
const char *lines[] = {
"$ make test\r\n",
"compiling module A... ok\r\n",
"compiling module B... error: missing semicolon\r\n",
"linking... error: undefined symbol\r\n",
"$ grep -n ERROR build.log\r\n",
};
for (size_t i = 0; i < sizeof(lines) / sizeof(lines[0]); i++) {
strlen(lines[i]));
}
size_t total = 0;
&total);
printf("%zu matches for \"error\"\n", total);
while (true) {
size_t idx = 0;
};
void *values[] = { &idx, &match };
search, sizeof(keys) / sizeof(keys[0]), keys, values, NULL);
printf("selected %zu of %zu\n", idx + 1, total);
if (idx + 1 == total) break;
}
.ptr = viewport_storage,
.cap = sizeof(viewport_storage) / sizeof(viewport_storage[0]),
};
&viewport);
for (
size_t i = 0; i < viewport.
len; i++) {
if (start.
y >= 24 || end.
y >= 24)
continue;
printf("highlight rows %u-%u, cols %u-%u\n",
(
unsigned)start.
y, (
unsigned)end.
y,
(
unsigned)start.
x, (
unsigned)end.
x);
}
return 0;
}
@ GHOSTTY_POINT_TAG_VIEWPORT
GHOSTTY_API GhosttyResult ghostty_search_new(const GhosttyAllocator *allocator, GhosttySearch *out_search, GhosttyTerminal terminal)
struct GhosttySearchImpl * GhosttySearch
GHOSTTY_API GhosttyResult ghostty_search_run(GhosttySearch search)
GHOSTTY_API GhosttyResult ghostty_search_get_multi(GhosttySearch search, size_t count, const GhosttySearchData *keys, void **values, size_t *out_written)
GHOSTTY_API GhosttyResult ghostty_search_set(GhosttySearch search, GhosttySearchOption option, const void *value)
GHOSTTY_API GhosttyResult ghostty_search_get(GhosttySearch search, GhosttySearchData data, void *value)
GHOSTTY_API void ghostty_search_free(GhosttySearch search)
GHOSTTY_API GhosttyResult ghostty_search_feed(GhosttySearch search)
@ GHOSTTY_SEARCH_OPT_NEEDLE
@ GHOSTTY_SEARCH_OPT_SELECT_NEXT
@ GHOSTTY_SEARCH_DATA_VIEWPORT_MATCHES
@ GHOSTTY_SEARCH_DATA_TOTAL_MATCHES
@ GHOSTTY_SEARCH_DATA_SELECTED_MATCH
@ GHOSTTY_SEARCH_DATA_SELECTED_INDEX
GHOSTTY_API GhosttyResult ghostty_terminal_point_from_grid_ref(GhosttyTerminal terminal, const GhosttyGridRef *ref, GhosttyPointTag tag, GhosttyPointCoordinate *out)
struct GhosttyTerminalImpl * GhosttyTerminal
GHOSTTY_API void ghostty_terminal_free(GhosttyTerminal terminal)
GHOSTTY_API void ghostty_terminal_vt_write(GhosttyTerminal terminal, const uint8_t *data, size_t len)
GHOSTTY_API GhosttyResult ghostty_terminal_new(const GhosttyAllocator *allocator, GhosttyTerminal *terminal, uint16_t cols, uint16_t rows)
#define GHOSTTY_INIT_SIZED(type)