use std::time::Duration;
use crate::{Error, Result};
#[cfg(unix)]
mod unix;
#[cfg(windows)]
mod windows;
#[cfg(unix)]
pub use unix::MAX_STDIN_WAIT;
#[cfg(windows)]
pub use windows::MAX_STDIN_WAIT;
#[cfg(all(not(unix), not(windows)))]
pub const MAX_STDIN_WAIT: Duration = Duration::ZERO;
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum SysEvent {
#[default]
Timeout,
StdinReady,
WindowResize,
}
#[derive(Clone, Debug)]
pub struct TermSize {
pub char_width: usize,
pub char_height: usize,
pub pixel_width: usize,
pub pixel_height: usize,
}
pub fn enable_raw_mode() -> Result<()> {
#[cfg(unix)]
return unix::enable_raw_mode();
#[cfg(windows)]
return windows::enable_raw_mode();
#[allow(unreachable_code)]
Err(Error::NotSupportedOnPlatform("raw mode"))
}
pub fn disable_raw_mode() -> Result<()> {
#[cfg(unix)]
return unix::disable_raw_mode();
#[cfg(windows)]
return windows::disable_raw_mode();
#[allow(unreachable_code)]
Err(Error::NotSupportedOnPlatform("raw mode"))
}
pub fn init_events() -> Result<()> {
#[cfg(unix)]
return unix::init_events();
#[allow(unreachable_code)]
Ok(())
}
pub fn is_raw_mode_enabled() -> bool {
#[cfg(unix)]
return unix::is_raw_mode_enabled();
#[cfg(windows)]
return windows::is_raw_mode_enabled().unwrap_or_default();
#[allow(unreachable_code)]
false
}
pub fn term_size() -> Result<TermSize> {
#[cfg(unix)]
return unix::window_size();
#[cfg(windows)]
return windows::term_size();
#[allow(unreachable_code)]
Err(Error::NotSupportedOnPlatform("terminal size"))
}
pub fn wait_for_stdin(timeout: Duration) -> Result<bool> {
#[cfg(unix)]
return unix::wait_for_stdin(timeout);
#[cfg(windows)]
return windows::wait_for_stdin(timeout);
#[allow(unreachable_code)]
{
_ = timeout;
Err(Error::NotSupportedOnPlatform("stdin timeout"))
}
}
pub fn wait_for_event(timeout: Duration) -> Result<SysEvent> {
#[cfg(unix)]
return unix::wait_for_event(timeout);
#[cfg(windows)]
return windows::wait_for_stdin(timeout).map(|b| {
if b {
SysEvent::StdinReady
} else {
SysEvent::Timeout
}
});
#[allow(unreachable_code)]
{
_ = timeout;
Err(Error::NotSupportedOnPlatform("stdin timeout"))
}
}