Use dev/tty everywere

This commit is contained in:
Timon 2023-01-11 21:19:18 +01:00
parent 614e6a73b7
commit a48cf31502
3 changed files with 39 additions and 9 deletions

View File

@ -32,7 +32,7 @@ fn read_position() -> Result<(u16, u16)> {
fn read_position_raw() -> Result<(u16, u16)> {
// Use `ESC [ 6 n` to and retrieve the cursor position.
let mut stdout = io::stdout();
let mut stdout = tty_fd()?;
stdout.write_all(b"\x1B[6n")?;
stdout.flush()?;

View File

@ -73,6 +73,7 @@ impl AsRawFd for FileDesc {
}
/// Creates a file descriptor pointing to the standard input or `/dev/tty`.
#[cfg(not(feature = "use-dev-tty"))]
pub fn tty_fd() -> Result<FileDesc> {
let (fd, close_on_drop) = if unsafe { libc::isatty(libc::STDIN_FILENO) == 1 } {
(libc::STDIN_FILENO, false)
@ -89,3 +90,37 @@ pub fn tty_fd() -> Result<FileDesc> {
Ok(FileDesc::new(fd, close_on_drop))
}
/// Creates a file descriptor pointing to the standard input or `/dev/tty`.
#[cfg(feature = "use-dev-tty")]
pub fn tty_fd(close_on_drop: bool) -> Result<FileDesc> {
use crate::tty::IsTty;
let fd = match open_rw("/dev/tty") {
Ok(tty) => {
println!("Can open");
tty
}
Err(e) => {
println!("Can not open");
if stdin().is_tty() {
libc::STDIN_FILENO
} else {
return Err(ErrorKind::IoError(io::Error::new(io::ErrorKind::Other, "Failed to initialize file descriptor. Crossterm first tried to open `/dev/tty` and then `libc::STDIN_FILENO` but both could not be used.")));
}
}
};
Ok(FileDesc::new(fd, close_on_drop))
}
fn open_rw<P: AsRef<Path>>(path: P) -> io::Result<RawFd> {
use std::fs::OpenOptions;
let file = OpenOptions::new()
.read(true)
.write(true)
.open(path)?;
Ok(file.into_raw_fd())
}

View File

@ -123,14 +123,9 @@ fn read_supports_keyboard_enhancement_raw() -> Result<bool> {
// ESC [ c Query primary device attributes.
const QUERY: &[u8] = b"\x1B[?u\x1B[c";
if let Err(_) = File::open("/dev/tty").and_then(|mut file| {
file.write_all(QUERY)?;
file.flush()
}) {
let mut stdout = io::stdout();
stdout.write_all(QUERY)?;
stdout.flush()?;
}
let fd = tty_fd()?;
fd.write_all(QUERY)?;
fd.flush();
loop {
match poll_internal(