mirror-ghostty/example/zig-vt-stream
Mitchell Hashimoto 580262c96f
terminal: add ReadonlyStream that updates terminal state (#9346)
This adds a new stream handler implementation that updates terminal
state in reaction to VT sequences, but doesn't perform any of the
actions that would require responses (e.g. queries).

This is exposed in two ways: first, as a standalone `ReadonlyStream` and
`ReadonlyHandler` type that contains all the implementation. Second, as
a convenience func on `Terminal` as `vtStream` and `vtHandler` which
return their respective types preconfigured to update the calling
terminal state.

This dramatically simplifies libghostty-vt usage from Zig (and will
eventually be exposed to C, too) since a Terminal on its own is ready to
go as a full VT parser and state machine without needing to build any
custom types!

There's a second big bonus here which is that our `stream_readonly.zig`
tests are true end-to-end tests for raw bytes to terminal state. This
will let us test a wider variety of situations more broadly. To start,
there are only a handful of tests implemented here.

**AI disclosure:** Amp wrote basically this whole thing, but I reviewed
it. https://ampcode.com/threads/T-3490efd2-1137-4112-96f6-4bf8a0141ff5
2025-10-25 14:52:33 -07:00
..
src terminal: add ReadonlyStream that updates terminal state (#9346) 2025-10-25 14:52:33 -07:00
README.md terminal: add ReadonlyStream that updates terminal state (#9346) 2025-10-25 14:52:33 -07:00
build.zig terminal: add ReadonlyStream that updates terminal state (#9346) 2025-10-25 14:52:33 -07:00
build.zig.zon terminal: add ReadonlyStream that updates terminal state (#9346) 2025-10-25 14:52:33 -07:00

README.md

Example: vtStream API for Parsing Terminal Streams

This example demonstrates how to use the vtStream API to parse and process VT sequences. The vtStream API is ideal for read-only terminal applications that need to parse terminal output without responding to queries, such as:

  • Replay tooling
  • CI log viewers
  • PaaS builder output
  • etc.

The stream processes VT escape sequences and updates terminal state, while ignoring sequences that require responses (like device status queries).

Requires the Zig version stated in the build.zig.zon file.

Usage

Run the program:

zig build run

The example will process various VT sequences including:

  • Plain text output
  • ANSI color codes
  • Cursor positioning
  • Line clearing
  • Multiple line handling

And display the final terminal state after processing all sequences.