From c18d47298ad7c38fed92c31e9b855039b83d920c Mon Sep 17 00:00:00 2001 From: aaron-jack-manning Date: Tue, 30 Aug 2022 18:23:23 +1000 Subject: [PATCH] track message and different date options --- src/main.rs | 10 ++++++++-- src/tasks.rs | 14 +++++++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 58a79e3..c5269a1 100755 --- a/src/main.rs +++ b/src/main.rs @@ -83,6 +83,12 @@ enum Command { hours : u16, #[clap(short='M', default_value_t=0)] minutes : u16, + /// Date for the time entry [default: Today] + #[clap(short, long)] + date : Option, + /// Message to identify the time entry. + #[clap(short, long)] + message : Option, }, /// For statistics about the state of your vault. #[clap(subcommand)] @@ -333,10 +339,10 @@ fn program() -> Result<(), error::Error> { } println!("Updated task {}", colour::text::id(id)); }, - Track { id_or_name, hours, minutes } => { + Track { id_or_name, hours, minutes, date, message } => { let id = state.data.index.lookup(&id_or_name)?; let mut task = tasks::Task::load(id, vault_folder, false)?; - let entry = tasks::TimeEntry::new(hours, minutes); + let entry = tasks::TimeEntry::new(hours, minutes, date, message); task.data.time_entries.push(entry); task.save()?; }, diff --git a/src/tasks.rs b/src/tasks.rs index 5b6733c..256416c 100755 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -46,6 +46,7 @@ impl fmt::Display for Priority { #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] pub struct TimeEntry { pub logged_date : chrono::NaiveDate, + pub message : Option, pub duration : Duration, } @@ -291,7 +292,12 @@ impl Task { let mut total = Duration::zero(); let mut lines = Vec::with_capacity(entries.len()); for entry in &entries { - lines.push(format!(" {} [{}]", entry.duration, entry.logged_date)); + lines.push(format!( + " {} [{}] {}", + entry.duration, + entry.logged_date, + entry.message.as_ref().unwrap_or(&String::new()) + )); total = total + entry.duration; } @@ -658,14 +664,16 @@ impl TimeEntry { .fold(Duration::zero(), |a, d| a + d) } - pub fn new(hours : u16, minutes : u16) -> Self { + /// Creates a new TimeEntry, correctly validating and setting defaults. + pub fn new(hours : u16, minutes : u16, date : Option, message : Option) -> Self { let (hours, minutes) = { (hours + minutes / 60, minutes % 60) }; Self { - logged_date : chrono::Utc::now().naive_local().date(), + logged_date : date.unwrap_or(chrono::Utc::now().naive_local().date()), + message, duration : Duration { hours, minutes,