aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2020-07-17 00:55:37 -0400
committerJesse Luehrs <doy@tozt.net>2020-07-17 00:55:37 -0400
commit4bfa68db838e153a1d755e4cfb5b088dfa6c4064 (patch)
treeb0b97c433e2340c1540b3d3ef7a0c08327b5eece
parente743ece128ee8b78a094d3dd68243759ca4c0758 (diff)
downloadpty-process-4bfa68db838e153a1d755e4cfb5b088dfa6c4064.tar.gz
pty-process-4bfa68db838e153a1d755e4cfb5b088dfa6c4064.zip
more useful example
-rw-r--r--examples/basic.rs49
1 files changed, 37 insertions, 12 deletions
diff --git a/examples/basic.rs b/examples/basic.rs
index 15cc137..f63b767 100644
--- a/examples/basic.rs
+++ b/examples/basic.rs
@@ -3,15 +3,42 @@ use std::os::unix::io::AsRawFd 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,
+ );
+ }
+}
+
fn main() {
- let mut child = std::process::Command::new("perl")
- .args(&[
- "-MTerm::ReadKey",
- "-E",
- "my @size = GetTerminalSize; say for @size",
- ])
+ let mut child = std::process::Command::new("sh")
.spawn_pty(Some(&pty_process::Size::new(24, 80)))
.unwrap();
+ let _raw = RawGuard::new();
let mut buf = [0_u8; 4096];
let pty = child.pty().as_raw_fd();
let stdin = std::io::stdin().as_raw_fd();
@@ -27,12 +54,10 @@ fn main() {
match child.pty().read(&mut buf) {
Ok(bytes) => {
let buf = &buf[..bytes];
- print!(
- "got {} bytes: '{}'",
- bytes,
- std::str::from_utf8(buf).unwrap()
- );
- std::io::stdout().flush().unwrap();
+ let stdout = std::io::stdout();
+ let mut stdout = stdout.lock();
+ stdout.write_all(buf).unwrap();
+ stdout.flush().unwrap();
}
Err(e) => {
eprintln!("pty read failed: {:?}", e);