From c78084caca2563c52e563901a94e4d094e88cd1c Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 3 Mar 2021 01:26:18 -0500 Subject: refactor examples --- examples/async-std.rs | 34 ++-------------------------------- examples/basic.rs | 33 ++------------------------------- examples/raw_guard/mod.rs | 32 ++++++++++++++++++++++++++++++++ examples/smol.rs | 34 ++-------------------------------- examples/tokio.rs | 34 ++-------------------------------- 5 files changed, 40 insertions(+), 127 deletions(-) create mode 100644 examples/raw_guard/mod.rs diff --git a/examples/async-std.rs b/examples/async-std.rs index c65b1ac..b521549 100644 --- a/examples/async-std.rs +++ b/examples/async-std.rs @@ -2,44 +2,14 @@ use async_std::io::prelude::WriteExt as _; use async_std::io::ReadExt as _; use async_std::prelude::FutureExt as _; use pty_process::Command as _; -use std::os::unix::io::AsRawFd as _; use std::os::unix::process::ExitStatusExt as _; -struct RawGuard { - termios: nix::sys::termios::Termios, -} - -impl RawGuard { - fn new() -> Self { - let stdin = std::io::stdin().as_raw_fd(); - let termios = nix::sys::termios::tcgetattr(stdin).unwrap(); - let mut termios_raw = termios.clone(); - nix::sys::termios::cfmakeraw(&mut termios_raw); - nix::sys::termios::tcsetattr( - stdin, - nix::sys::termios::SetArg::TCSANOW, - &termios_raw, - ) - .unwrap(); - Self { termios } - } -} - -impl Drop for RawGuard { - fn drop(&mut self) { - let stdin = std::io::stdin().as_raw_fd(); - let _ = nix::sys::termios::tcsetattr( - stdin, - nix::sys::termios::SetArg::TCSANOW, - &self.termios, - ); - } -} +mod raw_guard; async fn run( child: &pty_process::async_std::Child, ) -> std::result::Result<(), Box> { - let _raw = RawGuard::new(); + let _raw = raw_guard::RawGuard::new(); let ex = async_executor::Executor::new(); diff --git a/examples/basic.rs b/examples/basic.rs index bc9389c..97bdd46 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -4,39 +4,10 @@ use std::os::unix::process::ExitStatusExt as _; use pty_process::Command as _; -struct RawGuard { - termios: nix::sys::termios::Termios, -} - -impl RawGuard { - fn new() -> Self { - let stdin = std::io::stdin().as_raw_fd(); - let termios = nix::sys::termios::tcgetattr(stdin).unwrap(); - let mut termios_raw = termios.clone(); - nix::sys::termios::cfmakeraw(&mut termios_raw); - nix::sys::termios::tcsetattr( - stdin, - nix::sys::termios::SetArg::TCSANOW, - &termios_raw, - ) - .unwrap(); - Self { termios } - } -} - -impl Drop for RawGuard { - fn drop(&mut self) { - let stdin = std::io::stdin().as_raw_fd(); - let _ = nix::sys::termios::tcsetattr( - stdin, - nix::sys::termios::SetArg::TCSANOW, - &self.termios, - ); - } -} +mod raw_guard; fn run(child: &pty_process::std::Child) { - let _raw = RawGuard::new(); + let _raw = 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(); diff --git a/examples/raw_guard/mod.rs b/examples/raw_guard/mod.rs new file mode 100644 index 0000000..2930a08 --- /dev/null +++ b/examples/raw_guard/mod.rs @@ -0,0 +1,32 @@ +use std::os::unix::io::AsRawFd as _; + +pub struct RawGuard { + termios: nix::sys::termios::Termios, +} + +impl RawGuard { + pub fn new() -> Self { + let stdin = std::io::stdin().as_raw_fd(); + let termios = nix::sys::termios::tcgetattr(stdin).unwrap(); + let mut termios_raw = termios.clone(); + nix::sys::termios::cfmakeraw(&mut termios_raw); + nix::sys::termios::tcsetattr( + stdin, + nix::sys::termios::SetArg::TCSANOW, + &termios_raw, + ) + .unwrap(); + Self { termios } + } +} + +impl Drop for RawGuard { + fn drop(&mut self) { + let stdin = std::io::stdin().as_raw_fd(); + let _ = nix::sys::termios::tcsetattr( + stdin, + nix::sys::termios::SetArg::TCSANOW, + &self.termios, + ); + } +} diff --git a/examples/smol.rs b/examples/smol.rs index 5512c2d..d7b816f 100644 --- a/examples/smol.rs +++ b/examples/smol.rs @@ -1,43 +1,13 @@ use pty_process::Command as _; use smol::io::{AsyncReadExt as _, AsyncWriteExt as _}; -use std::os::unix::io::AsRawFd as _; use std::os::unix::process::ExitStatusExt as _; -struct RawGuard { - termios: nix::sys::termios::Termios, -} - -impl RawGuard { - fn new() -> Self { - let stdin = std::io::stdin().as_raw_fd(); - let termios = nix::sys::termios::tcgetattr(stdin).unwrap(); - let mut termios_raw = termios.clone(); - nix::sys::termios::cfmakeraw(&mut termios_raw); - nix::sys::termios::tcsetattr( - stdin, - nix::sys::termios::SetArg::TCSANOW, - &termios_raw, - ) - .unwrap(); - Self { termios } - } -} - -impl Drop for RawGuard { - fn drop(&mut self) { - let stdin = std::io::stdin().as_raw_fd(); - let _ = nix::sys::termios::tcsetattr( - stdin, - nix::sys::termios::SetArg::TCSANOW, - &self.termios, - ); - } -} +mod raw_guard; async fn run( child: &pty_process::smol::Child, ) -> std::result::Result<(), Box> { - let _raw = RawGuard::new(); + let _raw = raw_guard::RawGuard::new(); let ex = smol::Executor::new(); diff --git a/examples/tokio.rs b/examples/tokio.rs index 42cd643..2441c61 100644 --- a/examples/tokio.rs +++ b/examples/tokio.rs @@ -1,43 +1,13 @@ use pty_process::Command as _; -use std::os::unix::io::AsRawFd as _; use std::os::unix::process::ExitStatusExt as _; use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _}; -struct RawGuard { - termios: nix::sys::termios::Termios, -} - -impl RawGuard { - fn new() -> Self { - let stdin = std::io::stdin().as_raw_fd(); - let termios = nix::sys::termios::tcgetattr(stdin).unwrap(); - let mut termios_raw = termios.clone(); - nix::sys::termios::cfmakeraw(&mut termios_raw); - nix::sys::termios::tcsetattr( - stdin, - nix::sys::termios::SetArg::TCSANOW, - &termios_raw, - ) - .unwrap(); - Self { termios } - } -} - -impl Drop for RawGuard { - fn drop(&mut self) { - let stdin = std::io::stdin().as_raw_fd(); - let _ = nix::sys::termios::tcsetattr( - stdin, - nix::sys::termios::SetArg::TCSANOW, - &self.termios, - ); - } -} +mod raw_guard; async fn run( child: &mut pty_process::tokio::Child, ) -> std::result::Result<(), Box> { - let _raw = RawGuard::new(); + let _raw = raw_guard::RawGuard::new(); let mut in_buf = [0_u8; 4096]; let mut out_buf = [0_u8; 4096]; -- cgit v1.2.3-54-g00ecf