#[cfg(unix)]
mod imp {
use parking_lot::Mutex;
use std::sync::OnceLock;
static SAVED: OnceLock<Mutex<Option<libc::termios>>> = OnceLock::new();
static HANDLER_INSTALLED: OnceLock<()> = OnceLock::new();
pub(crate) fn save_if_tty() {
let cell = SAVED.get_or_init(|| Mutex::new(None));
let mut guard = cell.lock();
if guard.is_some() {
return;
}
unsafe {
if libc::isatty(0) != 1 {
return;
}
let mut t: libc::termios = std::mem::zeroed();
if libc::tcgetattr(0, &mut t) == 0 {
*guard = Some(t);
}
}
}
pub(crate) fn restore() {
let Some(cell) = SAVED.get() else { return };
let guard = cell.lock();
let Some(t) = *guard else { return };
unsafe {
let _ = libc::tcsetattr(0, libc::TCSANOW, &t);
}
}
pub(crate) fn install_signal_handler() {
if std::env::var_os("PLAYWRIGHT_NO_SIGNAL_HANDLER").is_some() {
return;
}
if HANDLER_INSTALLED.set(()).is_err() {
return;
}
tokio::spawn(async move {
if tokio::signal::ctrl_c().await.is_ok() {
restore();
std::process::exit(130);
}
});
}
}
#[cfg(not(unix))]
mod imp {
pub(crate) fn save_if_tty() {}
pub(crate) fn restore() {}
pub(crate) fn install_signal_handler() {}
}
pub(crate) use imp::{install_signal_handler, restore, save_if_tty};