The terminal layer,
stitched in Rust.

A cross-platform PTY, a VT100/VT220/xterm screen, and async Python bindings. One tested stack from raw bytes to styled cells.

v0.17.0 POSIX + ConPTY Rust 1.85+ Python 3.12+
session: bash attached

$ python demo.py

✓ PTY master attached /dev/pts/7

✓ screen attached 80 × 24 · 1,000 scrollback

✓ child watcher armed no zombies

✓ kitty flags 1 · disambiguate

› echo "hello from a real PTY"

hello from a real PTY

›

PtySession HistoryScreen asyncio

Illustrative session · v0.17.0

Native PTY transport VT100 / VT220 / xterm Scrollback + styled cells Rust core without PyO3

Bytes in.
A terminal out.

Most PTY libraries hand you a byte stream and stop. stitch-pty carries the stream through a real screen, then lets you keep the layers together or pull them apart.

Transport · Rust

PtySession owns the wire.

Async reads, writes, resize, bounded drains, EOF, and child exit. Unix uses a native PTY; Windows uses ConPTY with an event-driven exit watcher. The emulator is not required.

Emulation · shared

HistoryScreen owns the state.

ANSI, OSC, UTF-8, styles, cursor, title, modes, dirty rows, and scrollback. Feed it the bytes once and read the screen back as text or styled cells.

Surface · Rust or Python

Your frontend owns the moment.

Keep the layers separate for a multiplexer, or let TerminalSession read and feed in one call. Python's PtySession adds the same convenience with asyncio.

Small surface.
Serious terminal.

The parts that are hard to get right are already here. The parts that are yours stay yours.

Real PTY, not pipes

A native master on Unix, a ConPTY on Windows. Child exit is event-driven, so waits wake promptly and zombies do not accumulate.

transport

A screen, included

Feed bytes once. Get display rows, scrollback, cursor position, window title, styles, dirty rows, and mode state back.

emulation

Async at the edges

Tokio on the Rust side and native asyncio on the Python side. The GIL is released while I/O waits.

I/O

Keys with intent

A Kitty CSI u encoder distinguishes Shift+Enter from Enter. The flags live with the screen, not in the PTY byte stream.

input

Expect, without pexpect

Byte-oriented patterns across read chunks, optional regex, and timeout errors that keep the bytes already read.

testing

Composable by design

from_parts, reader()/writer(), an external vt100 example, and a core that builds without PyO3.

interop

A few things stay separate.

Transport stays transport.

Rust PtySession carries bytes. It does not presume you want a screen.

The screen is a state machine.

ANSI, OSC, and UTF-8 chunking are handled once, in one persistent parser.

Bindings are a layer.

PyO3 is optional. The Rust core tests and ships without it.

Two ecosystems.
One session.

Start from Python for the shortest path, or keep it in Rust and bring your own frontend. Both surfaces share the same emulator.

Python pip install stitch-pty
Rust cargo add stitch-pty

0.17.0 brings a faster parser/screen core: CSI parsing is roughly 2.5× faster than 0.16.0, native parser lanes allocate zero bytes per MiB, and Rust gains path-aware spawn APIs plus the canonical PtyFailure error struct.

# spawn a shell, wait for a line, read the screen
import asyncio
from stitch_pty import Modifiers, spawn

async def main():
    async with spawn("bash", ["-i"]) as term:
        await term.write(b"printf 'ready\n'\n")
        result = await term.expect(b"ready")

        print(result.buffer.decode())
        print(term.display)

        # Shift+Enter becomes CSI 13;2u under Kitty flag 1.
        await term.write(
            term.encode_key("enter", Modifiers.SHIFT)
        )

asyncio.run(main())
one parse · no double feeds UTF-8 safe

Stitch it in.

One dependency for the PTY, the screen, and the bindings. Take what you need.