This example demonstrates how to use the terminal and formatter APIs to create a terminal, write VT-encoded content into it, and format the screen contents as plain text.
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
FILE *file;
size_t written;
} OutputWriter;
static bool write_output(void *userdata, const uint8_t *data, size_t len) {
OutputWriter *output = userdata;
size_t offset = 0;
while (offset < len) {
size_t written = fwrite(data + offset, 1, len - offset, output->file);
output->written += written;
offset += written;
if (written == 0) return false;
}
return true;
}
int main() {
const char *commands[] = {
"Line 1: Hello World!\r\n",
("Line 2: \033[1mBold\033[0m and "
"\033[4mUnderline\033[0m\r\n"),
"Line 3: placeholder\r\n",
"\033[3;1H",
"\033[2K",
"Line 3: Overwritten!\r\n",
"\033[5;10H",
"Placed at (5,10)",
"\033[1;72H",
"RIGHT->",
};
for (size_t i = 0; i < sizeof(commands) / sizeof(commands[0]); i++) {
strlen(commands[i]));
}
OutputWriter output = {.file = stdout};
GhosttyWriter writer = {.write = write_output, .userdata = &output};
printf("Formatted output:\n");
printf("\n(%zu bytes)\n", output.written);
return 0;
}
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)