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.
A cross-platform PTY, a VT100/VT220/xterm screen, and async Python bindings. One tested stack from raw bytes to styled cells.
$ 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
›
Illustrative session · v0.17.0
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.
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.
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.
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.
The parts that are hard to get right are already here. The parts that are yours stay yours.
A native master on Unix, a ConPTY on Windows. Child exit is event-driven, so waits wake promptly and zombies do not accumulate.
transportFeed bytes once. Get display rows, scrollback, cursor position, window title, styles, dirty rows, and mode state back.
emulationTokio on the Rust side and native asyncio on the Python side. The GIL is released while I/O waits.
I/OA Kitty CSI u encoder distinguishes Shift+Enter from Enter. The flags live with the screen, not in the PTY byte stream.
inputByte-oriented patterns across read chunks, optional regex, and timeout errors that keep the bytes already read.
testing
from_parts, reader()/writer(),
an external vt100 example, and a core that builds
without PyO3.
Rust PtySession carries bytes. It does not presume you want a screen.
ANSI, OSC, and UTF-8 chunking are handled once, in one persistent parser.
PyO3 is optional. The Rust core tests and ships without it.
Start from Python for the shortest path, or keep it in Rust and bring your own frontend. Both surfaces share the same emulator.
pip install stitch-pty
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 type owns the PTY and one screen
use stitch_pty::{TerminalSession, Winsize};
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut term = TerminalSession::spawn(
"bash",
&["-i".into()],
Some(Winsize::new(24, 80, 0, 0)),
None,
1_000,
).await?;
let mut buf = [0u8; 4096];
let _ = term.read_timeout(&mut buf, Duration::from_secs(1)).await?;
println!("{}", term.display().join("\n"));
Ok(())
}
One dependency for the PTY, the screen, and the bindings. Take what you need.