From 870ec706b33c12f8d3f98488f83ff88e6d73f7ab Mon Sep 17 00:00:00 2001 From: JMARyA Date: Thu, 6 Jun 2024 09:10:28 +0200 Subject: [PATCH] update --- src/api/mod.rs | 15 +++++++++++++++ src/args.rs | 1 + src/main.rs | 3 ++- src/ui/task.rs | 15 ++++++++++++--- 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/api/mod.rs b/src/api/mod.rs index eba652c..1eaa550 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -72,6 +72,21 @@ impl VikunjaAPI { found.title } + pub fn parse_project_id(&self, project: &str) -> Option { + let project = project.trim_start_matches("#"); + + if let Ok(num) = project.parse() { + Some(num) + } else { + Some( + self.get_all_projects() + .into_iter() + .find(|x| x.title.contains(project))? + .id, + ) + } + } + pub fn get_all_projects(&self) -> Vec { let resp = self.get_request("/projects"); serde_json::from_str(&resp).unwrap() diff --git a/src/args.rs b/src/args.rs index dc82a39..8c1720f 100644 --- a/src/args.rs +++ b/src/args.rs @@ -5,6 +5,7 @@ pub fn get_args() -> clap::ArgMatches { .about("CLI Tool for Vikunja") .arg(arg!(-d --done "Show done tasks too").required(false)) .arg(arg!(-f --favorite "Show only favorites").required(false)) + .arg(arg!(--from "Show only tasks from project").required(false)) .subcommand( command!() .name("info") diff --git a/src/main.rs b/src/main.rs index 8775b66..90ccde0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,7 +27,8 @@ fn main() { _ => { let done = arg.get_flag("done"); let fav = arg.get_flag("favorite"); - ui::task::print_current_tasks(&api, done, fav); + let project: Option<&String> = arg.get_one("from"); + ui::task::print_current_tasks(&api, done, fav, project); } } } diff --git a/src/ui/task.rs b/src/ui/task.rs index b060304..783ff8d 100644 --- a/src/ui/task.rs +++ b/src/ui/task.rs @@ -15,21 +15,30 @@ fn print_task_oneline(task: &Task, api: &VikunjaAPI) { ); } -pub fn print_current_tasks(api: &VikunjaAPI, done: bool, fav: bool) { +pub fn print_current_tasks(api: &VikunjaAPI, done: bool, fav: bool, project: Option<&String>) { let current_tasks = api.get_all_tasks(); - let selection: Vec<_> = if done { + let mut selection: Vec<_> = if done { current_tasks } else { current_tasks.into_iter().filter(|x| !x.done).collect() }; - let selection = if fav { + selection = if fav { selection.into_iter().filter(|x| x.is_favorite).collect() } else { selection }; + if let Some(project) = project { + let p_id = api.parse_project_id(project).unwrap(); + + selection = selection + .into_iter() + .filter(|x| x.project_id == p_id) + .collect(); + } + for task in selection { print_task_oneline(&task, api); }