From 39b6aa899d8de4aeb086eb6acbaba73c62037257 Mon Sep 17 00:00:00 2001 From: blacknon Date: Sun, 25 Aug 2024 22:02:49 +0900 Subject: [PATCH] update. add set_blocking --- Cargo.toml | 2 +- src/event.rs | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 27d6448e..fdd1778e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "crossterm" -version = "0.28.1" +version = "0.28.2" authors = ["T. Post"] description = "A crossplatform terminal library for manipulating terminals." repository = "https://github.com/crossterm-rs/crossterm" diff --git a/src/event.rs b/src/event.rs index 4d28dc15..dd027565 100644 --- a/src/event.rs +++ b/src/event.rs @@ -245,13 +245,31 @@ pub fn poll(timeout: Duration) -> std::io::Result { /// } /// ``` pub fn read() -> std::io::Result { - match read_internal(&EventFilter)? { + // #[cfg(target_os = "macos")] + #[cfg(unix)] + let _ = set_nonblocking(true); + + let readed_event = read_internal(&EventFilter)?; + + // #[cfg(target_os = "macos")] + #[cfg(unix)] + let _ = set_nonblocking(false); + + match readed_event { InternalEvent::Event(event) => Ok(event), #[cfg(unix)] _ => unreachable!(), } } +fn set_nonblocking(is_nonblock: bool) { + use rustix::fd::AsRawFd; + let stdin_fd = std::io::stdin().as_raw_fd(); + let fd = unsafe { rustix::fd::BorrowedFd::borrow_raw(stdin_fd) }; + + let _ = rustix::io::ioctl_fionbio(fd, is_nonblock); +} + /// Polls to check if there are any `InternalEvent`s that can be read within the given duration. pub(crate) fn poll_internal(timeout: Option, filter: &F) -> std::io::Result where