Skip to content

Commit

Permalink
Merge pull request #6 from andfoy/get_pid
Browse files Browse the repository at this point in the history
PR: Add get_pid method to PTY struct
  • Loading branch information
andfoy authored Jan 11, 2022
2 parents 99b2427 + 22521bf commit f170946
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/pty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,9 @@ impl PTY {
pub fn is_alive(&mut self) -> Result<bool, OsString> {
self.pty.is_alive()
}

/// Retrieve the process ID (PID) of the spawned program.
pub fn get_pid(&self) -> u32 {
self.pty.get_pid()
}
}
8 changes: 8 additions & 0 deletions src/pty/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ pub trait PTYImpl: Sync + Send {

/// Determine if the process is still alive.
fn is_alive(&mut self) -> Result<bool, OsString>;

/// Retrieve the Process ID associated to the current process.
fn get_pid(&self) -> u32;
}


Expand Down Expand Up @@ -392,6 +395,11 @@ impl PTYProcess {
}
}

/// Retrieve the Process ID associated to the current process.
pub fn get_pid(&self) -> u32 {
self.pid
}

}

impl Drop for PTYProcess {
Expand Down
4 changes: 4 additions & 0 deletions src/pty/conpty/default_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ impl PTYImpl for ConPTY {
fn is_alive(&mut self) -> Result<bool, OsString> {
Err(OsString::from("pty_rs was compiled without ConPTY enabled"))
}

fn get_pid(&self) -> u32 {
0
}
}
4 changes: 4 additions & 0 deletions src/pty/conpty/pty_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,10 @@ impl PTYImpl for ConPTY {
fn is_alive(&mut self) -> Result<bool, OsString> {
self.process.is_alive()
}

fn get_pid(&self) -> u32 {
self.process.get_pid()
}
}

impl Drop for ConPTY {
Expand Down
4 changes: 4 additions & 0 deletions src/pty/winpty/default_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ impl PTYImpl for WinPTY {
fn is_alive(&mut self) -> Result<bool, OsString> {
Err(OsString::from("winpty_rs was compiled without WinPTY enabled"))
}

fn get_pid(&self) -> u32 {
0
}
}
4 changes: 4 additions & 0 deletions src/pty/winpty/pty_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,8 @@ impl PTYImpl for WinPTY {
fn is_alive(&mut self) -> Result<bool, OsString> {
self.process.is_alive()
}

fn get_pid(&self) -> u32 {
self.process.get_pid()
}
}
3 changes: 2 additions & 1 deletion tests/conpty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ fn read_write_conpty() {
}

println!("!!!!!!!!!!!!!!!!!");
assert!(out_regex.is_match(output_str))
assert!(out_regex.is_match(output_str));
assert_ne!(pty.get_pid(), 0)
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions tests/winpty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ fn read_write_winpty() {
}

assert!(out_regex.is_match(output_str));
assert_ne!(pty.get_pid(), 0)
}

#[test]
Expand Down

0 comments on commit f170946

Please sign in to comment.