implement labels
This commit is contained in:
parent
c469386c6a
commit
c2944722dd
4 changed files with 97 additions and 14 deletions
|
@ -125,6 +125,18 @@ impl VikunjaAPI {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn delete_request(&self, path: &str) -> String {
|
||||||
|
let client = reqwest::blocking::Client::new();
|
||||||
|
|
||||||
|
client
|
||||||
|
.delete(format!("{}/api/v1{}", self.host, path))
|
||||||
|
.header("Authorization", format!("Bearer {}", self.token))
|
||||||
|
.send()
|
||||||
|
.unwrap()
|
||||||
|
.text()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
// projects
|
// projects
|
||||||
|
|
||||||
pub fn get_project_name_from_id(&self, id: isize) -> String {
|
pub fn get_project_name_from_id(&self, id: isize) -> String {
|
||||||
|
@ -164,6 +176,47 @@ impl VikunjaAPI {
|
||||||
serde_json::from_str(&resp).unwrap()
|
serde_json::from_str(&resp).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn remove_label(&self, title: &str) {
|
||||||
|
let labels = self.get_all_labels();
|
||||||
|
|
||||||
|
let label_id = labels
|
||||||
|
.into_iter()
|
||||||
|
.find(|x| x.title.trim() == title)
|
||||||
|
.unwrap()
|
||||||
|
.id;
|
||||||
|
|
||||||
|
self.delete_request(&format!("/labels/{label_id}"));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn label_task_remove(&self, label: &str, task_id: isize) {
|
||||||
|
let labels = self.get_all_labels();
|
||||||
|
|
||||||
|
let label_id = labels
|
||||||
|
.into_iter()
|
||||||
|
.find(|x| x.title.trim() == label)
|
||||||
|
.unwrap()
|
||||||
|
.id;
|
||||||
|
|
||||||
|
self.delete_request(&format!("/tasks/{task_id}/labels/{label_id}"));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn label_task(&self, label: &str, task_id: isize) {
|
||||||
|
let labels = self.get_all_labels();
|
||||||
|
|
||||||
|
let label_id = labels
|
||||||
|
.into_iter()
|
||||||
|
.find(|x| x.title.trim() == label)
|
||||||
|
.unwrap()
|
||||||
|
.id;
|
||||||
|
|
||||||
|
self.put_request(
|
||||||
|
&format!("/tasks/{task_id}/labels"),
|
||||||
|
&serde_json::json!({
|
||||||
|
"label_id": label_id
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// tasks
|
// 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}"));
|
||||||
|
|
17
src/args.rs
17
src/args.rs
|
@ -35,6 +35,14 @@ pub fn get_args() -> clap::ArgMatches {
|
||||||
.subcommand(
|
.subcommand(
|
||||||
command!()
|
command!()
|
||||||
.name("label")
|
.name("label")
|
||||||
|
.about("Add a label to a task")
|
||||||
|
.arg(arg!(-u --undo "Remove label from task").required(false))
|
||||||
|
.arg(arg!([label] "Label").required(true))
|
||||||
|
.arg(arg!([task_id] "Task ID").required(true)),
|
||||||
|
)
|
||||||
|
.subcommand(
|
||||||
|
command!()
|
||||||
|
.name("labels")
|
||||||
.about("Manage labels")
|
.about("Manage labels")
|
||||||
.subcommand(command!().name("ls").about("List all labels"))
|
.subcommand(command!().name("ls").about("List all labels"))
|
||||||
.subcommand(
|
.subcommand(
|
||||||
|
@ -49,8 +57,13 @@ pub fn get_args() -> clap::ArgMatches {
|
||||||
.required(false),
|
.required(false),
|
||||||
)
|
)
|
||||||
.arg(arg!(<title> "Label title").required(true)),
|
.arg(arg!(<title> "Label title").required(true)),
|
||||||
), // todo : label rm
|
)
|
||||||
// todo : label <label> <task>
|
.subcommand(
|
||||||
|
command!()
|
||||||
|
.name("rm")
|
||||||
|
.about("Remove a label")
|
||||||
|
.arg(arg!(<title> "Label title").required(true)),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
.subcommand(
|
.subcommand(
|
||||||
command!()
|
command!()
|
||||||
|
|
22
src/main.rs
22
src/main.rs
|
@ -25,10 +25,15 @@ fn main() {
|
||||||
ui::project::list_projects(&api);
|
ui::project::list_projects(&api);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Some(("label", label_args)) => match label_args.subcommand() {
|
Some(("labels", label_args)) => match label_args.subcommand() {
|
||||||
Some(("ls", _)) => {
|
Some(("ls", _)) => {
|
||||||
ui::print_all_labels(&api);
|
ui::print_all_labels(&api);
|
||||||
}
|
}
|
||||||
|
Some(("rm", rm_label_arg)) => {
|
||||||
|
let title: &String = rm_label_arg.get_one("title").unwrap();
|
||||||
|
|
||||||
|
api.remove_label(title);
|
||||||
|
}
|
||||||
Some(("new", new_label_arg)) => {
|
Some(("new", new_label_arg)) => {
|
||||||
let description: Option<&String> = new_label_arg.get_one("description");
|
let description: Option<&String> = new_label_arg.get_one("description");
|
||||||
let color: Option<&String> = new_label_arg.get_one("color");
|
let color: Option<&String> = new_label_arg.get_one("color");
|
||||||
|
@ -40,10 +45,19 @@ fn main() {
|
||||||
color.map(|x| x.as_str()),
|
color.map(|x| x.as_str()),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {}
|
||||||
// todo : label tasks
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
Some(("label", label_args)) => {
|
||||||
|
let label: &String = label_args.get_one("label").unwrap();
|
||||||
|
let task_id: &String = label_args.get_one("task_id").unwrap();
|
||||||
|
let undo = label_args.get_flag("undo");
|
||||||
|
|
||||||
|
if undo {
|
||||||
|
api.label_task_remove(label, task_id.parse().unwrap());
|
||||||
|
} else {
|
||||||
|
api.label_task(label, task_id.parse().unwrap());
|
||||||
|
}
|
||||||
|
}
|
||||||
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();
|
||||||
|
|
|
@ -100,6 +100,11 @@ pub fn print_task_info(task_id: isize, api: &VikunjaAPI) {
|
||||||
);
|
);
|
||||||
|
|
||||||
println!("Created by {}", task.created_by.username);
|
println!("Created by {}", task.created_by.username);
|
||||||
|
println!(
|
||||||
|
"Created: {} | Updated: {}",
|
||||||
|
time_since(parse_datetime(&task.created).unwrap()),
|
||||||
|
time_since(parse_datetime(&task.updated).unwrap())
|
||||||
|
);
|
||||||
|
|
||||||
if let Some(due_date) = parse_datetime(&task.due_date) {
|
if let Some(due_date) = parse_datetime(&task.due_date) {
|
||||||
// todo : color if overdue
|
// todo : color if overdue
|
||||||
|
@ -118,16 +123,14 @@ pub fn print_task_info(task_id: isize, api: &VikunjaAPI) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(labels) = task.labels {
|
if let Some(labels) = task.labels {
|
||||||
// todo : labels and color
|
print!("Labels: ");
|
||||||
println!("Labels: {}", labels.first().unwrap().title);
|
for label in labels {
|
||||||
|
print_label(&label);
|
||||||
|
print!(" ");
|
||||||
|
}
|
||||||
|
println!();
|
||||||
}
|
}
|
||||||
|
|
||||||
println!(
|
|
||||||
"Created: {} | Updated: {}",
|
|
||||||
time_since(parse_datetime(&task.created).unwrap()),
|
|
||||||
time_since(parse_datetime(&task.updated).unwrap())
|
|
||||||
);
|
|
||||||
|
|
||||||
if task.description != "<p></p>" && !task.description.is_empty() {
|
if task.description != "<p></p>" && !task.description.is_empty() {
|
||||||
println!("---\n{}", task.description);
|
println!("---\n{}", task.description);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue