fix todos

This commit is contained in:
JMARyA 2024-06-06 16:08:47 +02:00
parent 4df86eb5c2
commit 78452bdb15
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
3 changed files with 61 additions and 23 deletions

View file

@ -249,6 +249,11 @@ impl VikunjaAPI {
get_all_items(|x| self.get_task_page(x))
}
pub fn get_latest_tasks(&self) -> Vec<Task> {
let resp = self.get_request("/tasks/all?per_page=60&sort_by=created&order_by=desc");
serde_json::from_str(&resp).unwrap()
}
pub fn get_task(&self, id: isize) -> Task {
let resp = self.get_request(&format!("/tasks/{id}"));
serde_json::from_str(&resp).unwrap()

View file

@ -50,19 +50,37 @@ fn parse_datetime(datetime_str: &str) -> Option<DateTime<Utc>> {
}
/// Return a formatted time duration
pub fn time_since(event: DateTime<Utc>) -> String {
pub fn time_relative(event: DateTime<Utc>) -> String {
let now = Utc::now();
let duration = now.signed_duration_since(event);
if duration.num_days() > 0 {
format!("{}d ago", duration.num_days())
} else if duration.num_hours() > 0 {
format!("{}h ago", duration.num_hours())
} else if duration.num_minutes() > 0 {
format!("{}m ago", duration.num_minutes())
} else {
"Just now".to_string()
if duration.num_seconds() == 0 {
return "Just now".to_string();
}
let is_past = duration.num_seconds() > 0;
let abs_duration = if is_past { duration } else { -duration };
let time_string = if abs_duration.num_days() > 0 {
format!("{}d", abs_duration.num_days())
} else if abs_duration.num_hours() > 0 {
format!("{}h", abs_duration.num_hours())
} else if abs_duration.num_minutes() > 0 {
format!("{}m", abs_duration.num_minutes())
} else {
format!("{}s", abs_duration.num_seconds())
};
if is_past {
format!("{} ago", time_string)
} else {
format!("in {}", time_string)
}
}
fn is_in_past(dt: DateTime<Utc>) -> bool {
let now = Utc::now();
dt < now
}
fn print_label(label: &Label) {

View file

@ -1,9 +1,10 @@
use crate::{
api::{ProjectID, Task, VikunjaAPI},
ui::{parse_datetime, print_color, print_label, time_since},
api::{Project, ProjectID, Task, VikunjaAPI},
ui::{hex_to_color, is_in_past, parse_datetime, print_color, print_label, time_relative},
};
fn print_task_oneline(task: &Task, api: &VikunjaAPI) {
// todo : move to grid view
fn print_task_oneline(task: &Task, projects: &[Project]) {
print_color(crossterm::style::Color::Yellow, &format!("({}) ", task.id));
if task.is_favorite {
@ -12,10 +13,13 @@ fn print_task_oneline(task: &Task, api: &VikunjaAPI) {
print_color(crossterm::style::Color::Blue, &task.title);
// todo : colors based on project colors
let project = projects
.into_iter()
.find(|x| x.id == task.project_id)
.unwrap();
print_color(
crossterm::style::Color::DarkRed,
&format!(" [{}]", api.get_project_name_from_id(task.project_id)),
hex_to_color(&project.hex_color).unwrap_or(crossterm::style::Color::Reset),
&format!(" [{}]", project.title),
);
if task.done {
@ -40,8 +44,7 @@ pub fn print_current_tasks(
project: Option<&String>,
label: Option<&String>,
) {
// todo : improve performance by using filters -> https://vikunja.io/docs/filters/
let current_tasks = api.get_all_tasks();
let current_tasks = api.get_latest_tasks();
let mut selection: Vec<_> = if done {
current_tasks
@ -73,8 +76,10 @@ pub fn print_current_tasks(
});
}
let projects = api.get_all_projects();
for task in selection {
print_task_oneline(&task, api);
print_task_oneline(&task, &projects);
}
}
@ -84,7 +89,10 @@ pub fn print_task_info(task_id: isize, api: &VikunjaAPI) {
if task.done {
print_color(
crossterm::style::Color::Green,
&format!("{}", time_since(parse_datetime(&task.done_at).unwrap())),
&format!(
"{} ✓ ",
time_relative(parse_datetime(&task.done_at).unwrap())
),
);
}
@ -102,13 +110,20 @@ pub fn print_task_info(task_id: isize, api: &VikunjaAPI) {
println!("Created by {}", task.created_by.username);
println!(
"Created: {} | Updated: {}",
time_since(parse_datetime(&task.created).unwrap()),
time_since(parse_datetime(&task.updated).unwrap())
time_relative(parse_datetime(&task.created).unwrap()),
time_relative(parse_datetime(&task.updated).unwrap())
);
if let Some(due_date) = parse_datetime(&task.due_date) {
// todo : color if overdue
println!("Due at {due_date}");
print_color(
if is_in_past(due_date) {
crossterm::style::Color::Red
} else {
crossterm::style::Color::Reset
},
&format!("Due {}", time_relative(due_date)),
);
println!();
}
if task.priority != 0 {