Skip to content

Allow setting window size in spawned process. #120

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ rust-version = "1.60"

[dependencies]
comma = "1.0"
nix = { version = "0.27", features = ["fs", "process", "signal", "term"] }
nix = { version = "0.27", features = ["fs", "process", "signal", "term", "ioctl"] }
regex = "1"
tempfile = "3"
thiserror = "1.0.34"
Expand Down
23 changes: 21 additions & 2 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
use crate::error::Error;
use nix;
use nix::fcntl::{open, OFlag};
use nix::libc::{STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO};
use nix::libc::{STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO, TIOCSWINSZ};
pub use nix::pty::Winsize;
use nix::pty::{grantpt, posix_openpt, unlockpt, PtyMaster};
pub use nix::sys::{signal, wait};
use nix::sys::{stat, termios};
Expand All @@ -16,6 +17,14 @@ use std::os::unix::process::CommandExt;
use std::process::Command;
use std::{thread, time};

/// Options for PtyProcess
///
/// - dimensions: If given, sets the terminal size of the spawned process.
#[derive(Default)]
pub struct Options {
pub dimensions: Option<Winsize>,
}

/// Start a process in a forked tty so you can interact with it the same as you would
/// within a terminal
///
Expand Down Expand Up @@ -85,7 +94,12 @@ fn ptsname_r(fd: &PtyMaster) -> nix::Result<String> {

impl PtyProcess {
/// Start a process in a forked pty
pub fn new(mut command: Command) -> Result<Self, Error> {
pub fn new(command: Command) -> Result<Self, Error> {
Self::new_with_options(command, Options::default())
}

/// Start a process in a forked pty with the given options
pub fn new_with_options(mut command: Command, options: Options) -> Result<Self, Error> {
// Open a new PTY master
let master_fd = posix_openpt(OFlag::O_RDWR)?;

Expand Down Expand Up @@ -124,6 +138,11 @@ impl PtyProcess {
flags.local_flags &= !termios::LocalFlags::ECHO;
termios::tcsetattr(&stdin, termios::SetArg::TCSANOW, &flags)?;

if let Some(winsize) = &options.dimensions {
nix::ioctl_write_ptr_bad!(_set_window_size, TIOCSWINSZ, Winsize);
unsafe { _set_window_size(stdin.as_raw_fd(), winsize)? };
}

command.exec();
Err(Error::Nix(nix::Error::last()))
}
Expand Down
Loading