From 1996a03987a5a8c7e50a16a7a63b6dbdcd0424ad Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Fri, 17 Jul 2020 00:56:00 -0400 Subject: better resize api --- src/command.rs | 10 +++++++++- src/pty.rs | 30 ++++++++++++++++-------------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/command.rs b/src/command.rs index 10de48e..cf1925d 100644 --- a/src/command.rs +++ b/src/command.rs @@ -14,7 +14,11 @@ impl Command for std::process::Command { size: Option<&crate::pty::Size>, ) -> Result { let pty = crate::pty::Pty::new()?; - let pts = pty.pts(size)?; + if let Some(size) = size { + pty.resize(size)?; + } + + let pts = pty.pts()?; let pt_fd = pty.pt().as_raw_fd(); let pts_fd = pts.as_raw_fd(); @@ -74,6 +78,10 @@ impl Child { pub fn pty(&self) -> &std::fs::File { self.pty.pt() } + + pub fn pty_resize(&self, size: &crate::pty::Size) -> Result<()> { + self.pty.resize(size) + } } impl std::ops::Deref for Child { diff --git a/src/pty.rs b/src/pty.rs index 383e81d..d8e00d6 100644 --- a/src/pty.rs +++ b/src/pty.rs @@ -78,27 +78,29 @@ impl Pty { &self.pt } - pub fn pts(&self, size: Option<&Size>) -> Result { + pub fn pts(&self) -> Result { let fh = std::fs::OpenOptions::new() .read(true) .write(true) .open(&self.ptsname) .map_err(|e| Error::OpenPts(self.ptsname.clone(), e))?; - let fd = fh.as_raw_fd(); - if let Some(size) = size { - let size = size.into(); - - // safe because fd is guaranteed to be valid here (or else the - // previous open call would have returned an error and exited the - // function early), and size is guaranteed to be initialized - // because it's a normal rust value, and nix::pty::Winsize is a - // repr(C) struct with the same layout as `struct winsize` from - // sys/ioctl.h. - unsafe { set_term_size(fd, &size as *const nix::pty::Winsize) } - .map_err(Error::SetTermSize)?; - } Ok(fh) } + + pub fn resize(&self, size: &Size) -> Result<()> { + let size = size.into(); + let fd = self.pt().as_raw_fd(); + + // safe because fd is guaranteed to be valid here (or else the + // previous open call would have returned an error and exited the + // function early), and size is guaranteed to be initialized + // because it's a normal rust value, and nix::pty::Winsize is a + // repr(C) struct with the same layout as `struct winsize` from + // sys/ioctl.h. + unsafe { set_term_size(fd, &size as *const nix::pty::Winsize) } + .map_err(Error::SetTermSize)?; + Ok(()) + } } nix::ioctl_write_ptr_bad!(set_term_size, libc::TIOCSWINSZ, nix::pty::Winsize); -- cgit v1.2.3-54-g00ecf