add labels
This commit is contained in:
parent
2aa573088c
commit
a11fe29048
5 changed files with 73 additions and 9 deletions
|
@ -35,7 +35,7 @@ where
|
||||||
F: FnMut(usize) -> Vec<T>,
|
F: FnMut(usize) -> Vec<T>,
|
||||||
{
|
{
|
||||||
let mut ret = Vec::new();
|
let mut ret = Vec::new();
|
||||||
let mut page = 0;
|
let mut page = 1;
|
||||||
loop {
|
loop {
|
||||||
let current_page = get_page(page);
|
let current_page = get_page(page);
|
||||||
if current_page.is_empty() {
|
if current_page.is_empty() {
|
||||||
|
@ -154,14 +154,25 @@ impl VikunjaAPI {
|
||||||
serde_json::from_str(&resp).unwrap()
|
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> {
|
pub fn get_task_page(&self, page: usize) -> Vec<Task> {
|
||||||
let resp = self.get_request(&format!("/tasks/all?page={page}"));
|
let resp = self.get_request(&format!("/tasks/all?page={page}"));
|
||||||
serde_json::from_str(&resp).unwrap()
|
serde_json::from_str(&resp).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
// tasks
|
|
||||||
pub fn get_all_tasks(&self) -> Vec<Task> {
|
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 {
|
pub fn get_task(&self, id: isize) -> Task {
|
||||||
|
|
|
@ -6,6 +6,7 @@ pub fn get_args() -> clap::ArgMatches {
|
||||||
.arg(arg!(-d --done "Show done tasks too").required(false))
|
.arg(arg!(-d --done "Show done tasks too").required(false))
|
||||||
.arg(arg!(-f --favorite "Show only favorites").required(false))
|
.arg(arg!(-f --favorite "Show only favorites").required(false))
|
||||||
.arg(arg!(--from <project> "Show only tasks from project").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(
|
.subcommand(
|
||||||
command!()
|
command!()
|
||||||
.name("info")
|
.name("info")
|
||||||
|
@ -29,6 +30,12 @@ pub fn get_args() -> clap::ArgMatches {
|
||||||
.default_value("Inbox"),
|
.default_value("Inbox"),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
.subcommand(
|
||||||
|
command!()
|
||||||
|
.name("label")
|
||||||
|
.about("Manage labels")
|
||||||
|
.subcommand(command!().name("ls").about("List all labels")),
|
||||||
|
)
|
||||||
.subcommand(
|
.subcommand(
|
||||||
command!()
|
command!()
|
||||||
.name("done")
|
.name("done")
|
||||||
|
|
11
src/main.rs
11
src/main.rs
|
@ -24,6 +24,14 @@ fn main() {
|
||||||
ui::project::list_projects(&api);
|
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)) => {
|
Some(("new", new_task_arg)) => {
|
||||||
let title: &String = new_task_arg.get_one("title").unwrap();
|
let title: &String = new_task_arg.get_one("title").unwrap();
|
||||||
let project: &String = new_task_arg.get_one("project").unwrap();
|
let project: &String = new_task_arg.get_one("project").unwrap();
|
||||||
|
@ -39,7 +47,8 @@ fn main() {
|
||||||
let done = arg.get_flag("done");
|
let done = arg.get_flag("done");
|
||||||
let fav = arg.get_flag("favorite");
|
let fav = arg.get_flag("favorite");
|
||||||
let project: Option<&String> = arg.get_one("from");
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,8 @@ use crossterm::{
|
||||||
ExecutableCommand,
|
ExecutableCommand,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::api::{Label, VikunjaAPI};
|
||||||
|
|
||||||
pub mod project;
|
pub mod project;
|
||||||
pub mod task;
|
pub mod task;
|
||||||
|
|
||||||
|
@ -65,3 +67,17 @@ pub fn time_since(event: DateTime<Utc>) -> String {
|
||||||
return "Just now".to_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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
api::{ProjectID, Task, VikunjaAPI},
|
api::{Label, ProjectID, Task, VikunjaAPI},
|
||||||
ui::{hex_to_color, parse_datetime, print_color, print_color_bg, time_since},
|
ui::{hex_to_color, parse_datetime, print_color, print_color_bg, print_label, time_since},
|
||||||
};
|
};
|
||||||
|
|
||||||
fn print_task_oneline(task: &Task, api: &VikunjaAPI) {
|
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 {
|
if let Some(labels) = &task.labels {
|
||||||
print!(" ");
|
print!(" ");
|
||||||
for label in labels {
|
for label in labels {
|
||||||
let color = hex_to_color(&label.hex_color).unwrap();
|
print_label(label);
|
||||||
print_color_bg(color, &label.title.trim());
|
|
||||||
print!(" ");
|
print!(" ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,7 +33,13 @@ fn print_task_oneline(task: &Task, api: &VikunjaAPI) {
|
||||||
print!("\n");
|
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/
|
// todo : improve performance by using filters -> https://vikunja.io/docs/filters/
|
||||||
let current_tasks = api.get_all_tasks();
|
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();
|
.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 {
|
for task in selection {
|
||||||
print_task_oneline(&task, api);
|
print_task_oneline(&task, api);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue