This commit is contained in:
JMARyA 2024-06-06 09:34:49 +02:00
parent 870ec706b3
commit ada01926ba
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
3 changed files with 44 additions and 29 deletions

View file

@ -28,6 +28,42 @@ pub struct User {
pub updated: String,
}
pub fn get_all_items<F, T>(mut get_page: F) -> Vec<T>
where
F: FnMut(usize) -> Vec<T>,
{
let mut ret = Vec::new();
let mut page = 0;
loop {
let current_page = get_page(page);
if current_page.is_empty() {
break;
}
ret.extend(current_page);
page += 1;
}
ret
}
pub struct ProjectID(pub isize);
impl ProjectID {
pub fn parse(api: &VikunjaAPI, project: &str) -> Option<Self> {
let project = project.trim_start_matches("#");
if let Ok(num) = project.parse() {
Some(Self(num))
} else {
Some(Self(
api.get_all_projects()
.into_iter()
.find(|x| x.title.contains(project))?
.id,
))
}
}
}
pub struct VikunjaAPI {
host: String,
token: String,
@ -72,21 +108,6 @@ impl VikunjaAPI {
found.title
}
pub fn parse_project_id(&self, project: &str) -> Option<isize> {
let project = project.trim_start_matches("#");
if let Ok(num) = project.parse() {
Some(num)
} else {
Some(
self.get_all_projects()
.into_iter()
.find(|x| x.title.contains(project))?
.id,
)
}
}
pub fn get_all_projects(&self) -> Vec<Project> {
let resp = self.get_request("/projects");
serde_json::from_str(&resp).unwrap()
@ -99,17 +120,7 @@ impl VikunjaAPI {
// tasks
pub fn get_all_tasks(&self) -> Vec<Task> {
let mut ret = Vec::new();
let mut page = 0;
loop {
let current_page = self.get_task_page(page);
if current_page.is_empty() {
break;
}
ret.extend(current_page);
page += 1;
}
ret
get_all_items(|x| self.get_task_page(x))
}
pub fn get_task(&self, id: isize) -> Task {

View file

@ -9,12 +9,14 @@ use crossterm::{
pub mod project;
pub mod task;
/// Print `txt` with a custom `color`
pub fn print_color(color: Color, txt: &str) {
stdout().execute(SetForegroundColor(color)).unwrap();
print!("{txt}");
stdout().execute(SetForegroundColor(Color::Reset)).unwrap();
}
/// Convert a HEX Color String into a `Color` struct
fn hex_to_color(hex: &str) -> Result<Color, String> {
let hex = hex.trim_start_matches('#');
@ -29,6 +31,7 @@ fn hex_to_color(hex: &str) -> Result<Color, String> {
Ok(Color::Rgb { r, g, b })
}
/// Parse datetime string
fn parse_datetime(datetime_str: &str) -> Option<DateTime<Utc>> {
if datetime_str == "0001-01-01T00:00:00Z" {
return None;
@ -40,6 +43,7 @@ fn parse_datetime(datetime_str: &str) -> Option<DateTime<Utc>> {
}
}
/// Return a formatted time duration
pub fn time_since(event: DateTime<Utc>) -> String {
let now = Utc::now();
let duration = now.signed_duration_since(event);

View file

@ -1,5 +1,5 @@
use crate::{
api::{Task, VikunjaAPI},
api::{ProjectID, Task, VikunjaAPI},
ui::{parse_datetime, time_since},
};
@ -31,11 +31,11 @@ pub fn print_current_tasks(api: &VikunjaAPI, done: bool, fav: bool, project: Opt
};
if let Some(project) = project {
let p_id = api.parse_project_id(project).unwrap();
let p_id = ProjectID::parse(api, project).unwrap();
selection = selection
.into_iter()
.filter(|x| x.project_id == p_id)
.filter(|x| x.project_id == p_id.0)
.collect();
}