use std::cell::Cell;
use std::future::Future;
use std::io::{self, Write};
use std::pin::Pin;
use std::sync::Once;
use std::sync::atomic::{AtomicBool, Ordering};
use std::task::{Context, Poll};
use crossterm::event::{DisableBracketedPaste, EnableBracketedPaste};
use crossterm::terminal::{
EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode,
};
use crossterm::{cursor, execute};
static FULL: AtomicBool = AtomicBool::new(false);
static INLINE: AtomicBool = AtomicBool::new(false);
thread_local! {
static IN_UI: Cell<bool> = const { Cell::new(false) };
}
pub(crate) fn restore(out: &mut impl Write) {
if FULL.swap(false, Ordering::SeqCst) {
let _ = execute!(out, DisableBracketedPaste, LeaveAlternateScreen, cursor::Show);
let _ = disable_raw_mode();
let _ = out.flush();
}
if INLINE.swap(false, Ordering::SeqCst) {
let _ = execute!(out, DisableBracketedPaste);
let _ = disable_raw_mode();
let _ = out.flush();
}
}
fn restore_on_panic() {
static HOOK: Once = Once::new();
HOOK.call_once(|| {
let previous = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
if IN_UI.with(Cell::get) {
restore(&mut io::stdout());
}
previous(info);
}));
});
}
pub(crate) fn enter_full(out: &mut impl Write) -> io::Result<()> {
restore_on_panic();
enable_raw_mode()?;
FULL.store(true, Ordering::SeqCst);
if let Err(error) = execute!(out, EnterAlternateScreen, EnableBracketedPaste) {
restore(out);
return Err(error);
}
Ok(())
}
pub(crate) fn enter_inline(out: &mut impl Write) -> io::Result<()> {
restore_on_panic();
enable_raw_mode()?;
INLINE.store(true, Ordering::SeqCst);
if let Err(error) = execute!(out, EnableBracketedPaste) {
restore(out);
return Err(error);
}
Ok(())
}
pub(crate) struct Guard;
impl Drop for Guard {
fn drop(&mut self) {
restore(&mut io::stdout());
}
}
pub(crate) struct Scoped<F>(Pin<Box<F>>);
pub(crate) fn scoped<F: Future>(future: F) -> Scoped<F> {
Scoped(Box::pin(future))
}
impl<F: Future> Future for Scoped<F> {
type Output = F::Output;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<F::Output> {
struct Reset(bool);
impl Drop for Reset {
fn drop(&mut self) {
IN_UI.with(|cell| cell.set(self.0));
}
}
let _reset = Reset(IN_UI.with(|cell| cell.replace(true)));
self.0.as_mut().poll(cx)
}
}
pub(crate) struct Signals {
#[cfg(unix)]
registered: Option<(
tokio::signal::unix::Signal,
tokio::signal::unix::Signal,
tokio::signal::unix::Signal,
)>,
#[cfg(windows)]
registered: Option<tokio::signal::windows::CtrlC>,
}
impl Signals {
pub(crate) fn install(enabled: bool) -> Self {
#[cfg(unix)]
{
use tokio::signal::unix::{SignalKind, signal};
let registered = enabled
.then(|| {
Some((
signal(SignalKind::terminate()).ok()?,
signal(SignalKind::hangup()).ok()?,
signal(SignalKind::interrupt()).ok()?,
))
})
.flatten();
Self { registered }
}
#[cfg(windows)]
{
let registered = enabled.then(|| tokio::signal::windows::ctrl_c().ok()).flatten();
Self { registered }
}
#[cfg(not(any(unix, windows)))]
{
let _ = enabled;
Self {}
}
}
pub(crate) async fn recv(&mut self) -> i32 {
#[cfg(unix)]
if let Some((term, hup, int)) = &mut self.registered {
return tokio::select! {
_ = term.recv() => 143,
_ = hup.recv() => 129,
_ = int.recv() => 130,
};
}
#[cfg(windows)]
if let Some(ctrl_c) = &mut self.registered {
ctrl_c.recv().await;
return 130;
}
std::future::pending().await
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_terminal_is_given_back_once_and_on_a_panic_of_its_own() {
FULL.store(true, Ordering::SeqCst);
let mut out = Vec::new();
restore(&mut out);
let written = String::from_utf8(out).unwrap();
assert!(written.contains("\x1b[?1049l"), "{written:?}");
assert!(written.contains("\x1b[?25h"), "{written:?}");
assert!(written.contains("\x1b[?2004l"), "{written:?}");
let mut again = Vec::new();
restore(&mut again);
assert!(again.is_empty());
INLINE.store(true, Ordering::SeqCst);
let mut out = Vec::new();
restore(&mut out);
let written = String::from_utf8(out).unwrap();
assert!(written.contains("\x1b[?2004l"), "{written:?}");
assert!(!written.contains("\x1b[?1049l"), "inline never took the alternate screen");
restore_on_panic();
FULL.store(true, Ordering::SeqCst);
let caught = std::thread::spawn(|| panic!("a tool's bug, caught")).join();
assert!(caught.is_err());
assert!(FULL.load(Ordering::SeqCst), "a caught panic elsewhere took the terminal back");
let own = std::thread::spawn(|| {
futures::executor::block_on(scoped(async { panic!("a bug while drawing") }))
})
.join();
assert!(own.is_err());
assert!(!FULL.load(Ordering::SeqCst), "the terminal's own panic gave it back");
}
#[cfg(any(unix, windows))]
#[test]
fn no_signal_is_registered_when_the_application_keeps_them() {
let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap();
runtime.block_on(async {
assert!(Signals::install(false).registered.is_none());
assert!(Signals::install(true).registered.is_some());
});
}
}