# ptyprocess [](https://github.com/zhiburt/ptyprocess/actions/workflows/ci.yml) [](https://codecov.io/gh/zhiburt/ptyprocess) [](https://crates.io/crates/ptyprocess) [](https://docs.rs/ptyprocess/0.1.0/ptyprocess/) [](./LICENSE.txt)
A library provides an interface for a PTY/TTY.
The library provides a `sync` and `async` IO operations for communication.
To be able to use `async` you must provide a feature flag `[async]`
and turn off default features `default-features = false`.
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
```rust
use ptyprocess::PtyProcess;
use std::process::Command;
use std::io::{Read, Write};
fn main() {
// spawn a cat process
let mut process = PtyProcess::spawn(Command::new("cat")).expect("failed to spawn a process");
// write message to cat.
process.write_all(b"hello cat\n").expect("failed to write");
// read what cat produced.
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");
// stop process
let sucess = process.exit(true).expect("failed to exit");
assert_eq!(sucess, true);
}
```
## Async
```rust
use ptyprocess::PtyProcess;
use std::process::Command;
#[tokio::main]
async fn main() {
// spawns a cat process
let mut process = PtyProcess::spawn(Command::new("cat")).expect("failed to spawn a process");
// sends line to cat
process.send_line("hello cat").await.expect("failed writing");
}
```