aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2021-12-29 14:39:53 -0500
committerJesse Luehrs <doy@tozt.net>2021-12-29 14:39:53 -0500
commite0337dbbb0a919d2bff3c22b90d0f0f17bad775d (patch)
tree6b27af50f179441d784e02f85fc9392cc5dbd335 /examples
parentd6e938a3454f70d2b4177d9fb23092f47ad6bb18 (diff)
downloadpty-process-e0337dbbb0a919d2bff3c22b90d0f0f17bad775d.tar.gz
pty-process-e0337dbbb0a919d2bff3c22b90d0f0f17bad775d.zip
simplify
Diffstat (limited to 'examples')
-rw-r--r--examples/async-std.rs15
-rw-r--r--examples/basic.rs25
-rw-r--r--examples/interhack.rs23
-rw-r--r--examples/smol.rs13
-rw-r--r--examples/tokio.rs9
5 files changed, 46 insertions, 39 deletions
diff --git a/examples/async-std.rs b/examples/async-std.rs
index 6bf9412..970841c 100644
--- a/examples/async-std.rs
+++ b/examples/async-std.rs
@@ -7,10 +7,13 @@ mod main {
use async_std::prelude::FutureExt as _;
pub async fn run(
- child: &pty_process::Child,
- ) -> std::result::Result<(), Box<dyn std::error::Error + '_>> {
+ child: &async_process::Child,
+ pty: &pty_process::Pty,
+ ) -> std::result::Result<(), Box<dyn std::error::Error + 'static>> {
let _raw = super::raw_guard::RawGuard::new();
+ let mut input_pty = pty;
+ let mut output_pty = pty;
let ex = async_executor::Executor::new();
let input = ex.spawn(async {
@@ -19,7 +22,7 @@ mod main {
loop {
match stdin.read(&mut buf).await {
Ok(bytes) => {
- child.pty().write_all(&buf[..bytes]).await.unwrap();
+ input_pty.write_all(&buf[..bytes]).await.unwrap();
}
Err(e) => {
eprintln!("stdin read failed: {:?}", e);
@@ -32,7 +35,7 @@ mod main {
let mut buf = [0_u8; 4096];
let mut stdout = async_std::io::stdout();
loop {
- match child.pty().read(&mut buf).await {
+ match output_pty.read(&mut buf).await {
Ok(bytes) => {
stdout.write_all(&buf[..bytes]).await.unwrap();
stdout.flush().await.unwrap();
@@ -64,9 +67,9 @@ fn main() {
pty.resize(pty_process::Size::new(24, 80)).unwrap();
let mut child = pty_process::Command::new("tac")
// .args(&["500"])
- .spawn(pty)
+ .spawn(&pty)
.unwrap();
- main::run(&child).await.unwrap();
+ main::run(&child, &pty).await.unwrap();
child.status().await.unwrap()
});
std::process::exit(
diff --git a/examples/basic.rs b/examples/basic.rs
index c9b5f6c..4f996b9 100644
--- a/examples/basic.rs
+++ b/examples/basic.rs
@@ -4,16 +4,19 @@ mod main {
use std::io::{Read as _, Write as _};
use std::os::unix::io::AsRawFd as _;
- pub fn run(child: &mut pty_process::blocking::Child) {
+ pub fn run(
+ child: &mut std::process::Child,
+ mut pty: &pty_process::blocking::Pty,
+ ) {
let _raw = super::raw_guard::RawGuard::new();
let mut buf = [0_u8; 4096];
- let pty = child.pty().as_raw_fd();
- let stdin = std::io::stdin().as_raw_fd();
+ let pty_fd = pty.as_raw_fd();
+ let stdin_fd = std::io::stdin().as_raw_fd();
loop {
let mut set = nix::sys::select::FdSet::new();
- set.insert(pty);
- set.insert(stdin);
+ set.insert(pty_fd);
+ set.insert(stdin_fd);
match nix::sys::select::select(
None,
Some(&mut set),
@@ -23,8 +26,8 @@ mod main {
) {
Ok(n) => {
if n > 0 {
- if set.contains(pty) {
- match child.pty().read(&mut buf) {
+ if set.contains(pty_fd) {
+ match pty.read(&mut buf) {
Ok(bytes) => {
let buf = &buf[..bytes];
let stdout = std::io::stdout();
@@ -38,11 +41,11 @@ mod main {
}
};
}
- if set.contains(stdin) {
+ if set.contains(stdin_fd) {
match std::io::stdin().read(&mut buf) {
Ok(bytes) => {
let buf = &buf[..bytes];
- child.pty().write_all(buf).unwrap();
+ pty.write_all(buf).unwrap();
}
Err(e) => {
eprintln!("stdin read failed: {:?}", e);
@@ -76,10 +79,10 @@ fn main() {
pty.resize(pty_process::Size::new(24, 80)).unwrap();
let mut child = pty_process::blocking::Command::new("tac")
// .args(&["500"])
- .spawn(pty)
+ .spawn(&pty)
.unwrap();
- main::run(&mut child);
+ main::run(&mut child, &pty);
let status = child.wait().unwrap();
std::process::exit(
diff --git a/examples/interhack.rs b/examples/interhack.rs
index 9d44bbf..212c12e 100644
--- a/examples/interhack.rs
+++ b/examples/interhack.rs
@@ -5,10 +5,13 @@ mod main {
use smol::io::{AsyncReadExt as _, AsyncWriteExt as _};
pub async fn run(
- child: &pty_process::Child,
+ child: &async_process::Child,
+ pty: &pty_process::Pty,
) -> std::result::Result<(), Box<dyn std::error::Error>> {
let _raw = super::raw_guard::RawGuard::new();
+ let mut input_pty = pty;
+ let mut output_pty = pty;
let ex = smol::Executor::new();
let input = ex.spawn(async {
@@ -21,24 +24,18 @@ mod main {
if buf[..bytes].contains(&5u8) {
for byte in buf[..bytes].iter() {
match byte {
- 5u8 => child
- .pty()
+ 5u8 => input_pty
.write_all(b"E- Elbereth\n")
.await
.unwrap(),
- _ => child
- .pty()
+ _ => input_pty
.write_all(&[*byte])
.await
.unwrap(),
}
}
} else {
- child
- .pty()
- .write_all(&buf[..bytes])
- .await
- .unwrap();
+ input_pty.write_all(&buf[..bytes]).await.unwrap();
}
}
Err(e) => {
@@ -54,7 +51,7 @@ mod main {
#[allow(clippy::trivial_regex)]
let re = regex::bytes::Regex::new("Elbereth").unwrap();
loop {
- match child.pty().read(&mut buf).await {
+ match output_pty.read(&mut buf).await {
Ok(bytes) => {
// highlight successful Elbereths
if re.is_match(&buf[..bytes]) {
@@ -102,8 +99,8 @@ fn main() {
let pty = pty_process::Pty::new().unwrap();
pty.resize(pty_process::Size::new(h, w)).unwrap();
let mut child =
- pty_process::Command::new("nethack").spawn(pty).unwrap();
- main::run(&child).await.unwrap();
+ pty_process::Command::new("nethack").spawn(&pty).unwrap();
+ main::run(&child, &pty).await.unwrap();
child.status().await.unwrap()
});
std::process::exit(
diff --git a/examples/smol.rs b/examples/smol.rs
index e8b1c4c..651ff10 100644
--- a/examples/smol.rs
+++ b/examples/smol.rs
@@ -5,10 +5,13 @@ mod main {
use smol::io::{AsyncReadExt as _, AsyncWriteExt as _};
pub async fn run(
- child: &pty_process::Child,
+ child: &async_process::Child,
+ pty: &pty_process::Pty,
) -> std::result::Result<(), Box<dyn std::error::Error>> {
let _raw = super::raw_guard::RawGuard::new();
+ let mut input_pty = pty;
+ let mut output_pty = pty;
let ex = smol::Executor::new();
let input = ex.spawn(async {
@@ -17,7 +20,7 @@ mod main {
loop {
match stdin.read(&mut buf).await {
Ok(bytes) => {
- child.pty().write_all(&buf[..bytes]).await.unwrap();
+ input_pty.write_all(&buf[..bytes]).await.unwrap();
}
Err(e) => {
eprintln!("stdin read failed: {:?}", e);
@@ -30,7 +33,7 @@ mod main {
let mut buf = [0_u8; 4096];
let mut stdout = smol::Unblock::new(std::io::stdout());
loop {
- match child.pty().read(&mut buf).await {
+ match output_pty.read(&mut buf).await {
Ok(bytes) => {
stdout.write_all(&buf[..bytes]).await.unwrap();
stdout.flush().await.unwrap();
@@ -63,9 +66,9 @@ fn main() {
pty.resize(pty_process::Size::new(24, 80)).unwrap();
let mut child = pty_process::Command::new("tac")
// .args(&["500"])
- .spawn(pty)
+ .spawn(&pty)
.unwrap();
- main::run(&child).await.unwrap();
+ main::run(&child, &pty).await.unwrap();
child.status().await.unwrap()
});
std::process::exit(
diff --git a/examples/tokio.rs b/examples/tokio.rs
index 16083c7..55436c2 100644
--- a/examples/tokio.rs
+++ b/examples/tokio.rs
@@ -6,7 +6,8 @@ mod main {
use tokio_util::compat::FuturesAsyncReadCompatExt as _;
pub async fn run(
- child: &pty_process::Child,
+ child: &async_process::Child,
+ pty: &pty_process::Pty,
) -> std::result::Result<(), Box<dyn std::error::Error>> {
let _raw = super::raw_guard::RawGuard::new();
@@ -15,9 +16,9 @@ mod main {
let mut stdin = tokio::io::stdin();
let mut stdout = tokio::io::stdout();
+ let mut pty = pty.compat();
loop {
- let mut pty = child.pty().compat();
tokio::select! {
bytes = stdin.read(&mut in_buf) => match bytes {
Ok(bytes) => {
@@ -55,9 +56,9 @@ async fn main() {
pty.resize(pty_process::Size::new(24, 80)).unwrap();
let mut child = pty_process::Command::new("tac")
// .args(&["500"])
- .spawn(pty)
+ .spawn(&pty)
.unwrap();
- main::run(&child).await.unwrap();
+ main::run(&child, &pty).await.unwrap();
let status = child.status().await.unwrap();
std::process::exit(
status