|
| 1 | +#include <assert.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <string.h> |
| 4 | +#include <ghostty/vt.h> |
| 5 | + |
| 6 | +int main(void) { |
| 7 | + //! [vt-stream-init] |
| 8 | + // Create a terminal |
| 9 | + GhosttyTerminal terminal; |
| 10 | + GhosttyTerminalOptions opts = { |
| 11 | + .cols = 80, |
| 12 | + .rows = 24, |
| 13 | + .max_scrollback = 0, |
| 14 | + }; |
| 15 | + GhosttyResult result = ghostty_terminal_new(NULL, &terminal, opts); |
| 16 | + assert(result == GHOSTTY_SUCCESS); |
| 17 | + //! [vt-stream-init] |
| 18 | + |
| 19 | + //! [vt-stream-write] |
| 20 | + // Feed VT data into the terminal |
| 21 | + const char *text = "Hello, World!\r\n"; |
| 22 | + ghostty_terminal_vt_write(terminal, (const uint8_t *)text, strlen(text)); |
| 23 | + |
| 24 | + // ANSI color codes: ESC[1;32m = bold green, ESC[0m = reset |
| 25 | + text = "\x1b[1;32mGreen Text\x1b[0m\r\n"; |
| 26 | + ghostty_terminal_vt_write(terminal, (const uint8_t *)text, strlen(text)); |
| 27 | + |
| 28 | + // Cursor positioning: ESC[1;1H = move to row 1, column 1 |
| 29 | + text = "\x1b[1;1HTop-left corner\r\n"; |
| 30 | + ghostty_terminal_vt_write(terminal, (const uint8_t *)text, strlen(text)); |
| 31 | + |
| 32 | + // Cursor movement: ESC[5B = move down 5 lines |
| 33 | + text = "\x1b[5B"; |
| 34 | + ghostty_terminal_vt_write(terminal, (const uint8_t *)text, strlen(text)); |
| 35 | + text = "Moved down!\r\n"; |
| 36 | + ghostty_terminal_vt_write(terminal, (const uint8_t *)text, strlen(text)); |
| 37 | + |
| 38 | + // Erase line: ESC[2K = clear entire line |
| 39 | + text = "\x1b[2K"; |
| 40 | + ghostty_terminal_vt_write(terminal, (const uint8_t *)text, strlen(text)); |
| 41 | + text = "New content\r\n"; |
| 42 | + ghostty_terminal_vt_write(terminal, (const uint8_t *)text, strlen(text)); |
| 43 | + |
| 44 | + // Multiple lines |
| 45 | + text = "Line A\r\nLine B\r\nLine C\r\n"; |
| 46 | + ghostty_terminal_vt_write(terminal, (const uint8_t *)text, strlen(text)); |
| 47 | + //! [vt-stream-write] |
| 48 | + |
| 49 | + //! [vt-stream-read] |
| 50 | + // Get the final terminal state as a plain string using the formatter |
| 51 | + GhosttyFormatterTerminalOptions fmt_opts = |
| 52 | + GHOSTTY_INIT_SIZED(GhosttyFormatterTerminalOptions); |
| 53 | + fmt_opts.emit = GHOSTTY_FORMATTER_FORMAT_PLAIN; |
| 54 | + fmt_opts.trim = true; |
| 55 | + |
| 56 | + GhosttyFormatter formatter; |
| 57 | + result = ghostty_formatter_terminal_new(NULL, &formatter, terminal, fmt_opts); |
| 58 | + assert(result == GHOSTTY_SUCCESS); |
| 59 | + |
| 60 | + uint8_t *buf = NULL; |
| 61 | + size_t len = 0; |
| 62 | + result = ghostty_formatter_format_alloc(formatter, NULL, &buf, &len); |
| 63 | + assert(result == GHOSTTY_SUCCESS); |
| 64 | + |
| 65 | + fwrite(buf, 1, len, stdout); |
| 66 | + printf("\n"); |
| 67 | + |
| 68 | + ghostty_free(NULL, buf, len); |
| 69 | + ghostty_formatter_free(formatter); |
| 70 | + //! [vt-stream-read] |
| 71 | + |
| 72 | + ghostty_terminal_free(terminal); |
| 73 | + return 0; |
| 74 | +} |
0 commit comments