use crate::{Linux, LinuxResult as Result};
use crate::{LinuxTermios, ScopeGuard, TermSize};
pub(crate) type LinuxTermModeGuard = ScopeGuard<LinuxTermios, fn(LinuxTermios, &()), ()>;
#[cfg(feature = "term")]
impl Linux {
#[must_use]
pub fn is_terminal() -> bool {
LinuxTermios::is_terminal()
}
pub fn terminal_size() -> Result<TermSize> {
LinuxTermios::read_window_size()
}
pub fn scoped_event_mode() -> Result<LinuxTermModeGuard> {
Self::scoped_termios_update(|state| state.make_event())
}
pub fn scoped_raw_mode() -> Result<LinuxTermModeGuard> {
Self::scoped_termios_update(|state| state.make_raw())
}
pub fn scoped_termios_update(f: impl FnOnce(&mut LinuxTermios)) -> Result<LinuxTermModeGuard> {
let initial_state = LinuxTermios::read_state()?;
let mut active_state = initial_state;
f(&mut active_state);
active_state.write_state()?;
Ok(ScopeGuard::with(initial_state, (), Self::restore_linux_termios))
}
pub fn enable_raw_mode() -> Result<()> {
LinuxTermios::enable_raw_mode()
}
pub fn reset_cooked_mode() -> Result<()> {
LinuxTermios::reset_cooked_mode()
}
#[allow(
clippy::trivially_copy_pass_by_ref,
reason = "matches ScopeGuard's FnOnce(T, &S) callback signature"
)]
fn restore_linux_termios(state: LinuxTermios, (): &()) {
let _ = state.write_state();
}
}