crate::ix!();
#[cfg(unix)]
pub(crate) mod unix_impl {
use super::*;
use libc::{
c_int, isatty, poll, tcgetattr, tcsetattr, termios, POLLIN, STDIN_FILENO, TCSANOW,
};
pub fn set_echo(enable: bool) {
unsafe {
let mut tty: termios = core::mem::zeroed();
if tcgetattr(STDIN_FILENO, &mut tty as *mut _) != 0 {
error!(target: "compat::stdin", "tcgetattr failed");
return;
}
if enable {
tty.c_lflag |= libc::ECHO;
} else {
tty.c_lflag &= !libc::ECHO;
}
if tcsetattr(STDIN_FILENO, TCSANOW, &tty as *const _) != 0 {
error!(target: "compat::stdin", "tcsetattr failed");
}
}
}
#[inline]
pub fn is_tty() -> bool {
unsafe { isatty(STDIN_FILENO) == 1 }
}
pub fn is_ready() -> bool {
unsafe {
let mut fds = libc::pollfd {
fd: STDIN_FILENO,
events: POLLIN,
revents: 0,
};
poll(&mut fds as *mut _, 1, 0) == 1
}
}
}