add labels

This commit is contained in:
JMARyA 2024-06-06 11:17:47 +02:00
parent 2aa573088c
commit a11fe29048
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
5 changed files with 73 additions and 9 deletions

View file

@ -35,7 +35,7 @@ where
F: FnMut(usize) -> Vec<T>,
{
let mut ret = Vec::new();
let mut page = 0;
let mut page = 1;
loop {
let current_page = get_page(page);
if current_page.is_empty() {
@ -154,14 +154,25 @@ impl VikunjaAPI {
serde_json::from_str(&resp).unwrap()
}
// labels
pub fn get_all_labels(&self) -> Vec<Label> {
get_all_items(|x| {
let resp = self.get_request(&format!("/labels?page={x}"));
if resp.trim() == "null" {
return Vec::new();
}
serde_json::from_str(&resp).unwrap()
})
}
// tasks
pub fn get_task_page(&self, page: usize) -> Vec<Task> {
let resp = self.get_request(&format!("/tasks/all?page={page}"));
serde_json::from_str(&resp).unwrap()
}
// tasks
pub fn get_all_tasks(&self) -> Vec<Task> {
unique_tasks(get_all_items(|x| self.get_task_page(x)))
get_all_items(|x| self.get_task_page(x))
}
pub fn get_task(&self, id: isize) -> Task {

View file

@ -6,6 +6,7 @@ pub fn get_args() -> clap::ArgMatches {
.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))
.arg(arg!(-l --label <label> "Show only tasks with label").required(false))
.subcommand(
command!()
.name("info")
@ -29,6 +30,12 @@ pub fn get_args() -> clap::ArgMatches {
.default_value("Inbox"),
),
)
.subcommand(
command!()
.name("label")
.about("Manage labels")
.subcommand(command!().name("ls").about("List all labels")),
)
.subcommand(
command!()
.name("done")

View file

@ -24,6 +24,14 @@ fn main() {
ui::project::list_projects(&api);
}
},
Some(("label", label_args)) => match label_args.subcommand() {
Some(("ls", _)) => {
ui::print_all_labels(&api);
}
_ => {
ui::print_all_labels(&api);
}
},
Some(("new", new_task_arg)) => {
let title: &String = new_task_arg.get_one("title").unwrap();
let project: &String = new_task_arg.get_one("project").unwrap();
@ -39,7 +47,8 @@ fn main() {
let done = arg.get_flag("done");
let fav = arg.get_flag("favorite");
let project: Option<&String> = arg.get_one("from");
ui::task::print_current_tasks(&api, done, fav, project);
let label: Option<&String> = arg.get_one("label");
ui::task::print_current_tasks(&api, done, fav, project, label);
}
}
}

View file

@ -6,6 +6,8 @@ use crossterm::{
ExecutableCommand,
};
use crate::api::{Label, VikunjaAPI};
pub mod project;
pub mod task;
@ -65,3 +67,17 @@ pub fn time_since(event: DateTime<Utc>) -> String {
return "Just now".to_string();
}
}
fn print_label(label: &Label) {
let color = hex_to_color(&label.hex_color).unwrap();
print_color_bg(color, &label.title.trim());
}
pub fn print_all_labels(api: &VikunjaAPI) {
let labels = api.get_all_labels();
for label in labels {
print_label(&label);
print!("\n\n");
}
}

View file

@ -1,6 +1,6 @@
use crate::{
api::{ProjectID, Task, VikunjaAPI},
ui::{hex_to_color, parse_datetime, print_color, print_color_bg, time_since},
api::{Label, ProjectID, Task, VikunjaAPI},
ui::{hex_to_color, parse_datetime, print_color, print_color_bg, print_label, time_since},
};
fn print_task_oneline(task: &Task, api: &VikunjaAPI) {
@ -25,8 +25,7 @@ fn print_task_oneline(task: &Task, api: &VikunjaAPI) {
if let Some(labels) = &task.labels {
print!(" ");
for label in labels {
let color = hex_to_color(&label.hex_color).unwrap();
print_color_bg(color, &label.title.trim());
print_label(label);
print!(" ");
}
}
@ -34,7 +33,13 @@ fn print_task_oneline(task: &Task, api: &VikunjaAPI) {
print!("\n");
}
pub fn print_current_tasks(api: &VikunjaAPI, done: bool, fav: bool, project: Option<&String>) {
pub fn print_current_tasks(
api: &VikunjaAPI,
done: bool,
fav: bool,
project: Option<&String>,
label: Option<&String>,
) {
// todo : improve performance by using filters -> https://vikunja.io/docs/filters/
let current_tasks = api.get_all_tasks();
@ -59,6 +64,22 @@ pub fn print_current_tasks(api: &VikunjaAPI, done: bool, fav: bool, project: Opt
.collect();
}
if let Some(label_match) = label {
selection = selection
.into_iter()
.filter(|x| {
if let Some(labels) = &x.labels {
for label in labels {
if label.title.trim() == *label_match {
return true;
}
}
}
false
})
.collect();
}
for task in selection {
print_task_oneline(&task, api);
}