From 98378570813fee1a5c0ac36bd633f45f3ca74bc5 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Thu, 18 Aug 2022 12:18:06 +0300 Subject: [PATCH] cli/list: add filtering by group Closes #23 --- src/cli.rs | 2 +- src/cli/list.rs | 23 +++++++++++++++++++++-- src/main.rs | 4 ++-- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 507f3e4..8e2fa53 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -41,7 +41,7 @@ pub enum SubCommand { Remove(self::remove::Args), Priority(self::priority::Args), #[clap(visible_alias("ls"))] - List, + List(self::list::Args), #[clap(visible_alias("st"))] Start(self::start::Args), Pause, diff --git a/src/cli/list.rs b/src/cli/list.rs index 0da8534..1f5128c 100644 --- a/src/cli/list.rs +++ b/src/cli/list.rs @@ -16,7 +16,12 @@ use crate::domain::CurrentTaskInfo; use crate::repo::Repository; -pub fn execute(repo: impl Repository) { +#[derive(clap::Args)] +pub struct Args { + groups: Vec, +} + +pub fn execute(repo: impl Repository, args: Args) { let tasks = match repo.get_tasks() { Ok(tasks) => tasks, Err(err) => return eprintln!("Cannot read tasks: {}", err), @@ -29,7 +34,21 @@ pub fn execute(repo: impl Repository) { } }; - for (i, task) in tasks.iter().enumerate() { + let groups = args + .groups + .into_iter() + .map(|g| match g.as_str() { + "-" => None, + _ => Some(g), + }) + .collect::>(); + + let filtered_tasks = tasks + .iter() + .enumerate() + .filter(|(_, t)| groups.is_empty() || groups.contains(&t.group)); + + for (i, task) in filtered_tasks { let idx = i + 1; match cur_task { diff --git a/src/main.rs b/src/main.rs index df93cb7..86c5c15 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,8 +50,8 @@ fn main() { cli::SubCommand::Remove(args) => { cli::remove::execute(repo, args); } - cli::SubCommand::List => { - cli::list::execute(repo); + cli::SubCommand::List(args) => { + cli::list::execute(repo, args); } cli::SubCommand::Priority(args) => { cli::priority::execute(repo, args);