Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

publish the pty module to crates.io #27

Closed
prabirshrestha opened this issue May 9, 2019 · 12 comments
Closed

publish the pty module to crates.io #27

prabirshrestha opened this issue May 9, 2019 · 12 comments

Comments

@prabirshrestha
Copy link

Would it be possible to publish the pty module as a crate that can be reused by other programs?
Currently there is non in rust that works on all 3 major OS.

I had made similar request here alacritty/alacritty#2079

@wez
Copy link
Owner

wez commented May 18, 2019

Sorry for the delay: I only just saw this (for some reason, I wasn't getting any notifications for this project at all: now fixed!)

I don't have a strong objection to the idea, but want to note that I didn't design this to be a general pty solution, but rather one targeted for wezterm, so I wonder just how useful this is beyond wezterm. Can you share more on how you intend to use it? Do you have thoughts and feedback on the interface as it stands at the moment?

@prabirshrestha
Copy link
Author

prabirshrestha commented May 19, 2019

The primary reason for this is because there are no cross platform pty crate for rust at all.

Pty allows to create lot of other interesting programs such as tmux alternatives as there are non that works natively in windows. This seems to be in your readme so I’m looking forward to it :). I personally use dwm even for windows so I’m not worried about tabs https://github.com/prabirshrestha/dwm-win32. Also OpenGL doesn’t seem to play nicely on remote desktop alacritty/alacritty#1976. Wezterm also crashes with the same issue.

@prabirshrestha
Copy link
Author

Also one thing that is currently missing in pty is working directory.

wez added a commit that referenced this issue May 20, 2019
wez added a commit that referenced this issue May 20, 2019
wez added a commit that referenced this issue May 20, 2019
@wez
Copy link
Owner

wez commented May 20, 2019

I published it as https://crates.io/crates/portable-pty; the docs should show up at https://docs.rs/portable-pty once it has been ingested by docs.rs.

I didn't add an api for setting the cwd, but it should be pretty straight forward to add support for that to the CommandBuilder if someone needs it (PRs welcome!)

@prabirshrestha
Copy link
Author

Thanks for making the crate.

Now when I'm trying to run spawn a simple command I get the following error when running on win10.0.17763.437.

image

use portable_pty::{CommandBuilder, PtySize, PtySystemSelection};

fn main() {
    let pty_system = PtySystemSelection::default().get().unwrap();

    let (mut _master, slave) = pty_system.openpty(PtySize {
        rows: 24,
        cols: 80,
        pixel_width: 0,
        pixel_height: 0
    }).unwrap();

    let cmd = CommandBuilder::new("c:\\WINDOWS\\system32\\whoami.exe");
    let _child = slave.spawn_command(cmd).unwrap();
}

output for cargo run:

   Compiling hello-pty-rs v0.1.0 (D:\tmp\hello-pty-rs)
    Finished dev [unoptimized + debuginfo] target(s) in 0.88s
     Running `target\debug\hello-pty-rs.exe`
Running: module: c:\WINDOWS\system32\whoami.exe  "c:\\WINDOWS\\system32\\whoami.exe\u{0}"

Let me know if I'm doing something wrong here.

wez added a commit that referenced this issue May 20, 2019
@wez
Copy link
Owner

wez commented May 20, 2019

I just added an example that works on macos; I think there's a good chance that it will also run unmodified on windows. The key difference from your code snippet above is that it waits for the child process and reads from it. I suspect that the issue you hit is that the pty is being dropped as the child is starting up, because it isn't waiting for the child.

https://github.com/wez/wezterm/blob/master/pty/examples/whoami.rs

@prabirshrestha
Copy link
Author

The example no longer crashes on windows but seems to hang now. If I remove this line it doesn't hang .read_to_string(&mut s).

@prabirshrestha
Copy link
Author

So tried this and seems like it is getting stuck in buffer read hence read_to_string() see hang.

This is the new code that I ran on Windows and ArchLinux and seem to given different output.

use std::io::{BufRead, BufReader};
use portable_pty::{CommandBuilder, PtySize, PtySystemSelection};

fn main() {
    let pty_system = PtySystemSelection::default().get().unwrap();

    let (master, slave) = pty_system
        .openpty(PtySize {
            rows: 24,
            cols: 80,
            pixel_width: 0,
            pixel_height: 0,
        })
        .unwrap();

    let cmd = CommandBuilder::new("whoami");
    let mut child = slave.spawn_command(cmd).unwrap();

    let reader = master.try_clone_reader().unwrap();
    let br = BufReader::new(reader);

    for line in br.lines() {
        println!("{}", line.unwrap());

        match child.try_wait().unwrap() {
            Some(e) => { println!("Some({})", e.success()); return; },
            None => { println!("None"); return; },
            _ => { println!("_"); return; }
        }
    }
}

Output on windows is always the following.

prabirshrestha
Some(true)

Output on ArchLinux is random, sometime Some(true) sometimes None. This is most likely because starting a new process on windows is slower.

prabirshrestha
None

What would be the best way to keep buffering but once the child exits stop reading and quit the program. Also seems like the actual status code is not exposed to public.

@sigmaSd
Copy link

sigmaSd commented May 21, 2019

would something like this work? (sorry about the formatting Im on phone)

loop {
    match child.try_wait() {
        Ok(None) => continue,
        Err(e) => reteurn Err(e),
        Ok(Some(status)) => if status.is_sucess() {
                                         return Ok(())
                                             } else {
                                                   return io::Error::last_os_err()
                                             }

}

wez added a commit that referenced this issue May 21, 2019
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: #27
@wez
Copy link
Owner

wez commented May 21, 2019

I've pushed an updated example with comments inline that should help understand where the blocking is happening

@prabirshrestha
Copy link
Author

It works now. Thanks for the detailed comments in the example. That was very helpful.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 4, 2023

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants