fix todos
This commit is contained in:
parent
4df86eb5c2
commit
78452bdb15
3 changed files with 61 additions and 23 deletions
|
@ -249,6 +249,11 @@ impl VikunjaAPI {
|
||||||
get_all_items(|x| self.get_task_page(x))
|
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 {
|
pub fn get_task(&self, id: isize) -> Task {
|
||||||
let resp = self.get_request(&format!("/tasks/{id}"));
|
let resp = self.get_request(&format!("/tasks/{id}"));
|
||||||
serde_json::from_str(&resp).unwrap()
|
serde_json::from_str(&resp).unwrap()
|
||||||
|
|
|
@ -50,19 +50,37 @@ fn parse_datetime(datetime_str: &str) -> Option<DateTime<Utc>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return a formatted time duration
|
/// 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 now = Utc::now();
|
||||||
let duration = now.signed_duration_since(event);
|
let duration = now.signed_duration_since(event);
|
||||||
|
|
||||||
if duration.num_days() > 0 {
|
if duration.num_seconds() == 0 {
|
||||||
format!("{}d ago", duration.num_days())
|
return "Just now".to_string();
|
||||||
} 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()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
fn print_label(label: &Label) {
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
api::{ProjectID, Task, VikunjaAPI},
|
api::{Project, ProjectID, Task, VikunjaAPI},
|
||||||
ui::{parse_datetime, print_color, print_label, time_since},
|
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));
|
print_color(crossterm::style::Color::Yellow, &format!("({}) ", task.id));
|
||||||
|
|
||||||
if task.is_favorite {
|
if task.is_favorite {
|
||||||
|
@ -12,10 +13,13 @@ fn print_task_oneline(task: &Task, api: &VikunjaAPI) {
|
||||||
|
|
||||||
print_color(crossterm::style::Color::Blue, &task.title);
|
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(
|
print_color(
|
||||||
crossterm::style::Color::DarkRed,
|
hex_to_color(&project.hex_color).unwrap_or(crossterm::style::Color::Reset),
|
||||||
&format!(" [{}]", api.get_project_name_from_id(task.project_id)),
|
&format!(" [{}]", project.title),
|
||||||
);
|
);
|
||||||
|
|
||||||
if task.done {
|
if task.done {
|
||||||
|
@ -40,8 +44,7 @@ pub fn print_current_tasks(
|
||||||
project: Option<&String>,
|
project: Option<&String>,
|
||||||
label: Option<&String>,
|
label: Option<&String>,
|
||||||
) {
|
) {
|
||||||
// todo : improve performance by using filters -> https://vikunja.io/docs/filters/
|
let current_tasks = api.get_latest_tasks();
|
||||||
let current_tasks = api.get_all_tasks();
|
|
||||||
|
|
||||||
let mut selection: Vec<_> = if done {
|
let mut selection: Vec<_> = if done {
|
||||||
current_tasks
|
current_tasks
|
||||||
|
@ -73,8 +76,10 @@ pub fn print_current_tasks(
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let projects = api.get_all_projects();
|
||||||
|
|
||||||
for task in selection {
|
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 {
|
if task.done {
|
||||||
print_color(
|
print_color(
|
||||||
crossterm::style::Color::Green,
|
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 by {}", task.created_by.username);
|
||||||
println!(
|
println!(
|
||||||
"Created: {} | Updated: {}",
|
"Created: {} | Updated: {}",
|
||||||
time_since(parse_datetime(&task.created).unwrap()),
|
time_relative(parse_datetime(&task.created).unwrap()),
|
||||||
time_since(parse_datetime(&task.updated).unwrap())
|
time_relative(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
|
print_color(
|
||||||
println!("Due at {due_date}");
|
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 {
|
if task.priority != 0 {
|
||||||
|
|
Loading…
Add table
Reference in a new issue