1+ use std:: process:: exit;
2+
13use crate :: models:: tasks:: Tasks ;
24use crate :: printer:: { print_error, print_success, print_task_table, print_warning} ;
35use crate :: service:: google_api:: GoogleApiClient ;
46use crate :: service:: google_tasks:: ApiTasks ;
57use anyhow;
8+ use chrono:: { DateTime , Local } ;
69use console:: Term ;
710use 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 < ( ) > {
0 commit comments