14 releases

0.5.0 Sep 12, 2025
0.4.1 Mar 6, 2023
0.3.0 Dec 11, 2021
0.2.0 Oct 7, 2021
0.1.9 Jul 28, 2021

#257 in Command-line interface

Download history 4008/week @ 2025-12-30 3820/week @ 2026-01-06 4325/week @ 2026-01-13 5644/week @ 2026-01-20 6467/week @ 2026-01-27 8527/week @ 2026-02-03 7406/week @ 2026-02-10 9779/week @ 2026-02-17 11454/week @ 2026-02-24 14754/week @ 2026-03-03 15555/week @ 2026-03-10 15834/week @ 2026-03-17 15508/week @ 2026-03-24 15750/week @ 2026-03-31 18586/week @ 2026-04-07 22107/week @ 2026-04-14

74,714 downloads per month
Used in 53 crates (4 directly)

MIT license

33KB
584 lines

ptyprocess Build codecov Crate docs.rs license

A library provides an interface for a unix PTY/TTY.

It aims to work on all major Unix variants.

The library was developed as a backend for a https://github.com/zhiburt/expectrl. If you're interested in a high level operations may you'd better take a look at zhiburt/expectrl.

Usage

use std::io::{BufRead, BufReader, Result, Write};
use std::process::Command;

use ptyprocess::PtyProcess;

fn main() -> Result<()> {
    // spawn a cat process
    let mut process = PtyProcess::spawn(Command::new("cat"))?;

    // create a communication stream
    let mut stream = process.get_raw_handle()?;

    // send a message to process
    writeln!(stream, "Hello World!")?;

    // read a line from the stream
    let mut reader = BufReader::new(stream);
    let mut buf = String::new();
    reader.read_line(&mut buf)?;

    println!("line was entered {buf:?}");

    // stop the process
    assert!(process.exit(true)?);

    Ok(())
}

Features

  • close-range - optimization for faster PtyProcess::spawn (available on FreeBSD and on Linux since 5.9 and glibc 2.34)

Dependencies

~2MB
~39K SLoC