use std::io::{IsTerminal, stderr};
use tokio::signal::{
ctrl_c,
unix::{SignalKind, signal},
};
use tokio_util::sync::CancellationToken;
use tracing::{debug, info};
pub fn spawn_os_signal_handler(cancellation_token: CancellationToken) {
tokio::spawn(async move {
let mut terminate =
signal(SignalKind::terminate()).expect("Failed to set up SIGTERM handler");
let mut quit = signal(SignalKind::quit()).expect("Failed to set up SIGQUIT handler");
tokio::select! {
_ = ctrl_c() => info!("Received Ctrl+C"),
_ = terminate.recv() => info!("Received SIGTERM"),
_ = quit.recv() => info!("Received SIGQUIT"),
_ = cancellation_token.cancelled() => {
debug!("Stopping OS signal handling task, as streamer has been stopped programmatically");
return;
}
}
cancellation_token.cancel();
});
}
pub fn spawn_ctrlc_cursor_fix() {
if stderr().is_terminal() {
tokio::spawn(async {
if ctrl_c().await.is_ok() {
eprint!("\r\x1b[2K\x1b[1A");
}
});
}
}