From 88c47a20b888c3d065c8ddcd9fdcc822ddf7c227 Mon Sep 17 00:00:00 2001 From: Thibault Date: Sun, 28 Aug 2022 13:07:59 +0200 Subject: [PATCH] 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) --- src/handlers/task_handler.rs | 34 ++++++++++++++++++++++++++++++++-- src/models/tasks.rs | 4 ++-- src/printer.rs | 5 +++-- src/service/google_tasks.rs | 2 ++ 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/handlers/task_handler.rs b/src/handlers/task_handler.rs index 4344a0b..60f9d91 100644 --- a/src/handlers/task_handler.rs +++ b/src/handlers/task_handler.rs @@ -1,8 +1,11 @@ +use std::process::exit; + use crate::models::tasks::Tasks; use crate::printer::{print_error, print_success, print_task_table, print_warning}; use crate::service::google_api::GoogleApiClient; use crate::service::google_tasks::ApiTasks; use anyhow; +use chrono::{DateTime, Local}; use console::Term; use dialoguer::{theme::ColorfulTheme, Input, Select}; @@ -47,7 +50,7 @@ impl TaskManager { } else { String::from("needsAction") }; - Tasks::new(None, title, notes.unwrap_or_else(||String::from("")), status) + Tasks::new(None, title, notes.unwrap_or_else(||String::from("")), status, "".to_string()) } fn create_task_with_prompts (&self, notes: Option, done: bool) -> anyhow::Result { @@ -65,6 +68,33 @@ impl TaskManager { )?; let items = vec!["No", "Yes"]; + let add_due = Select::with_theme(&ColorfulTheme::default()) + .with_prompt("Add a due date?") + .items(&items) + .default(0) + .interact_on_opt(&Term::stderr())? + .unwrap(); + + let due : String = if add_due!=1 {"".to_string()} else { + let today = Local::today(); + let user_input = Input::with_theme(&ColorfulTheme::default()) + .with_prompt("Due date") + // We initialize the field with today's date + .with_initial_text(today.format("%Y-%m-%d").to_string()) + .allow_empty(true) + .interact_text() + .unwrap(); + // We complete the user's input with the time (not used by google API) and the local timezone offset + match DateTime::parse_from_str(&[user_input,"00:00:00".to_string(),today.offset().to_string()].join(" "), "%Y-%m-%d %H:%M:%S %z") { + Ok(date) => { + date.to_rfc3339_opts(chrono::SecondsFormat::Millis, false)}, + Err(_) => {println!("Provided date is not valid, abording..."); exit(1);}, + + } + }; + + + let completed = if done { 1_usize } else { Select::with_theme(&ColorfulTheme::default()) .with_prompt("Is the task completed?") @@ -80,7 +110,7 @@ impl TaskManager { String::from("needsAction") }; - Ok(Tasks::new(None, title, notes, status)) + Ok(Tasks::new(None, title, notes, status, due)) } pub fn show_task(&self, pos: usize) -> anyhow::Result<()> { diff --git a/src/models/tasks.rs b/src/models/tasks.rs index 5abf27c..8fe5bb7 100644 --- a/src/models/tasks.rs +++ b/src/models/tasks.rs @@ -34,7 +34,7 @@ pub struct Tasks { } impl Tasks { - pub fn new(id: Option, title: String, notes: String, status: String) -> Tasks { + pub fn new(id: Option, title: String, notes: String, status: String, due: String) -> Tasks { Tasks { kind: "".to_string(), id, @@ -45,7 +45,7 @@ impl Tasks { position: None, notes, status, - due: String::from(""), + due, } } diff --git a/src/printer.rs b/src/printer.rs index 925d710..3afa9f3 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -71,12 +71,13 @@ pub fn force_write(action: String) -> anyhow::Result<()> { } pub fn print_task_table(tasks: &[Tasks]) { - let mut table = Table::new(); - table.set_format(*format::consts::FORMAT_NO_BORDER); if tasks.is_empty() { print_success("You have no tasks!".to_string()); return; } + + let mut table = Table::new(); + table.set_format(*format::consts::FORMAT_NO_BORDER); table.add_row(row![cb => "Index", "Title", "Status", "Notes", "Due"]); let mut order = 1; for task in tasks { diff --git a/src/service/google_tasks.rs b/src/service/google_tasks.rs index 6da791a..9cdf3e1 100644 --- a/src/service/google_tasks.rs +++ b/src/service/google_tasks.rs @@ -119,6 +119,7 @@ impl ApiTasks for TasksDatabase { "".to_string(), "".to_string(), "".to_string(), + "".to_string() )) } fn fetch_all_tasks( @@ -161,6 +162,7 @@ impl ApiTasks for TasksDatabase { "".to_string(), "".to_string(), "".to_string(), + "".to_string() )) }