From 1c297c4f89fca61b72a290a46be66a0c99f1912d Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 3 Mar 2021 01:44:39 -0500 Subject: add interhack example --- Cargo.toml | 2 + examples/interhack.rs | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 examples/interhack.rs diff --git a/Cargo.toml b/Cargo.toml index dad4339..a3e4446 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,9 @@ futures = { version = "0.3", optional = true } [dev-dependencies] async-std = { version = "1.9", features = ["unstable"] } async-executor = "1.4" +regex = "1.4" smol = "1.2" +term_size = "0.3" tokio = { version = "1.2", features = [ "rt-multi-thread", "macros", "io-std", "io-util" ] } [features] diff --git a/examples/interhack.rs b/examples/interhack.rs new file mode 100644 index 0000000..747c0c9 --- /dev/null +++ b/examples/interhack.rs @@ -0,0 +1,104 @@ +use pty_process::Command as _; +use smol::io::{AsyncReadExt as _, AsyncWriteExt as _}; +use std::os::unix::process::ExitStatusExt as _; + +mod raw_guard; + +async fn run( + child: &pty_process::smol::Child, +) -> std::result::Result<(), Box> { + let _raw = raw_guard::RawGuard::new(); + + 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) => { + // engrave Elbereth with ^E + if buf[..bytes].contains(&5u8) { + for byte in buf[..bytes].iter() { + match byte { + 5u8 => child + .pty() + .write_all(b"E- Elbereth\n") + .await + .unwrap(), + _ => child + .pty() + .write_all(&[*byte]) + .await + .unwrap(), + } + } + } else { + child.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()); + #[allow(clippy::trivial_regex)] + let re = regex::bytes::Regex::new("Elbereth").unwrap(); + loop { + match child.pty().read(&mut buf).await { + Ok(bytes) => { + // highlight successful Elbereths + if re.is_match(&buf[..bytes]) { + stdout + .write_all(&re.replace_all( + &buf[..bytes], + &b"\x1b[35m$0\x1b[m"[..], + )) + .await + .unwrap(); + } else { + stdout.write_all(&buf[..bytes]).await.unwrap(); + } + stdout.flush().await.unwrap(); + } + Err(e) => { + // EIO means that the process closed the other + // end of the pty + if e.raw_os_error() != Some(libc::EIO) { + eprintln!("pty read failed: {:?}", e); + } + break; + } + } + } + }); + + ex.run(smol::future::or(input, output)).await; + + Ok(()) +} + +fn main() { + 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 mut child = smol::process::Command::new("nethack") + .spawn_pty(Some(&pty_process::Size::new(h, w))) + .unwrap(); + run(&child).await.unwrap(); + child.status().await.unwrap() + }); + std::process::exit( + status + .code() + .unwrap_or_else(|| status.signal().unwrap_or(0) + 128), + ); +} -- cgit v1.2.3-54-g00ecf