Skip to content

Commit 24a6f12

Browse files
Tch3renk0vThibault
authored andcommitted
feat: added a due date to task (#11)
- created a simple Datetime selector in the task add method - Set the initial date field to today date - Handled the Timezone offset based on local date - Prepared time implementation (google api doesn't support it yet)
1 parent c74e5e6 commit 24a6f12

4 files changed

Lines changed: 39 additions & 6 deletions

File tree

src/handlers/task_handler.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
use std::process::exit;
2+
13
use crate::models::tasks::Tasks;
24
use crate::printer::{print_error, print_success, print_task_table, print_warning};
35
use crate::service::google_api::GoogleApiClient;
46
use crate::service::google_tasks::ApiTasks;
57
use anyhow;
8+
use chrono::{DateTime, Local};
69
use console::Term;
710
use dialoguer::{theme::ColorfulTheme, Input, Select};
811

@@ -47,7 +50,7 @@ impl TaskManager {
4750
} else {
4851
String::from("needsAction")
4952
};
50-
Tasks::new(None, title, notes.unwrap_or_else(||String::from("")), status)
53+
Tasks::new(None, title, notes.unwrap_or_else(||String::from("")), status, "".to_string())
5154
}
5255

5356
fn create_task_with_prompts (&self, notes: Option<String>, done: bool) -> anyhow::Result<Tasks> {
@@ -65,6 +68,33 @@ impl TaskManager {
6568
)?;
6669

6770
let items = vec!["No", "Yes"];
71+
let add_due = Select::with_theme(&ColorfulTheme::default())
72+
.with_prompt("Add a due date?")
73+
.items(&items)
74+
.default(0)
75+
.interact_on_opt(&Term::stderr())?
76+
.unwrap();
77+
78+
let due : String = if add_due!=1 {"".to_string()} else {
79+
let today = Local::today();
80+
let user_input = Input::with_theme(&ColorfulTheme::default())
81+
.with_prompt("Due date")
82+
// We initialize the field with today's date
83+
.with_initial_text(today.format("%Y-%m-%d").to_string())
84+
.allow_empty(true)
85+
.interact_text()
86+
.unwrap();
87+
// We complete the user's input with the time (not used by google API) and the local timezone offset
88+
match DateTime::parse_from_str(&[user_input,"00:00:00".to_string(),today.offset().to_string()].join(" "), "%Y-%m-%d %H:%M:%S %z") {
89+
Ok(date) => {
90+
date.to_rfc3339_opts(chrono::SecondsFormat::Millis, false)},
91+
Err(_) => {println!("Provided date is not valid, abording..."); exit(1);},
92+
93+
}
94+
};
95+
96+
97+
6898
let completed = if done { 1_usize } else {
6999
Select::with_theme(&ColorfulTheme::default())
70100
.with_prompt("Is the task completed?")
@@ -80,7 +110,7 @@ impl TaskManager {
80110
String::from("needsAction")
81111
};
82112

83-
Ok(Tasks::new(None, title, notes, status))
113+
Ok(Tasks::new(None, title, notes, status, due))
84114
}
85115

86116
pub fn show_task(&self, pos: usize) -> anyhow::Result<()> {

src/models/tasks.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub struct Tasks {
3434
}
3535

3636
impl Tasks {
37-
pub fn new(id: Option<String>, title: String, notes: String, status: String) -> Tasks {
37+
pub fn new(id: Option<String>, title: String, notes: String, status: String, due: String) -> Tasks {
3838
Tasks {
3939
kind: "".to_string(),
4040
id,
@@ -45,7 +45,7 @@ impl Tasks {
4545
position: None,
4646
notes,
4747
status,
48-
due: String::from(""),
48+
due,
4949
}
5050
}
5151

src/printer.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,13 @@ pub fn force_write(action: String) -> anyhow::Result<()> {
7171
}
7272

7373
pub fn print_task_table(tasks: &[Tasks]) {
74-
let mut table = Table::new();
75-
table.set_format(*format::consts::FORMAT_NO_BORDER);
7674
if tasks.is_empty() {
7775
print_success("You have no tasks!".to_string());
7876
return;
7977
}
78+
79+
let mut table = Table::new();
80+
table.set_format(*format::consts::FORMAT_NO_BORDER);
8081
table.add_row(row![cb => "Index", "Title", "Status", "Notes", "Due"]);
8182
let mut order = 1;
8283
for task in tasks {

src/service/google_tasks.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ impl ApiTasks for TasksDatabase {
119119
"".to_string(),
120120
"".to_string(),
121121
"".to_string(),
122+
"".to_string()
122123
))
123124
}
124125
fn fetch_all_tasks(
@@ -161,6 +162,7 @@ impl ApiTasks for TasksDatabase {
161162
"".to_string(),
162163
"".to_string(),
163164
"".to_string(),
165+
"".to_string()
164166
))
165167
}
166168

0 commit comments

Comments
 (0)