refactor
This commit is contained in:
parent
870ec706b3
commit
ada01926ba
3 changed files with 44 additions and 29 deletions
|
@ -28,6 +28,42 @@ pub struct User {
|
||||||
pub updated: String,
|
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 {
|
pub struct VikunjaAPI {
|
||||||
host: String,
|
host: String,
|
||||||
token: String,
|
token: String,
|
||||||
|
@ -72,21 +108,6 @@ impl VikunjaAPI {
|
||||||
found.title
|
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> {
|
pub fn get_all_projects(&self) -> Vec<Project> {
|
||||||
let resp = self.get_request("/projects");
|
let resp = self.get_request("/projects");
|
||||||
serde_json::from_str(&resp).unwrap()
|
serde_json::from_str(&resp).unwrap()
|
||||||
|
@ -99,17 +120,7 @@ impl VikunjaAPI {
|
||||||
|
|
||||||
// tasks
|
// tasks
|
||||||
pub fn get_all_tasks(&self) -> Vec<Task> {
|
pub fn get_all_tasks(&self) -> Vec<Task> {
|
||||||
let mut ret = Vec::new();
|
get_all_items(|x| self.get_task_page(x))
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_task(&self, id: isize) -> Task {
|
pub fn get_task(&self, id: isize) -> Task {
|
||||||
|
|
|
@ -9,12 +9,14 @@ use crossterm::{
|
||||||
pub mod project;
|
pub mod project;
|
||||||
pub mod task;
|
pub mod task;
|
||||||
|
|
||||||
|
/// Print `txt` with a custom `color`
|
||||||
pub fn print_color(color: Color, txt: &str) {
|
pub fn print_color(color: Color, txt: &str) {
|
||||||
stdout().execute(SetForegroundColor(color)).unwrap();
|
stdout().execute(SetForegroundColor(color)).unwrap();
|
||||||
print!("{txt}");
|
print!("{txt}");
|
||||||
stdout().execute(SetForegroundColor(Color::Reset)).unwrap();
|
stdout().execute(SetForegroundColor(Color::Reset)).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Convert a HEX Color String into a `Color` struct
|
||||||
fn hex_to_color(hex: &str) -> Result<Color, String> {
|
fn hex_to_color(hex: &str) -> Result<Color, String> {
|
||||||
let hex = hex.trim_start_matches('#');
|
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 })
|
Ok(Color::Rgb { r, g, b })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parse datetime string
|
||||||
fn parse_datetime(datetime_str: &str) -> Option<DateTime<Utc>> {
|
fn parse_datetime(datetime_str: &str) -> Option<DateTime<Utc>> {
|
||||||
if datetime_str == "0001-01-01T00:00:00Z" {
|
if datetime_str == "0001-01-01T00:00:00Z" {
|
||||||
return None;
|
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 {
|
pub fn time_since(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);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
api::{Task, VikunjaAPI},
|
api::{ProjectID, Task, VikunjaAPI},
|
||||||
ui::{parse_datetime, time_since},
|
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 {
|
if let Some(project) = project {
|
||||||
let p_id = api.parse_project_id(project).unwrap();
|
let p_id = ProjectID::parse(api, project).unwrap();
|
||||||
|
|
||||||
selection = selection
|
selection = selection
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|x| x.project_id == p_id)
|
.filter(|x| x.project_id == p_id.0)
|
||||||
.collect();
|
.collect();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue