From 3bf551706ea6353f2f1d0fa12ef3cf6d7cf7d3d7 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Tue, 21 May 2019 08:56:39 -0700 Subject: [PATCH] improve pty example Example output on linux: ``` child status: ExitStatus { successful: true } output: wez\r\n% ``` Example output on windows: ``` child status: ExitStatus { successful: true } output: \u{1b}[2J\u{1b}[?25l\u{1b}[m\u{1b}[HDOMAIN\\wez\u{8}\u{1b}]0;C:\\windows\\system32\\whoami.EXE\u{0}\u{7}\u{1b}[?25h ``` Refs: https://github.com/wez/wezterm/issues/27 --- pty/examples/whoami.rs | 54 +++++++++++++++++++++++++++++++++++------- pty/src/win/conpty.rs | 5 ---- 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/pty/examples/whoami.rs b/pty/examples/whoami.rs index 8171163ff..172c0fab7 100644 --- a/pty/examples/whoami.rs +++ b/pty/examples/whoami.rs @@ -1,5 +1,26 @@ +//! This is a conceptually simple example that spawns the `whoami` program +//! to print your username. It is made more complex because there are multiple +//! pipes involved and it is easy to get blocked/deadlocked if care and attention +//! is not paid to those pipes! use portable_pty::{CommandBuilder, PtySize, PtySystemSelection}; +// Read all available data until we reach EOF or encounter an error +// condition. Only returns an error if we didn't receive any data. +fn read_until_eof_or_error(mut r: R) -> std::io::Result> { + let mut result = vec![]; + + match r.read_to_end(&mut result) { + Ok(_len) => Ok(result), + Err(err) => { + if result.is_empty() { + Err(err) + } else { + Ok(result) + } + } + } +} + fn main() { let pty_system = PtySystemSelection::default().get().unwrap(); @@ -14,13 +35,30 @@ fn main() { let cmd = CommandBuilder::new("whoami"); let mut child = slave.spawn_command(cmd).unwrap(); + // Release any handles owned by the slave: we don't need it now + // that we've spawned the child. + drop(slave); - let mut s = String::new(); - master - .try_clone_reader() - .unwrap() - .read_to_string(&mut s) - .unwrap(); - println!("output: {}", s); - child.wait().unwrap(); + let reader = master.try_clone_reader().unwrap(); + println!("child status: {:?}", child.wait().unwrap()); + // We hold handles on the pty. Now that the child is complete + // there are no processes remaining that will write to it until + // we spawn more. We're not going to do that in this example, + // so we should close it down. If we didn't drop it explicitly + // here, then the attempt to read its output would block forever + // waiting for a future child that will never be spawned. + drop(master); + + // Consume the output from the child + let buf = read_until_eof_or_error(reader).unwrap(); + let s = String::from_utf8(buf).unwrap(); + + // We print with escapes escaped because the windows conpty + // implementation synthesizes title change escape sequences + // in the output stream and it can be confusing to see those + // printed out raw in another terminal. + print!("output: "); + for c in s.escape_debug() { + print!("{}", c); + } } diff --git a/pty/src/win/conpty.rs b/pty/src/win/conpty.rs index 6048d21c2..751591b80 100644 --- a/pty/src/win/conpty.rs +++ b/pty/src/win/conpty.rs @@ -260,11 +260,6 @@ impl SlavePty for ConPtySlavePty { let (mut exe, mut cmdline) = cmd.cmdline()?; let cmd_os = OsString::from_wide(&cmdline); - eprintln!( - "Running: module: {} {:?}", - Path::new(&OsString::from_wide(&exe)).display(), - cmd_os - ); let res = unsafe { CreateProcessW( exe.as_mut_slice().as_mut_ptr(),