Radish alpha
h
rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5
Radicle Heartwood Protocol & Stack
Radicle
Git
rad-inbox-clear: Add confirmation dialog
Open did:key:z6MkoohH...AjJf opened 1 month ago

This patch adds a confirmation dialog to the potentially-destructive rad inbox clear command.

The default for this confirmation-dialog is set to “Y” for now, but I am of course open to change it to “N” by default - which would IMHO be a better idea for a potentially-destructive operation.

I opted for “Y” for now for better acceptance ;-)

2 files changed +47 -17 e9245b63 9acdad64
modified crates/radicle-cli/src/commands/inbox.rs
@@ -50,7 +50,7 @@ pub fn run(args: Args, ctx: impl term::Context) -> anyhow::Result<()> {
                &profile,
            )
        }
-
        Command::Clear(args) => clear(&mut notifs, args.into()),
+
        Command::Clear(args) => clear(&mut notifs, args.interactive(), args.into()),
        Command::Show { id } => show(&mut notifs, id, storage, &profile),
    }
}
@@ -385,23 +385,37 @@ impl NotificationRow {
    }
}

-
fn clear(notifs: &mut notifications::StoreWriter, mode: ClearMode) -> anyhow::Result<()> {
-
    let cleared = match mode {
-
        ClearMode::ByNotifications(ids) => notifs.clear(&ids)?,
-
        ClearMode::ByRepo(rid) => notifs.clear_by_repo(&rid)?,
-
        ClearMode::All => notifs.clear_all()?,
-
        ClearMode::Contextual => {
-
            if let Ok((_, rid)) = radicle::rad::cwd() {
-
                notifs.clear_by_repo(&rid)?
-
            } else {
-
                return Err(anyhow!("not a radicle repository"));
+
fn clear(
+
    notifs: &mut notifications::StoreWriter,
+
    interactive: radicle_term::Interactive,
+
    mode: ClearMode,
+
) -> anyhow::Result<()> {
+
    let question = match &mode {
+
        ClearMode::ByNotifications(ids) => format!("Clear {} notifications?", ids.len()),
+
        ClearMode::ByRepo(rid) => format!("Clear all notifications of repository {rid}?"),
+
        ClearMode::All => "Clear all notification?".to_string(),
+
        ClearMode::Contextual => "Clear all notifications of current repository?".to_string(),
+
    };
+

+
    if interactive.confirm(question) {
+
        let cleared = match mode {
+
            ClearMode::ByNotifications(ids) => notifs.clear(&ids)?,
+
            ClearMode::ByRepo(rid) => notifs.clear_by_repo(&rid)?,
+
            ClearMode::All => notifs.clear_all()?,
+
            ClearMode::Contextual => {
+
                if let Ok((_, rid)) = radicle::rad::cwd() {
+
                    notifs.clear_by_repo(&rid)?
+
                } else {
+
                    return Err(anyhow!("not a radicle repository"));
+
                }
            }
+
        };
+

+
        if cleared > 0 {
+
            term::success!("Cleared {cleared} item(s) from your inbox");
+
        } else {
+
            term::print(term::format::italic("Your inbox is empty."));
        }
-
    };
-
    if cleared > 0 {
-
        term::success!("Cleared {cleared} item(s) from your inbox");
-
    } else {
-
        term::print(term::format::italic("Your inbox is empty."));
    }
    Ok(())
}
modified crates/radicle-cli/src/commands/inbox/args.rs
@@ -2,6 +2,7 @@ use std::{fmt::Display, str::FromStr};

use clap::{Parser, Subcommand, ValueEnum};
use radicle::{node::notifications::NotificationId, prelude::RepoId};
+
use radicle_term::Interactive;

const ABOUT: &str = "Manage your Radicle notifications";

@@ -139,6 +140,11 @@ pub(super) struct ClearArgs {
    #[arg(long, value_name = "RID")]
    repo: Option<RepoId>,

+
    /// Do not ask for confirmation
+
    #[arg(long)]
+
    #[arg(global = true)]
+
    no_confirm: bool,
+

    /// Operate on all repositories
    #[arg(short, long, conflicts_with = "repo")]
    all: bool,
@@ -151,8 +157,18 @@ pub(super) struct ClearArgs {
    ids: Option<Vec<NotificationId>>,
}

+
impl ClearArgs {
+
    pub(super) fn interactive(&self) -> Interactive {
+
        if self.no_confirm {
+
            Interactive::No
+
        } else {
+
            Interactive::new(std::io::stdout())
+
        }
+
    }
+
}
+

impl From<ClearArgs> for ClearMode {
-
    fn from(ClearArgs { repo, all, ids }: ClearArgs) -> Self {
+
    fn from(ClearArgs { repo, all, ids, .. }: ClearArgs) -> Self {
        if let Some(ids) = ids {
            return Self::ByNotifications(ids);
        }