term: Fix editor command with args

We add support for `EDITOR` being set to a command with arguments, eg.
`code --wait`.

Fixes dd6861f.
This commit is contained in:
cloudhead 2024-04-04 10:52:36 +02:00
parent ea69168f44
commit a5212a7a88
No known key found for this signature in database
3 changed files with 16 additions and 1 deletions

1
Cargo.lock generated
View File

@ -2595,6 +2595,7 @@ dependencies = [
"libc", "libc",
"once_cell", "once_cell",
"pretty_assertions", "pretty_assertions",
"shlex",
"tempfile", "tempfile",
"termion 3.0.0", "termion 3.0.0",
"unicode-display-width", "unicode-display-width",

View File

@ -17,6 +17,7 @@ anstyle-query = { version = "1.0.0" }
inquire = { version = "0.6.2", default-features = false, features = ["termion", "editor"] } inquire = { version = "0.6.2", default-features = false, features = ["termion", "editor"] }
libc = { version = "0.2" } libc = { version = "0.2" }
once_cell = { version = "1.13" } once_cell = { version = "1.13" }
shlex = { version = "1.1" }
termion = { version = "3" } termion = { version = "3" }
unicode-display-width = { version = "0.3.0" } unicode-display-width = { version = "0.3.0" }
unicode-segmentation = { version = "1.7.1" } unicode-segmentation = { version = "1.7.1" }

View File

@ -69,6 +69,18 @@ impl Editor {
"editor not configured: the `EDITOR` environment variable is not set", "editor not configured: the `EDITOR` environment variable is not set",
)); ));
}; };
let Some(parts) = shlex::split(cmd.to_string_lossy().as_ref()) else {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
format!("invalid editor command {cmd:?}"),
));
};
let Some((program, args)) = parts.split_first() else {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
format!("invalid editor command {cmd:?}"),
));
};
// We duplicate the stderr file descriptor to pass it to the child process, otherwise, if // We duplicate the stderr file descriptor to pass it to the child process, otherwise, if
// we simply pass the `RawFd` of our stderr, `Command` will close our stderr when the // we simply pass the `RawFd` of our stderr, `Command` will close our stderr when the
@ -84,10 +96,11 @@ impl Editor {
process::Stdio::from(tty) process::Stdio::from(tty)
}; };
process::Command::new(&cmd) process::Command::new(program)
.stdout(unsafe { process::Stdio::from_raw_fd(stderr) }) .stdout(unsafe { process::Stdio::from_raw_fd(stderr) })
.stderr(process::Stdio::inherit()) .stderr(process::Stdio::inherit())
.stdin(stdin) .stdin(stdin)
.args(args)
.arg(&self.path) .arg(&self.path)
.spawn() .spawn()
.map_err(|e| { .map_err(|e| {