1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-23 13:21:38 +03:00

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
This commit is contained in:
Wez Furlong 2019-05-21 08:56:39 -07:00
parent 015a97e2be
commit 3bf551706e
2 changed files with 46 additions and 13 deletions

View File

@ -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<R: std::io::Read>(mut r: R) -> std::io::Result<Vec<u8>> {
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);
}
}

View File

@ -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(),