aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2022-02-22 17:02:12 -0500
committerJesse Luehrs <doy@tozt.net>2022-02-23 02:44:18 -0500
commit39287b07f87aba15c4cb0f64d7008ba67289151d (patch)
treea0ce5fee6e98a7c429f668000d7cdc71cf8d4797 /examples
parentebcf5f15081f6a84c861eb2aecbf962396a88695 (diff)
downloadpty-process-39287b07f87aba15c4cb0f64d7008ba67289151d.tar.gz
pty-process-39287b07f87aba15c4cb0f64d7008ba67289151d.zip
another rewrite
Diffstat (limited to 'examples')
-rw-r--r--examples/async-std.rs85
-rw-r--r--examples/basic.rs9
-rw-r--r--examples/interhack.rs87
-rw-r--r--examples/smol.rs84
-rw-r--r--examples/tokio.rs17
5 files changed, 48 insertions, 234 deletions
diff --git a/examples/async-std.rs b/examples/async-std.rs
deleted file mode 100644
index 970841c..0000000
--- a/examples/async-std.rs
+++ /dev/null
@@ -1,85 +0,0 @@
-mod raw_guard;
-
-#[cfg(feature = "async")]
-mod main {
- use async_std::io::prelude::WriteExt as _;
- use async_std::io::ReadExt as _;
- use async_std::prelude::FutureExt as _;
-
- pub async fn run(
- 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 {
- let mut buf = [0_u8; 4096];
- let mut stdin = async_std::io::stdin();
- loop {
- match stdin.read(&mut buf).await {
- Ok(bytes) => {
- input_pty.write_all(&buf[..bytes]).await.unwrap();
- }
- Err(e) => {
- eprintln!("stdin read failed: {:?}", e);
- break;
- }
- }
- }
- });
- let output = ex.spawn(async {
- let mut buf = [0_u8; 4096];
- let mut stdout = async_std::io::stdout();
- loop {
- match output_pty.read(&mut buf).await {
- Ok(bytes) => {
- stdout.write_all(&buf[..bytes]).await.unwrap();
- stdout.flush().await.unwrap();
- }
- Err(e) => {
- eprintln!("pty read failed: {:?}", e);
- break;
- }
- }
- }
- });
-
- let wait = async {
- child.status_no_drop().await.unwrap();
- };
-
- ex.run(input.race(output).race(wait)).await;
-
- Ok(())
- }
-}
-
-#[cfg(feature = "async")]
-fn main() {
- use std::os::unix::process::ExitStatusExt as _;
-
- let status = async_std::task::block_on(async {
- let pty = pty_process::Pty::new().unwrap();
- pty.resize(pty_process::Size::new(24, 80)).unwrap();
- let mut child = pty_process::Command::new("tac")
- // .args(&["500"])
- .spawn(&pty)
- .unwrap();
- main::run(&child, &pty).await.unwrap();
- child.status().await.unwrap()
- });
- std::process::exit(
- status
- .code()
- .unwrap_or_else(|| status.signal().unwrap_or(0) + 128),
- );
-}
-
-#[cfg(not(feature = "async"))]
-fn main() {
- unimplemented!()
-}
diff --git a/examples/basic.rs b/examples/basic.rs
index 4f996b9..8a56e57 100644
--- a/examples/basic.rs
+++ b/examples/basic.rs
@@ -6,7 +6,7 @@ mod main {
pub fn run(
child: &mut std::process::Child,
- mut pty: &pty_process::blocking::Pty,
+ pty: &mut pty_process::blocking::Pty,
) {
let _raw = super::raw_guard::RawGuard::new();
let mut buf = [0_u8; 4096];
@@ -75,14 +75,15 @@ mod main {
fn main() {
use std::os::unix::process::ExitStatusExt as _;
- let pty = pty_process::blocking::Pty::new().unwrap();
+ let mut pty = pty_process::blocking::Pty::new().unwrap();
+ let pts = pty.pts().unwrap();
pty.resize(pty_process::Size::new(24, 80)).unwrap();
let mut child = pty_process::blocking::Command::new("tac")
// .args(&["500"])
- .spawn(&pty)
+ .spawn(&pts)
.unwrap();
- main::run(&mut child, &pty);
+ main::run(&mut child, &mut pty);
let status = child.wait().unwrap();
std::process::exit(
diff --git a/examples/interhack.rs b/examples/interhack.rs
index 212c12e..5fe1474 100644
--- a/examples/interhack.rs
+++ b/examples/interhack.rs
@@ -2,68 +2,63 @@ mod raw_guard;
#[cfg(feature = "async")]
mod main {
- use smol::io::{AsyncReadExt as _, AsyncWriteExt as _};
+ use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
pub async fn run(
- child: &async_process::Child,
- pty: &pty_process::Pty,
+ child: &mut tokio::process::Child,
+ pty: &mut 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 mut in_buf = [0_u8; 4096];
+ let mut out_buf = [0_u8; 4096];
- let input = ex.spawn(async {
- let mut buf = [0_u8; 4096];
- let mut stdin = smol::Unblock::new(std::io::stdin());
- loop {
- match stdin.read(&mut buf).await {
+ let mut stdin = tokio::io::stdin();
+ let mut stdout = tokio::io::stdout();
+
+ #[allow(clippy::trivial_regex)]
+ let re = regex::bytes::Regex::new("Elbereth").unwrap();
+
+ loop {
+ tokio::select! {
+ bytes = stdin.read(&mut in_buf) => match bytes {
Ok(bytes) => {
// engrave Elbereth with ^E
- if buf[..bytes].contains(&5u8) {
- for byte in buf[..bytes].iter() {
+ if in_buf[..bytes].contains(&5u8) {
+ for byte in in_buf[..bytes].iter() {
match byte {
- 5u8 => input_pty
+ 5u8 => pty
.write_all(b"E- Elbereth\n")
.await
.unwrap(),
- _ => input_pty
+ _ => pty
.write_all(&[*byte])
.await
.unwrap(),
}
}
} else {
- input_pty.write_all(&buf[..bytes]).await.unwrap();
+ pty.write_all(&in_buf[..bytes]).await.unwrap();
}
}
Err(e) => {
eprintln!("stdin read failed: {:?}", e);
break;
}
- }
- }
- });
- let output = ex.spawn(async {
- let mut buf = [0_u8; 4096];
- let mut stdout = smol::Unblock::new(std::io::stdout());
- #[allow(clippy::trivial_regex)]
- let re = regex::bytes::Regex::new("Elbereth").unwrap();
- loop {
- match output_pty.read(&mut buf).await {
+ },
+ bytes = pty.read(&mut out_buf) => match bytes {
Ok(bytes) => {
// highlight successful Elbereths
- if re.is_match(&buf[..bytes]) {
+ if re.is_match(&out_buf[..bytes]) {
stdout
.write_all(&re.replace_all(
- &buf[..bytes],
+ &out_buf[..bytes],
&b"\x1b[35m$0\x1b[m"[..],
))
.await
.unwrap();
} else {
- stdout.write_all(&buf[..bytes]).await.unwrap();
+ stdout.write_all(&out_buf[..bytes]).await.unwrap();
}
stdout.flush().await.unwrap();
}
@@ -71,38 +66,26 @@ mod main {
eprintln!("pty read failed: {:?}", e);
break;
}
- }
+ },
+ _ = child.wait() => break,
}
- });
-
- let wait = async {
- child.status_no_drop().await.unwrap();
- };
-
- ex.run(smol::future::or(smol::future::or(input, output), wait))
- .await;
+ }
Ok(())
}
}
#[cfg(feature = "async")]
-fn main() {
+#[tokio::main]
+async fn main() {
use std::os::unix::process::ExitStatusExt as _;
- let (w, h) = if let Some((w, h)) = term_size::dimensions() {
- (w as u16, h as u16)
- } else {
- (80, 24)
- };
- let status = smol::block_on(async {
- 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, &pty).await.unwrap();
- child.status().await.unwrap()
- });
+ let mut pty = pty_process::Pty::new().unwrap();
+ let pts = pty.pts().unwrap();
+ pty.resize(pty_process::Size::new(24, 80)).unwrap();
+ let mut child = pty_process::Command::new("nethack").spawn(&pts).unwrap();
+ main::run(&mut child, &mut pty).await.unwrap();
+ let status = child.wait().await.unwrap();
std::process::exit(
status
.code()
diff --git a/examples/smol.rs b/examples/smol.rs
deleted file mode 100644
index 651ff10..0000000
--- a/examples/smol.rs
+++ /dev/null
@@ -1,84 +0,0 @@
-mod raw_guard;
-
-#[cfg(feature = "async")]
-mod main {
- use smol::io::{AsyncReadExt as _, AsyncWriteExt as _};
-
- pub async fn run(
- 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 {
- let mut buf = [0_u8; 4096];
- let mut stdin = smol::Unblock::new(std::io::stdin());
- loop {
- match stdin.read(&mut buf).await {
- Ok(bytes) => {
- input_pty.write_all(&buf[..bytes]).await.unwrap();
- }
- Err(e) => {
- eprintln!("stdin read failed: {:?}", e);
- break;
- }
- }
- }
- });
- let output = ex.spawn(async {
- let mut buf = [0_u8; 4096];
- let mut stdout = smol::Unblock::new(std::io::stdout());
- loop {
- match output_pty.read(&mut buf).await {
- Ok(bytes) => {
- stdout.write_all(&buf[..bytes]).await.unwrap();
- stdout.flush().await.unwrap();
- }
- Err(e) => {
- eprintln!("pty read failed: {:?}", e);
- break;
- }
- }
- }
- });
-
- let wait = async {
- child.status_no_drop().await.unwrap();
- };
-
- ex.run(smol::future::or(smol::future::or(input, output), wait))
- .await;
-
- Ok(())
- }
-}
-
-#[cfg(feature = "async")]
-fn main() {
- use std::os::unix::process::ExitStatusExt as _;
-
- let status = smol::block_on(async {
- let pty = pty_process::Pty::new().unwrap();
- pty.resize(pty_process::Size::new(24, 80)).unwrap();
- let mut child = pty_process::Command::new("tac")
- // .args(&["500"])
- .spawn(&pty)
- .unwrap();
- main::run(&child, &pty).await.unwrap();
- child.status().await.unwrap()
- });
- std::process::exit(
- status
- .code()
- .unwrap_or_else(|| status.signal().unwrap_or(0) + 128),
- );
-}
-
-#[cfg(not(feature = "async"))]
-fn main() {
- unimplemented!()
-}
diff --git a/examples/tokio.rs b/examples/tokio.rs
index 55436c2..dc11855 100644
--- a/examples/tokio.rs
+++ b/examples/tokio.rs
@@ -3,11 +3,10 @@ mod raw_guard;
#[cfg(feature = "async")]
mod main {
use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
- use tokio_util::compat::FuturesAsyncReadCompatExt as _;
pub async fn run(
- child: &async_process::Child,
- pty: &pty_process::Pty,
+ child: &mut tokio::process::Child,
+ pty: &mut pty_process::Pty,
) -> std::result::Result<(), Box<dyn std::error::Error>> {
let _raw = super::raw_guard::RawGuard::new();
@@ -16,7 +15,6 @@ mod main {
let mut stdin = tokio::io::stdin();
let mut stdout = tokio::io::stdout();
- let mut pty = pty.compat();
loop {
tokio::select! {
@@ -39,7 +37,7 @@ mod main {
break;
}
},
- _ = child.status_no_drop() => break,
+ _ = child.wait() => break,
}
}
@@ -52,14 +50,15 @@ mod main {
async fn main() {
use std::os::unix::process::ExitStatusExt as _;
- let pty = pty_process::Pty::new().unwrap();
+ let mut pty = pty_process::Pty::new().unwrap();
+ let pts = pty.pts().unwrap();
pty.resize(pty_process::Size::new(24, 80)).unwrap();
let mut child = pty_process::Command::new("tac")
// .args(&["500"])
- .spawn(&pty)
+ .spawn(&pts)
.unwrap();
- main::run(&child, &pty).await.unwrap();
- let status = child.status().await.unwrap();
+ main::run(&mut child, &mut pty).await.unwrap();
+ let status = child.wait().await.unwrap();
std::process::exit(
status
.code()