Repository navigation
libghostty: add initial C API for terminal, formatter - #11506
Merged
Merged
Conversation
Add a size field as the first member of formatter option structs (TerminalOptions, TerminalOptions.Extra, ScreenOptions.Extra) for ABI compatibility. This allows adding new fields without breaking callers compiled against older versions of the struct. Introduce include/ghostty/vt/types.h as the foundational header containing GhosttyResult and the GHOSTTY_INIT_SIZED macro for zero-initializing sized structs. Remove the separate result.h header, moving its contents into types.h.
Rename the existing format function to format_buf to clarify that it writes into a caller-provided buffer. Add a new format_alloc variant that allocates the output buffer internally using the provided allocator (or the default if NULL). The caller receives the allocated pointer and length and is responsible for freeing it. This is useful for consumers that do not know the required buffer size ahead of time and want to avoid the two-pass query-then-format pattern needed with format_buf.
Add an example showing how to use the ghostty-vt terminal and formatter APIs from C. The example creates a terminal, writes VT-encoded content with cursor movement and styling sequences, then formats the screen contents as plain text using the formatter API.
The Discarding writer count field is u64, but appendNTimes expects usize which is u32 on 32-bit targets like arm-linux-androideabi. Use std.math.cast instead of @intcast to safely handle the conversion, returning WriteFailed on overflow rather than risking undefined behavior.
mitchellh
commented
Mar 14, 2026
Contributor
Author
There was a problem hiding this comment.
I snuck this in here <_<
The Discarding writer count field is u64, but several call sites pass it where a usize is expected. On wasm32-freestanding, usize is 32-bit, so this caused compilation errors. Use std.math.cast instead of a bare @intcast so that overflow is handled gracefully, returning WriteFailed rather than triggering safety-checked undefined behavior at runtime.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds an initial C API for terminals and formatting. There is a new example that shows how to use this.
With these APIs in place, users of the C API can now create a terminal, pass raw VT streams to it, and dump the terminal viewport to various formats. As noted in the docs, the formatter API is not a rendering API, it isn't high performance enough for that. But it's a simpler API to implement than the render state API so I started with that.
Both APIs are purposely fairly minimal, we're just setting the stage for future functionality.
Example
New APIs
ghostty_terminal_newghostty_terminal_freeghostty_terminal_resetghostty_terminal_resizeghostty_terminal_vt_writeghostty_terminal_scroll_viewportghostty_formatter_terminal_newghostty_formatter_format_bufghostty_formatter_format_allocghostty_formatter_freeFuture
vt_writecan invoke callbacks for side effect sequences like clipboards, title setting, responses, etc.terminal.RenderStateC API so that people can build high performance renderers on top of libghostty-vtAnd so on...