cli/list: add filtering by group

Closes #23
This commit is contained in:
Dmitriy Pleshevskiy 2022-08-18 12:18:06 +03:00
parent 9674918396
commit 9837857081
Signed by: pleshevskiy
GPG key ID: 1B59187B161C0215
3 changed files with 24 additions and 5 deletions

View file

@ -41,7 +41,7 @@ pub enum SubCommand {
Remove(self::remove::Args), Remove(self::remove::Args),
Priority(self::priority::Args), Priority(self::priority::Args),
#[clap(visible_alias("ls"))] #[clap(visible_alias("ls"))]
List, List(self::list::Args),
#[clap(visible_alias("st"))] #[clap(visible_alias("st"))]
Start(self::start::Args), Start(self::start::Args),
Pause, Pause,

View file

@ -16,7 +16,12 @@
use crate::domain::CurrentTaskInfo; use crate::domain::CurrentTaskInfo;
use crate::repo::Repository; use crate::repo::Repository;
pub fn execute(repo: impl Repository) { #[derive(clap::Args)]
pub struct Args {
groups: Vec<String>,
}
pub fn execute(repo: impl Repository, args: Args) {
let tasks = match repo.get_tasks() { let tasks = match repo.get_tasks() {
Ok(tasks) => tasks, Ok(tasks) => tasks,
Err(err) => return eprintln!("Cannot read tasks: {}", err), 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::<Vec<_>>();
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; let idx = i + 1;
match cur_task { match cur_task {

View file

@ -50,8 +50,8 @@ fn main() {
cli::SubCommand::Remove(args) => { cli::SubCommand::Remove(args) => {
cli::remove::execute(repo, args); cli::remove::execute(repo, args);
} }
cli::SubCommand::List => { cli::SubCommand::List(args) => {
cli::list::execute(repo); cli::list::execute(repo, args);
} }
cli::SubCommand::Priority(args) => { cli::SubCommand::Priority(args) => {
cli::priority::execute(repo, args); cli::priority::execute(repo, args);