Skip to content
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

Use dev/tty everywere #743

Draft
wants to merge 1 commit into
base: master
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 src/cursor/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;

Expand Down
35 changes: 35 additions & 0 deletions src/event/sys/unix/file_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())
}
11 changes: 3 additions & 8 deletions src/terminal/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down