Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
REVIEW: introduce the modes back for list and clear
Fintan Halpenny committed 6 months ago
commit 76f5131a10b365c705a455090816e9d9c2a46dd7
parent 03f30e1fc1c442f1a82a822c810b7da58e2d8019
2 files changed +78 -28
modified crates/radicle-cli/src/commands/inbox.rs
@@ -24,13 +24,14 @@ use radicle::{cob, git, Storage};
use term::Element as _;

use crate::terminal as term;
-
use args::{Command, ListArgs, SortBy};
+
use args::{ClearMode, Command, ListArgs, ListMode, SortBy};

pub fn run(args: Args, ctx: impl term::Context) -> anyhow::Result<()> {
    let profile = ctx.profile()?;
    let storage = &profile.storage;
    let mut cache = profile.notifications_mut()?;
    let command = args
+
        .clone()
        .command
        .unwrap_or_else(|| Command::List(args.empty.into()));

@@ -41,32 +42,29 @@ pub fn run(args: Args, ctx: impl term::Context) -> anyhow::Result<()> {
            show_unknown,
        }) => list(
            &cache.read_only(),
-
            args.all,
-
            args.repo,
+
            args.list_mode(),
            sort_by,
            reverse,
            show_unknown,
            storage,
            &profile,
        ),
-
        Command::Clear { ids } => clear(&mut cache, args.all, args.repo, ids),
+
        Command::Clear { ids } => clear(&mut cache, args.clear_mode(ids)),
        Command::Show { id } => show(&mut cache, id, storage, &profile),
    }
}

fn list(
    notifs: &notifications::StoreReader,
-
    all: bool,
-
    repo: Option<RepoId>,
+
    mode: ListMode,
    sort_by: SortBy,
    reverse: bool,
    show_unknown: bool,
    storage: &Storage,
    profile: &Profile,
) -> anyhow::Result<()> {
-
    let repos: Vec<term::VStack<'_>> = match (all, repo) {
-
        (true, None) => list_all(notifs, sort_by, reverse, show_unknown, storage, profile)?,
-
        (false, None) => {
+
    let repos: Vec<term::VStack<'_>> = match mode {
+
        ListMode::Contextual => {
            if let Ok((_, rid)) = radicle::rad::cwd() {
                list_repo(
                    notifs,
@@ -83,7 +81,8 @@ fn list(
                list_all(notifs, sort_by, reverse, show_unknown, storage, profile)?
            }
        }
-
        (false, Some(rid)) => list_repo(
+
        ListMode::All => list_all(notifs, sort_by, reverse, show_unknown, storage, profile)?,
+
        ListMode::ByRepo(rid) => list_repo(
            notifs,
            rid,
            sort_by,
@@ -94,7 +93,6 @@ fn list(
        )?
        .into_iter()
        .collect(),
-
        (true, Some(_)) => list_all(notifs, sort_by, reverse, show_unknown, storage, profile)?,
    };

    if repos.is_empty() {
@@ -123,7 +121,7 @@ fn list_all<'a>(
        let vstack = list_repo(
            notifs,
            repo.rid,
-
            sort_by.clone(),
+
            sort_by,
            reverse,
            show_unknown,
            storage,
@@ -386,17 +384,12 @@ impl NotificationRow {
    }
}

-
fn clear(
-
    notifs: &mut notifications::StoreWriter,
-
    all: bool,
-
    rid: Option<RepoId>,
-
    ids: Option<Vec<NotificationId>>,
-
) -> anyhow::Result<()> {
-
    let cleared = match (all, rid, ids) {
-
        (true, _, _) => notifs.clear_all()?,
-
        (_, _, Some(ids)) => notifs.clear(&ids)?,
-
        (_, Some(rid), _) => notifs.clear_by_repo(&rid)?,
-
        (_, None, _) => {
+
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 {
modified crates/radicle-cli/src/commands/inbox/args.rs
@@ -18,7 +18,7 @@ The `rad inbox clear` command will delete all notifications by their passed id
or all notifications if no ids were passed.
"#;

-
#[derive(Debug, Parser)]
+
#[derive(Clone, Debug, Parser)]
#[command(about = ABOUT, long_about = LONG_ABOUT, disable_version_flag = true)]
pub struct Args {
    #[command(subcommand)]
@@ -39,7 +39,39 @@ pub struct Args {
    pub(crate) empty: EmptyArgs,
}

-
#[derive(Subcommand, Debug)]
+
impl Args {
+
    pub(super) fn list_mode(&self) -> ListMode {
+
        if self.all {
+
            assert!(self.repo.is_none());
+
            return ListMode::All;
+
        }
+

+
        if let Some(repo) = self.repo {
+
            return ListMode::ByRepo(repo);
+
        }
+

+
        ListMode::Contextual
+
    }
+

+
    pub(super) fn clear_mode(&self, ids: Option<Vec<NotificationId>>) -> ClearMode {
+
        if let Some(ids) = ids {
+
            return ClearMode::ByNotifications(ids);
+
        }
+

+
        if self.all {
+
            assert!(self.repo.is_none());
+
            return ClearMode::All;
+
        }
+

+
        if let Some(repo) = self.repo {
+
            return ClearMode::ByRepo(repo);
+
        }
+

+
        ClearMode::Contextual
+
    }
+
}
+

+
#[derive(Subcommand, Clone, Debug)]
pub(crate) enum Command {
    /// List all items in your inbox
    List(ListArgs),
@@ -65,7 +97,7 @@ pub(crate) enum Command {
    },
}

-
#[derive(Parser, Debug)]
+
#[derive(Parser, Clone, Copy, Debug)]
pub struct EmptyArgs {
    /// Sort by column
    #[arg(long, value_enum, default_value_t, hide = true)]
@@ -80,7 +112,7 @@ pub struct EmptyArgs {
    show_unknown: bool,
}

-
#[derive(Parser, Debug)]
+
#[derive(Parser, Clone, Copy, Debug)]
pub struct ListArgs {
    /// Sort by column
    #[arg(long, value_enum, default_value_t)]
@@ -111,7 +143,7 @@ impl From<EmptyArgs> for ListArgs {
    }
}

-
#[derive(ValueEnum, Clone, Default, Debug)]
+
#[derive(ValueEnum, Clone, Copy, Default, Debug)]
pub enum SortBy {
    Id,
    #[default]
@@ -138,3 +170,28 @@ impl FromStr for SortBy {
        }
    }
}
+

+
pub(super) enum ListMode {
+
    /// List the notifications of the current repository, if in a working
+
    /// directory, otherwise all the repositories.
+
    Contextual,
+
    /// List the notifications for a all repositories.
+
    All,
+
    /// List the notifications for a specific repository.
+
    ByRepo(RepoId),
+
}
+

+
pub(super) enum ClearMode {
+
    /// Clear the specified notifications.
+
    ///
+
    /// Note that this does not require a `RepoId` since the IDs are globally
+
    /// unique due to the use of a single sqlite table.
+
    ByNotifications(Vec<NotificationId>),
+
    /// Clear the notifications of a specific repository.
+
    ByRepo(RepoId),
+
    /// Clear all notifications of all repositories.
+
    All,
+
    /// Clear the notifications of the current repository, only if in a working
+
    /// directory.
+
    Contextual,
+
}