Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
cli: Allow caller of `confirm` to choose `Y/n` or `y/N`
Fintan Halpenny committed 2 months ago
commit 2efb5132ee631d081cbac17c7a9885d0da3c0213
parent 423cf604e1178d62c6952f866b4b5d0b9667aca8
5 files changed +44 -25
modified crates/radicle-cli/src/commands/clean.rs
@@ -17,7 +17,7 @@ pub fn run(args: Args, ctx: impl term::Context) -> anyhow::Result<()> {
        anyhow::bail!("repository {rid} was not found");
    }

-
    if args.no_confirm || term::confirm(format!("Clean {rid}?")) {
+
    if args.no_confirm || term::confirm(format!("Clean {rid}?"), term::DefaultConfirmation::No) {
        let cleaned = storage.clean(rid)?;
        for remote in cleaned {
            term::info!("Removed {remote}");
modified crates/radicle-cli/src/commands/id.rs
@@ -53,7 +53,10 @@ pub fn run(args: Args, ctx: impl term::Context) -> anyhow::Result<()> {
                anyhow::bail!("cannot vote on revision that is {}", revision.state);
            }

-
            if interactive.confirm(format!("Accept revision {}?", term::format::tertiary(id))) {
+
            if interactive.confirm(
+
                format!("Accept revision {}?", term::format::tertiary(id)),
+
                term::DefaultConfirmation::No,
+
            ) {
                identity.accept(&revision.id, &signer)?;

                if let Some(revision) = identity.revision(&id) {
@@ -78,10 +81,10 @@ pub fn run(args: Args, ctx: impl term::Context) -> anyhow::Result<()> {
                anyhow::bail!("cannot vote on revision that is {}", revision.state);
            }

-
            if interactive.confirm(format!(
-
                "Reject revision {}?",
-
                term::format::tertiary(revision.id)
-
            )) {
+
            if interactive.confirm(
+
                format!("Reject revision {}?", term::format::tertiary(revision.id)),
+
                term::DefaultConfirmation::No,
+
            ) {
                identity.reject(revision.id, &signer)?;

                if !args.quiet {
@@ -258,10 +261,10 @@ pub fn run(args: Args, ctx: impl term::Context) -> anyhow::Result<()> {
            if revision.is_accepted() {
                anyhow::bail!("cannot redact accepted revision");
            }
-
            if interactive.confirm(format!(
-
                "Redact revision {}?",
-
                term::format::tertiary(revision.id)
-
            )) {
+
            if interactive.confirm(
+
                format!("Redact revision {}?", term::format::tertiary(revision.id)),
+
                term::DefaultConfirmation::No,
+
            ) {
                identity.redact(revision.id, &signer)?;

                if !args.quiet {
modified crates/radicle-cli/src/commands/init.rs
@@ -500,11 +500,14 @@ pub fn setup_signing(
        ));
        true
    } else if interactive.yes() {
-
        term::confirm(format!(
-
            "Configure radicle signing key {} in {}?",
-
            term::format::tertiary(key),
-
            term::format::tertiary(config.display()),
-
        ))
+
        term::confirm(
+
            format!(
+
                "Configure radicle signing key {} in {}?",
+
                term::format::tertiary(key),
+
                term::format::tertiary(config.display()),
+
            ),
+
            term::DefaultConfirmation::Yes,
+
        )
    } else {
        true
    };
@@ -536,7 +539,10 @@ pub fn setup_signing(

                if ssh_keys.contains(&ssh_key) {
                    term::success!("Signing key is already in {gitsigners} file");
-
                } else if term::confirm(format!("Add signing key to {gitsigners}?")) {
+
                } else if term::confirm(
+
                    format!("Add signing key to {gitsigners}?"),
+
                    term::DefaultConfirmation::Yes,
+
                ) {
                    git::add_gitsigners(repo, [node_id])?;
                }
            }
modified crates/radicle-term/src/io.rs
@@ -11,7 +11,7 @@ use inquire::{ui::Color, ui::RenderConfig, Confirm, CustomType, Password};
use thiserror::Error;
use zeroize::Zeroizing;

-
use crate::format;
+
use crate::{format, DefaultConfirmation};
use crate::{style, Paint, Size};

pub use inquire;
@@ -238,12 +238,11 @@ pub fn ask<D: fmt::Display>(prompt: D, default: bool) -> bool {
        .unwrap_or_default()
}

-
pub fn confirm<D: fmt::Display>(prompt: D) -> bool {
-
    ask(prompt, true)
-
}
-

-
pub fn abort<D: fmt::Display>(prompt: D) -> bool {
-
    ask(prompt, false)
+
pub fn confirm<D: fmt::Display>(prompt: D, default: DefaultConfirmation) -> bool {
+
    match default {
+
        DefaultConfirmation::Yes => ask(prompt, true),
+
        DefaultConfirmation::No => ask(prompt, false),
+
    }
}

#[non_exhaustive]
modified crates/radicle-term/src/lib.rs
@@ -35,6 +35,17 @@ pub enum Interactive {
    No,
}

+
/// When asking for interactive confirmation from the user, choose which default
+
/// should be chosen when the user presses enter with no input.
+
#[derive(Debug, PartialEq, Eq, Copy, Clone, Default)]
+
pub enum DefaultConfirmation {
+
    /// Equivalent to `Y/n`.
+
    Yes,
+
    /// Equivalent to `y/N`.
+
    #[default]
+
    No,
+
}
+

impl Interactive {
    pub fn new(term: impl IsTerminal) -> Self {
        Self::from(term.is_terminal())
@@ -48,9 +59,9 @@ impl Interactive {
        !self.yes()
    }

-
    pub fn confirm(&self, prompt: impl fmt::Display) -> bool {
+
    pub fn confirm(&self, prompt: impl fmt::Display, default: DefaultConfirmation) -> bool {
        if self.yes() {
-
            confirm(prompt)
+
            confirm(prompt, default)
        } else {
            true
        }