Repository: launchbadge/sqlx
File: sqlx-cli/src/database.rs (Rust)

Code before:
    force: bool,
) -> anyhow::Result<()> {
    drop(connect_opts, confirm, force).await?;
    setup(migration_source, connect_opts).await
}

pub async fn setup(migration_source: &str, connect_opts: &ConnectOpts) -> anyhow::Result<()> {
    create(connect_opts).await?;
    migrate::run(migration_source, connect_opts, false, false, None).await
}

async fn ask_to_continue_drop(db_url: String) -> bool {
    let ctrlc_fut = signal::ctrl_c();
    let decision_fut = task::spawn_blocking(move || {
        Confirm::new()
            .with_prompt(format!("Drop database at {}?", style(&db_url).cyan()))
            .wait_for_newline(true)
            .default(false)
            .show_default(true)
            .interact()
    });

    let restore_cursor = || Term::stderr().show_cursor().unwrap();

    select! {
        biased;
        _ = ctrlc_fut => {
            restore_cursor();
            false
        },
        decision = decision_fut => {
            match decision.expect("Confirm thread panicked") {
                Ok(decision) => decision,
                Err(dialoguer::Error::IO(err)) if err.kind() == io::ErrorKind::Interrupted => {
                    // Sometimes CTRL + C causes this error to be returned
                    restore_cursor();
                    false
                },
                Err(err) => panic!("Confirm dialog failed with {err}"),
            }
        }
    }
}


Review comment (suggestion):
Context: despite `biased` sometimes the CTRL + C future returns first, other times the decision futures is ready first.

Code after:
    force: bool,
) -> anyhow::Result<()> {
    drop(connect_opts, confirm, force).await?;
    setup(migration_source, connect_opts).await
}

pub async fn setup(migration_source: &str, connect_opts: &ConnectOpts) -> anyhow::Result<()> {
    create(connect_opts).await?;
    migrate::run(migration_source, connect_opts, false, false, None).await
}

async fn ask_to_continue_drop(db_url: String) -> bool {
    // If the setup operation is cancelled while we are waiting for the user to decide whether
    // or not to drop the database, this will restore the terminal's cursor to its normal state.
    struct RestoreCursorGuard {
        disarmed: bool,
    }

    impl Drop for RestoreCursorGuard {
        fn drop(&mut self) {
            if !self.disarmed {
                Term::stderr().show_cursor().unwrap()
            }
        }
    }

    let mut guard = RestoreCursorGuard { disarmed: false };

    let decision_result = task::spawn_blocking(move || {
        Confirm::new()
            .with_prompt(format!("Drop database at {}?", style(&db_url).cyan()))
            .wait_for_newline(true)
            .default(false)
            .show_default(true)
            .interact()
    })
    .await
    .expect("Confirm thread panicked");
    match decision_result {
        Ok(decision) => {
            guard.disarmed = true;
            decision
        }
        Err(dialoguer::Error::IO(err)) if err.kind() == io::ErrorKind::Interrupted => {
            // Sometimes CTRL + C causes this error to be returned
            mem::drop(guard);
            false
        }
        Err(err) => {
            mem::drop(guard);
            panic!("Confirm dialog failed with {err}")