term: Ensure `Editor` gets a TTY

Some text editors don't work correctly if `stdin` is not a TTY device.
This commit is contained in:
Alexis Sellier 2023-06-20 12:18:23 +02:00
parent bb099faa4f
commit 9418a20cae
No known key found for this signature in database
1 changed files with 10 additions and 1 deletions

View File

@ -1,4 +1,5 @@
use std::ffi::OsString;
use std::io::IsTerminal;
use std::io::Write;
use std::os::fd::{AsRawFd, FromRawFd};
use std::path::PathBuf;
@ -73,11 +74,19 @@ impl Editor {
// child exits.
let stderr = io::stderr().as_raw_fd();
let stderr = unsafe { libc::dup(stderr) };
let stdin = if io::stdin().is_terminal() {
process::Stdio::inherit()
} else {
let tty = termion::get_tty()?;
// If standard input is not a terminal device, the editor won't work correctly.
// In that case, we use the terminal device, eg. `/dev/tty` as standard input.
process::Stdio::from(tty)
};
process::Command::new(cmd)
.stdout(unsafe { process::Stdio::from_raw_fd(stderr) })
.stderr(process::Stdio::inherit())
.stdin(process::Stdio::inherit())
.stdin(stdin)
.arg(&self.path)
.spawn()?
.wait()?;