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),
Priority(self::priority::Args),
#[clap(visible_alias("ls"))]
List,
List(self::list::Args),
#[clap(visible_alias("st"))]
Start(self::start::Args),
Pause,

View File

@ -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<String>,
}
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::<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;
match cur_task {

View File

@ -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);