aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2020-07-17 00:56:00 -0400
committerJesse Luehrs <doy@tozt.net>2020-07-17 00:57:33 -0400
commit1996a03987a5a8c7e50a16a7a63b6dbdcd0424ad (patch)
tree4ccf84e60df8e1c94166d4533a4f1e037b5b3446
parent4bfa68db838e153a1d755e4cfb5b088dfa6c4064 (diff)
downloadpty-process-1996a03987a5a8c7e50a16a7a63b6dbdcd0424ad.tar.gz
pty-process-1996a03987a5a8c7e50a16a7a63b6dbdcd0424ad.zip
better resize api
-rw-r--r--src/command.rs10
-rw-r--r--src/pty.rs30
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<Child> {
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<std::fs::File> {
+ pub fn pts(&self) -> Result<std::fs::File> {
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);