lib-vt: add color scheme report encoder

Add a shared encoder for CSI ? 997 ; Ps n color scheme reports and use 
it for both CSI ? 996 n replies and unsolicited Termio reports. Export the 
same encoder through the libghostty-vt C API with docs and an example.

This is a really light API, arguably easy for consumers to hardcode,
but it didn't match the rest of our style in the libghostty API so we 
should expose it.

Example: GHOSTTY_COLOR_SCHEME_DARK encodes to ESC [ ? 997 ; 1 n,
while GHOSTTY_COLOR_SCHEME_LIGHT encodes to ESC [ ? 997 ; 2 n.
This commit is contained in:
Mitchell Hashimoto
2026-07-04 20:34:53 -07:00
parent 98df7efc83
commit f00e906949
12 changed files with 301 additions and 8 deletions

View File

@@ -126,6 +126,7 @@ extern "C" {
#include <ghostty/vt/allocator.h>
#include <ghostty/vt/build_info.h>
#include <ghostty/vt/color.h>
#include <ghostty/vt/color_scheme.h>
#include <ghostty/vt/device.h>
#include <ghostty/vt/focus.h>
#include <ghostty/vt/formatter.h>

View File

@@ -0,0 +1,73 @@
/**
* @file color_scheme.h
*
* Color scheme report encoding - encode terminal color scheme reports into
* escape sequences.
*/
#ifndef GHOSTTY_VT_COLOR_SCHEME_H
#define GHOSTTY_VT_COLOR_SCHEME_H
/** @defgroup color_scheme Color Scheme Report Encoding
*
* Utilities for encoding color scheme reports into terminal escape
* sequences for color scheme reporting mode (mode 2031).
*
* ## Basic Usage
*
* Use ghostty_color_scheme_report_encode() to encode a color scheme 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
*
* @snippet c-vt-color-scheme/src/main.c color-scheme-report-encode
*
* @{
*/
#include <stddef.h>
#include <ghostty/vt/types.h>
#include <ghostty/vt/device.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* Encode a color scheme report into an escape sequence.
*
* Encodes a color scheme report into the provided buffer. Dark color schemes
* emit ESC [ ? 997 ; 1 n, and light color schemes emit ESC [ ? 997 ; 2 n.
* The encoded bytes are identical to the terminal's internal CSI ? 996 n
* query response.
*
* Hosts should gate unsolicited sends on GHOSTTY_MODE_COLOR_SCHEME_REPORT
* (mode 2031) being set, which can be checked via the mode getters.
*
* 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 scheme The color scheme to encode
* @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
*/
GHOSTTY_API GhosttyResult ghostty_color_scheme_report_encode(
GhosttyColorScheme scheme,
char* buf,
size_t buf_len,
size_t* out_written);
#ifdef __cplusplus
}
#endif
/** @} */
#endif /* GHOSTTY_VT_COLOR_SCHEME_H */