![]() |
libghostty
|
Encode and restore the complete state of a terminal via a binary format.
A snapshot is an ordered, authenticated record stream. Its READY checkpoint contains enough state to render and resume the terminal, including any unfinished VT parser input. Older scrollback pages follow READY and the FINISH checkpoint authenticates the complete snapshot.
End-of-file before an operation's required READY or FINISH checkpoint is malformed, truncated snapshot data and returns GHOSTTY_INVALID_VALUE. GHOSTTY_IO_ERROR is reserved for a reader callback that returns false.
The complete working example is available in example/c-vt-snapshot.
Every integer is unsigned and little-endian. The stream begins with this fixed ten-byte envelope:
The envelope is followed by independently checksummed records. A record's CRC32C covers its encoded tag and payload length followed by its payload; it does not cover the CRC field itself.
Record groups occur in this strict order. SCREEN and HISTORY groups contain one entry for each screen declared by TERMINAL. Each manifest is followed by the number of PAGE records it declares. Active SCREEN pages make the terminal renderable; HISTORY pages are older scrollback ordered newest to oldest so an incremental decoder can prepend them as they arrive.
READY authenticates the renderable prefix through CONTINUATION. FINISH authenticates READY and every history record as well as the earlier prefix. Thus record CRC32C detects local corruption while the BLAKE3 checkpoints also bind the ordering and completeness of the record stream.
Snapshot format version 1 is a work in progress and does not yet carry a binary-compatibility guarantee.
Typedefs | |
| typedef struct GhosttySnapshotDecoderImpl * | GhosttySnapshotDecoder |
Enumerations | |
| enum | GhosttySnapshotDecoderOption { GHOSTTY_SNAPSHOT_DECODER_OPT_MAX_CONTINUATION_BYTES = 0 , GHOSTTY_SNAPSHOT_DECODER_OPT_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE } |
| enum | GhosttySnapshotDecoderData { GHOSTTY_SNAPSHOT_DECODER_DATA_INVALID = 0 , GHOSTTY_SNAPSHOT_DECODER_DATA_MAX_CONTINUATION_BYTES = 1 , GHOSTTY_SNAPSHOT_DECODER_DATA_SOURCE_OFFSET = 2 , GHOSTTY_SNAPSHOT_DECODER_DATA_HISTORY_ROWS_PRIMARY = 3 , GHOSTTY_SNAPSHOT_DECODER_DATA_HISTORY_ROWS_ALTERNATE = 4 , GHOSTTY_SNAPSHOT_DECODER_DATA_PROGRESS_SCREEN = 5 , GHOSTTY_SNAPSHOT_DECODER_DATA_PROGRESS_ROWS = 6 , GHOSTTY_SNAPSHOT_DECODER_DATA_PROGRESS_REMAINING = 7 , GHOSTTY_SNAPSHOT_DECODER_DATA_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE } |
| typedef struct GhosttySnapshotDecoderImpl* GhosttySnapshotDecoder |
Queryable snapshot decoder data.
Each variant documents the output pointer type expected by ghostty_snapshot_decoder_get().
| Enumerator | |
|---|---|
| GHOSTTY_SNAPSHOT_DECODER_DATA_INVALID | Invalid data type. Never results in data extraction. |
| GHOSTTY_SNAPSHOT_DECODER_DATA_MAX_CONTINUATION_BYTES | Current maximum accepted continuation size. This value is available in every non-failed decoder state. Output type: size_t * |
| GHOSTTY_SNAPSHOT_DECODER_DATA_SOURCE_OFFSET | Number of snapshot source bytes consumed so far. At FINISH this identifies the first byte after the snapshot. Trailing bytes are not consumed. This value is unavailable after a decoding error, because the decoder can no longer guarantee its source position. Output type: size_t * |
| GHOSTTY_SNAPSHOT_DECODER_DATA_HISTORY_ROWS_PRIMARY | Advisory complete logical history extent for the primary screen. The value counts rows before the active area, including any resident overlap carried before READY. It becomes available after READY validates. Output type: uint64_t * |
| GHOSTTY_SNAPSHOT_DECODER_DATA_HISTORY_ROWS_ALTERNATE | Advisory complete logical history extent for the alternate screen. The value has the same semantics and lifetime as GHOSTTY_SNAPSHOT_DECODER_DATA_HISTORY_ROWS_PRIMARY. Querying it returns GHOSTTY_NO_VALUE when the snapshot does not declare an alternate screen. Output type: uint64_t * |
| GHOSTTY_SNAPSHOT_DECODER_DATA_PROGRESS_SCREEN | Screen associated with the most recently decoded history page. This value is available only after ghostty_snapshot_decoder_next() returns GHOSTTY_SUCCESS. A later call to next replaces it or clears it when FINISH is reached or an error occurs. Output type: GhosttyTerminalScreen * |
| GHOSTTY_SNAPSHOT_DECODER_DATA_PROGRESS_ROWS | Rows prepended by the most recently decoded history page. Zero means the page was consumed and authenticated but could not be applied to the live terminal. Output type: size_t * |
| GHOSTTY_SNAPSHOT_DECODER_DATA_PROGRESS_REMAINING | Page records remaining in the same screen's HISTORY sequence. This is not a count of all pages remaining in the snapshot. Output type: uint32_t * |
Definition at line 150 of file snapshot.h.
Configurable snapshot decoder options.
Options may only be changed before decoding starts. Calling ghostty_snapshot_decoder_set() after ghostty_snapshot_decoder_ready() or ghostty_snapshot_decoder_decode() returns GHOSTTY_INVALID_VALUE.
Definition at line 126 of file snapshot.h.
| GHOSTTY_API GhosttyResult ghostty_snapshot_decoder_decode | ( | GhosttySnapshotDecoder | decoder, |
| GhosttyTerminal * | terminal ) |
Decode and authenticate one complete snapshot.
This is the one-shot form of READY followed by all history pages through FINISH. It may only be called before decoding starts. Bytes following FINISH are left unread. On success terminal receives a caller-owned terminal with its persistent VT stream restored. Continuation tracking on the returned terminal is disabled and GHOSTTY_TERMINAL_DATA_CONTINUATION_MAX_BYTES returns zero. terminal is set to NULL on every error. A decoding, I/O, or allocation error after input consumption begins poisons the decoder, after which it must be freed. An invalid argument or lifecycle error detected before the operation consumes input does not poison it.
| decoder | Decoder handle (must not be NULL) | |
| [out] | Terminal | Pointer to receive the terminal (must not be NULL) |
| GHOSTTY_API void ghostty_snapshot_decoder_free | ( | GhosttySnapshotDecoder | decoder | ) |
Free a snapshot decoder.
This does not release the caller's ownership of a terminal returned by ready or decode. Abandoning an incremental decode leaves that terminal usable with whatever history had already been restored.
| decoder | Decoder to free (may be NULL) |
| GHOSTTY_API GhosttyResult ghostty_snapshot_decoder_get | ( | GhosttySnapshotDecoder | decoder, |
| GhosttySnapshotDecoderData | data, | ||
| void * | out ) |
Get typed data from a snapshot decoder.
The output pointer must have the type documented by data. A phase-dependent value that is not currently available returns GHOSTTY_NO_VALUE.
| decoder | Decoder handle (must not be NULL) | |
| data | Data kind to query | |
| [out] | out | Pointer to receive the value (must not be NULL) |
| GHOSTTY_API GhosttyResult ghostty_snapshot_decoder_get_multi | ( | GhosttySnapshotDecoder | decoder, |
| size_t | count, | ||
| const GhosttySnapshotDecoderData * | keys, | ||
| void ** | values, | ||
| size_t * | out_written ) |
Get multiple snapshot decoder data fields in a single call.
Each keys element selects a data kind and the corresponding values element points to storage of the documented output type. Processing stops at the first error. On success out_written is set to count; on error it is set to the number of values written before the failing key. Invalid array arguments report zero values written.
| decoder | Decoder handle (must not be NULL) | |
| count | Number of key/value pairs | |
| keys | Array of data kinds to query | |
| values | Array of output pointers corresponding to keys | |
| [out] | out_written | Number of successfully written values (may be NULL) |
| GHOSTTY_API GhosttyResult ghostty_snapshot_decoder_new | ( | const GhosttyAllocator * | allocator, |
| GhosttySnapshotDecoder * | decoder, | ||
| GhosttyReader | reader ) |
Create a snapshot decoder that reads from a caller-provided reader.
The decoder stores a copy of reader. Its read callback must not be NULL, and both the callback and its caller-owned context must remain valid until FINISH is reached or the decoder is freed. Reads are synchronous and occur only during ready, next, or decode calls. A zero-byte successful read is permanent end-of-file, not temporary starvation; nonblocking sources must wait outside the decoder or block in their callback. The read callback must not call APIs, including ghostty_snapshot_decoder_free(), on the decoder that owns it. Returning false reports GHOSTTY_IO_ERROR; returning true with zero bytes before a required checkpoint reports truncated snapshot data as GHOSTTY_INVALID_VALUE.
| Memory Management | Allocator for decoder and decoded terminal state, or NULL for the default allocator |
| decoder | Pointer to receive the decoder handle (must not be NULL) |
| reader | Snapshot source reader |
| GHOSTTY_API GhosttyResult ghostty_snapshot_decoder_new_buf | ( | const GhosttyAllocator * | allocator, |
| GhosttySnapshotDecoder * | decoder, | ||
| const uint8_t * | ptr, | ||
| size_t | len ) |
Create a snapshot decoder over a borrowed byte buffer.
The bytes are not copied. ptr must remain valid and immutable until FINISH is reached or the decoder is freed. Bytes after FINISH are not consumed; query GHOSTTY_SNAPSHOT_DECODER_DATA_SOURCE_OFFSET to locate them.
| Memory Management | Allocator for decoder and decoded terminal state, or NULL for the default allocator |
| decoder | Pointer to receive the decoder handle (must not be NULL) |
| ptr | Snapshot source bytes |
| len | Number of source bytes |
| GHOSTTY_API GhosttyResult ghostty_snapshot_decoder_next | ( | GhosttySnapshotDecoder | decoder | ) |
Decode one history page into the terminal returned by READY.
Each GHOSTTY_SUCCESS consumes and authenticates one PAGE record. Query the GHOSTTY_SNAPSHOT_DECODER_DATA_PROGRESS_* values before calling next again. GHOSTTY_NO_VALUE means FINISH was validated; repeated calls after FINISH also return GHOSTTY_NO_VALUE.
The terminal may be rendered, resized, and fed live PTY input between calls. If a history page can no longer be applied safely, it is still consumed and authenticated and progress reports zero rows. The decoder applies history to the caller-owned terminal produced by its READY operation.
A decoding error invalidates the decoder's source position. The terminal remains caller-owned and usable with its already-restored history, but only ghostty_snapshot_decoder_free() may subsequently be called on the decoder.
| decoder | Decoder handle (must not be NULL) |
| GHOSTTY_API GhosttyResult ghostty_snapshot_decoder_ready | ( | GhosttySnapshotDecoder | decoder, |
| GhosttyTerminal * | terminal ) |
Decode and authenticate the renderable snapshot prefix through READY.
On success, terminal receives a caller-owned terminal with its persistent VT stream already restored from the snapshot continuation. The terminal is immediately usable for rendering and live input. Older scrollback remains to be restored with ghostty_snapshot_decoder_next().
The restored parser state may be unfinished, but terminal continuation tracking is disabled; GHOSTTY_TERMINAL_DATA_CONTINUATION_MAX_BYTES returns zero. The decoder's continuation option is an input limit, not terminal runtime policy.
The caller must keep the returned terminal alive until FINISH validates or the decoder is freed. The decoder borrows this terminal handle while it restores history; ghostty_snapshot_decoder_next() uses it automatically.
This operation may only be called once and only before decoding starts. terminal is set to NULL on every error. A decoding, I/O, or allocation error after input consumption begins poisons the decoder, after which it must be freed. An invalid argument or lifecycle error detected before the operation consumes input does not poison it.
| decoder | Decoder handle (must not be NULL) | |
| [out] | Terminal | Pointer to receive the terminal (must not be NULL) |
| GHOSTTY_API GhosttyResult ghostty_snapshot_decoder_set | ( | GhosttySnapshotDecoder | decoder, |
| GhosttySnapshotDecoderOption | option, | ||
| const void * | value ) |
Set a snapshot decoder option.
The value pointer must have the type documented by option. Options may only be changed before decoding starts.
| decoder | Decoder handle (must not be NULL) |
| option | Option to change |
| value | Pointer to the option value (must not be NULL) |
| GHOSTTY_API GhosttyResult ghostty_snapshot_encode | ( | GhosttyTerminal | terminal, |
| GhosttyWriter | writer ) |
Encode a complete terminal snapshot to a writer.
The terminal's persistent VT stream supplies the continuation bytes needed to reconstruct unfinished parser state. The caller must prevent concurrent writes or other terminal mutation for the duration of this call. The writer callback must not call terminal APIs with the same terminal handle. A terminal can be encoded with tracking disabled when its VT parser and UTF-8 decoder are both at ground. If either is unfinished, tracking must have been enabled before the input that produced that state was written; otherwise this returns GHOSTTY_INVALID_VALUE.
Encoding begins at the writer's current position. If an error occurs, the writer may contain a partial snapshot without a valid FINISH checkpoint. Calls to the writer are synchronous; this function does not flush or make the caller's destination durable.
| Terminal | Terminal to encode (must not be NULL) |
| writer | Destination writer whose write callback must not be NULL |
| GHOSTTY_API GhosttyResult ghostty_snapshot_encode_alloc | ( | GhosttyTerminal | terminal, |
| const GhosttyAllocator * | allocator, | ||
| uint8_t ** | out_ptr, | ||
| size_t * | out_len ) |
Encode a complete terminal snapshot to an allocated buffer.
The returned buffer is allocated with allocator, or the default allocator when allocator is NULL. The caller must release it with ghostty_free(), passing the same allocator used here.
A terminal can be encoded with tracking disabled when its VT parser and UTF-8 decoder are both at ground. If either is unfinished, tracking must have been enabled before the input that produced that state was written; otherwise this returns GHOSTTY_INVALID_VALUE.
| Terminal | Terminal to encode (must not be NULL) | |
| Memory Management | Allocator for the output, or NULL for the default allocator | |
| [out] | out_ptr | Allocated snapshot bytes (must not be NULL) |
| [out] | out_len | Number of allocated snapshot bytes (must not be NULL) |
| GHOSTTY_API GhosttyResult ghostty_snapshot_encode_buf | ( | GhosttyTerminal | terminal, |
| uint8_t * | buf, | ||
| size_t | buf_len, | ||
| size_t * | out_written ) |
Encode a complete terminal snapshot to a caller-provided buffer.
Pass NULL for buf with buf_len zero to query the required size. If the buffer is too small, this returns GHOSTTY_OUT_OF_SPACE and stores the required capacity in out_written. A non-NULL undersized buffer may contain a partial snapshot prefix. On success, out_written receives the number of bytes encoded.
A terminal can be encoded with tracking disabled when its VT parser and UTF-8 decoder are both at ground. If either is unfinished, tracking must have been enabled before the input that produced that state was written; otherwise this returns GHOSTTY_INVALID_VALUE.
| Terminal | Terminal to encode (must not be NULL) | |
| buf | Destination buffer, or NULL when buf_len is zero | |
| buf_len | Destination buffer capacity in bytes | |
| [out] | out_written | Bytes written, or required capacity on GHOSTTY_OUT_OF_SPACE (must not be NULL) |