libghostty-vt 0.0.0
Loading...
Searching...
No Matches
c-vt-cmake/src/main.c

Build and link libghostty-vt from a CMake project with FetchContent.

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ghostty/vt.h>
int main() {
// Create a terminal with a small grid
GhosttyTerminal terminal;
GhosttyResult result = ghostty_terminal_new(NULL, &terminal, 80, 24);
assert(result == GHOSTTY_SUCCESS);
// Write some VT-encoded content into the terminal
const char *commands[] = {
"Hello from a \033[1mCMake\033[0m-built program!\r\n",
"Line 2: \033[4munderlined\033[0m text\r\n",
"Line 3: \033[31mred\033[0m \033[32mgreen\033[0m \033[34mblue\033[0m\r\n",
};
for (size_t i = 0; i < sizeof(commands) / sizeof(commands[0]); i++) {
ghostty_terminal_vt_write(terminal, (const uint8_t *)commands[i],
strlen(commands[i]));
}
// Format the terminal contents as plain text
GhosttyFormatter formatter;
result = ghostty_formatter_terminal_new(NULL, &formatter, terminal);
assert(result == GHOSTTY_SUCCESS);
assert(result == GHOSTTY_SUCCESS);
bool trim = true;
assert(result == GHOSTTY_SUCCESS);
uint8_t *buf = NULL;
size_t len = 0;
result = ghostty_formatter_format_alloc(formatter, NULL, &buf, &len);
assert(result == GHOSTTY_SUCCESS);
printf("Plain text (%zu bytes):\n", len);
fwrite(buf, 1, len, stdout);
printf("\n");
ghostty_free(NULL, buf, len);
return 0;
}
GHOSTTY_API void ghostty_free(const GhosttyAllocator *allocator, uint8_t *ptr, size_t len)
GhosttyResult
Definition types.h:143
@ GHOSTTY_SUCCESS
Definition types.h:145
GHOSTTY_API GhosttyResult ghostty_formatter_format_alloc(GhosttyFormatter formatter, const GhosttyAllocator *allocator, uint8_t **out_ptr, size_t *out_len)
GHOSTTY_API GhosttyResult ghostty_formatter_terminal_new(const GhosttyAllocator *allocator, GhosttyFormatter *formatter, GhosttyTerminal terminal)
GHOSTTY_API GhosttyResult ghostty_formatter_set(GhosttyFormatter formatter, GhosttyFormatterOption option, const void *value)
GHOSTTY_API void ghostty_formatter_free(GhosttyFormatter formatter)
GhosttyFormatterFormat
Definition types.h:333
struct GhosttyFormatterImpl * GhosttyFormatter
Definition types.h:304
@ GHOSTTY_FORMATTER_OPT_TRIM
Definition formatter.h:173
@ GHOSTTY_FORMATTER_OPT_EMIT
Definition formatter.h:151
@ GHOSTTY_FORMATTER_FORMAT_PLAIN
Definition types.h:335
struct GhosttyTerminalImpl * GhosttyTerminal
Definition types.h:196
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)