sim-cli 0.23.0

CLI tool for running and comparing Solana simulator backtests
Documentation
use std::{
    fmt,
    io::{IsTerminal, stderr},
};

use tokio::signal::{
    ctrl_c,
    unix::{SignalKind, signal},
};
use tokio_util::sync::CancellationToken;
use tracing::{info, warn};

/// Exit code for a run stopped by a signal before it finished (128 + SIGINT).
pub const EXIT_INTERRUPTED: i32 = 130;

/// A run stopped by a signal before it finished; the binary exits with
/// [`EXIT_INTERRUPTED`] on it.
#[derive(Debug)]
pub struct Interrupted;

impl fmt::Display for Interrupted {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(
            "stopped by a signal before the run finished; the output file is partial and has \
             no `summary` line",
        )
    }
}

impl std::error::Error for Interrupted {}

/// First signal cancels the token (triggering a graceful session close); a
/// second signal exits immediately without waiting for it.
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() => return,
        }
        cancellation_token.cancel();

        tokio::select! {
            _ = ctrl_c() => {}
            _ = terminate.recv() => {}
            _ = quit.recv() => {}
        }
        warn!("Received second signal; exiting without graceful close");
        std::process::exit(EXIT_INTERRUPTED);
    });
}

/// Spawn a task that erases the "^C" terminal echo on Ctrl+C.
///
/// When Ctrl+C is pressed, the terminal echoes "^C" on the current line, which shifts
/// indicatif's cursor tracking by one line and causes the top progress bar to be rendered twice.
/// This moves the cursor back up so that it's in the right position before re-rendering.
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");
            }
        });
    }
}