ptyprocess 0.1.2

A library for cutting a string while preserving colors.
Documentation

ptyprocess Build codecov Crate docs.rs license

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

It has an sync and async backends. Default is sync. You can turn on an async interface by async feature.

Usage

use ptyprocess::PtyProcess;
use std::process::Command;
use std::io::{Read, Write};

fn main() {
    let mut process = PtyProcess::spawn(Command::new("cat")).expect("failed to spawn a process");

    process.write_all(b"hello cat\n").expect("failed to write");

    let mut buf = vec![0; 128];
    let size = process.read(&mut buf).expect("failed to read");
    assert_eq!(&buf[..size], b"hello cat\r\n");

    assert!(process.exit(true).expect("failed toexit"));
}

Async

It must support most runtimes such (tokio, async-std, smol etc.).

use ptyprocess::PtyProcess;
use std::process::Command;
use futures_lite::{AsyncReadExt, AsyncWriteExt};

#[tokio::main]
fn main() {
    let mut process = PtyProcess::spawn(Command::new("cat")).expect("failed to spawn a process");

    process.write_all(b"hello cat\n").await.expect("failed to write");

    let mut buf = vec![0; 128];
    let size = process.read(&mut buf).await.expect("failed to read");
    assert_eq!(&buf[..size], b"hello cat\r\n");

    assert!(process.exit(true).expect("failed toexit"));
}