vt: expose size_report encoding in the C API

Add ghostty_size_report_encode() to libghostty-vt, following the
same pattern as focus encoding: a single stateless function that
writes a terminal size report escape sequence into a caller-provided
buffer.

The size_report.zig Style enum and Size struct now use lib.Enum and
lib.Struct so the types are automatically C-compatible when building
with c_abi, eliminating the need for duplicate type definitions in
the C wrapper. The C wrapper in c/size_report.zig re-exports these
types directly and provides the callconv(.c) encode entry point.

Supports mode 2048 in-band reports and XTWINOPS responses (CSI 14 t,
CSI 16 t, CSI 18 t).
This commit is contained in:
Mitchell Hashimoto
2026-03-17 16:28:21 -07:00
parent a1d7ad9243
commit 7bf89740dd
6 changed files with 239 additions and 25 deletions

View File

@@ -102,6 +102,7 @@ extern "C" {
#include <ghostty/vt/modes.h>
#include <ghostty/vt/mouse.h>
#include <ghostty/vt/paste.h>
#include <ghostty/vt/size_report.h>
#include <ghostty/vt/wasm.h>
#ifdef __cplusplus

View File

@@ -0,0 +1,126 @@
/**
* @file size_report.h
*
* Size report encoding - encode terminal size reports into escape sequences.
*/
#ifndef GHOSTTY_VT_SIZE_REPORT_H
#define GHOSTTY_VT_SIZE_REPORT_H
/** @defgroup size_report Size Report Encoding
*
* Utilities for encoding terminal size reports into escape sequences,
* supporting in-band size reports (mode 2048) and XTWINOPS responses
* (CSI 14 t, CSI 16 t, CSI 18 t).
*
* ## Basic Usage
*
* Use ghostty_size_report_encode() to encode a size report into a
* caller-provided buffer. If the buffer is too small, the function
* returns GHOSTTY_OUT_OF_SPACE and sets the required size in the
* output parameter.
*
* ## Example
*
* @code{.c}
* #include <stdio.h>
* #include <ghostty/vt.h>
*
* int main() {
* GhosttySizeReportSize size = {
* .rows = 24,
* .columns = 80,
* .cell_width = 9,
* .cell_height = 18,
* };
*
* char buf[64];
* size_t written = 0;
*
* GhosttyResult result = ghostty_size_report_encode(
* GHOSTTY_SIZE_REPORT_MODE_2048, size, buf, sizeof(buf), &written);
*
* if (result == GHOSTTY_SUCCESS) {
* printf("Encoded %zu bytes: ", written);
* fwrite(buf, 1, written, stdout);
* printf("\n");
* }
*
* return 0;
* }
* @endcode
*
* @{
*/
#include <stddef.h>
#include <stdint.h>
#include <ghostty/vt/types.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* Size report style.
*
* Determines the output format for the terminal size report.
*/
typedef enum {
/** In-band size report (mode 2048): ESC [ 48 ; rows ; cols ; height ; width t */
GHOSTTY_SIZE_REPORT_MODE_2048 = 0,
/** XTWINOPS text area size in pixels: ESC [ 4 ; height ; width t */
GHOSTTY_SIZE_REPORT_CSI_14_T = 1,
/** XTWINOPS cell size in pixels: ESC [ 6 ; height ; width t */
GHOSTTY_SIZE_REPORT_CSI_16_T = 2,
/** XTWINOPS text area size in characters: ESC [ 8 ; rows ; cols t */
GHOSTTY_SIZE_REPORT_CSI_18_T = 3,
} GhosttySizeReportStyle;
/**
* Terminal size information for encoding size reports.
*/
typedef struct {
/** Terminal row count in cells. */
uint16_t rows;
/** Terminal column count in cells. */
uint16_t columns;
/** Width of a single terminal cell in pixels. */
uint32_t cell_width;
/** Height of a single terminal cell in pixels. */
uint32_t cell_height;
} GhosttySizeReportSize;
/**
* Encode a terminal size report into an escape sequence.
*
* Encodes a size report in the format specified by @p style into the
* provided buffer.
*
* If the buffer is too small, the function returns GHOSTTY_OUT_OF_SPACE
* and writes the required buffer size to @p out_written. The caller can
* then retry with a sufficiently sized buffer.
*
* @param style The size report format to encode
* @param size Terminal size information
* @param buf Output buffer to write the encoded sequence into (may be NULL)
* @param buf_len Size of the output buffer in bytes
* @param[out] out_written On success, the number of bytes written. On
* GHOSTTY_OUT_OF_SPACE, the required buffer size.
* @return GHOSTTY_SUCCESS on success, GHOSTTY_OUT_OF_SPACE if the buffer
* is too small
*/
GhosttyResult ghostty_size_report_encode(
GhosttySizeReportStyle style,
GhosttySizeReportSize size,
char* buf,
size_t buf_len,
size_t* out_written);
#ifdef __cplusplus
}
#endif
/** @} */
#endif /* GHOSTTY_VT_SIZE_REPORT_H */