use crate::{error, sys};
#[allow(clippy::module_name_repetitions)]
pub struct TerminalControl {
prev_fg_pid: Option<sys::process::ProcessId>,
}
impl TerminalControl {
pub fn acquire() -> Result<Self, error::Error> {
let prev_fg_pid = sys::terminal::get_foreground_pid();
let _ = sys::signal::lead_new_process_group();
sys::terminal::move_self_to_foreground()?;
sys::signal::mask_sigttou()?;
Ok(Self { prev_fg_pid })
}
fn try_release(&mut self) {
if let Some(pid) = self.prev_fg_pid {
if sys::terminal::move_to_foreground(pid).is_ok() {
self.prev_fg_pid = None;
}
}
}
}
impl Drop for TerminalControl {
fn drop(&mut self) {
self.try_release();
}
}