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

Only for UNIX OS, modify it to enable O_NONBLOCK and use event::read(). #920

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
20 changes: 19 additions & 1 deletion src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,31 @@ pub fn poll(timeout: Duration) -> std::io::Result<bool> {
/// }
/// ```
pub fn read() -> std::io::Result<Event> {
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<F>(timeout: Option<Duration>, filter: &F) -> std::io::Result<bool>
where
Expand Down