use std::{convert::TryFrom, time::Duration};
use ncursesw::{NCurseswError, WINDOW, shims::ncurses};
use crate::{Timeout, NCurseswWinError, gen::HasHandle};
pub trait HasNonBlocking: HasHandle<WINDOW> {
#[deprecated(since = "0.1.0", note = "ambiguous function name. Use get_timeout() instead")]
fn getdelay(&self) -> result!(Duration) {
Ok(ncursesw::wgetdelay(self._handle())?)
}
#[deprecated(since = "0.1.0", note = "ambiguous function name. Use set_timeout() instead")]
fn timeout(&self, ms: Duration) -> result!(()) {
Ok(ncursesw::wtimeout(self._handle(), ms)?)
}
fn get_timeout(&self) -> result!(Timeout) {
match unsafe { ncurses::wgetdelay(self._handle()) } {
-1 => Ok(None),
rc => {
if rc < 0 {
Err(NCurseswWinError::from(NCurseswError::LibraryError { func: "wgetdelay".to_string(), rc: Some(rc) }))
} else {
Ok(Some(Duration::from_millis(u64::try_from(rc)?)))
}
}
}
}
fn set_timeout(&self, ms: Timeout) -> result!(()) {
match ms {
None => unsafe { ncurses::wtimeout(self._handle(), -1) },
Some(ms) => ncursesw::wtimeout(self._handle(), ms)?
}
Ok(())
}
}