This commit is contained in:
JMARyA 2024-06-06 09:10:28 +02:00
parent 4c7c5d88fa
commit 870ec706b3
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
4 changed files with 30 additions and 4 deletions

View file

@ -72,6 +72,21 @@ impl VikunjaAPI {
found.title
}
pub fn parse_project_id(&self, project: &str) -> Option<isize> {
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<Project> {
let resp = self.get_request("/projects");
serde_json::from_str(&resp).unwrap()

View file

@ -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 <project> "Show only tasks from project").required(false))
.subcommand(
command!()
.name("info")

View file

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

View file

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