track message and different date options

This commit is contained in:
aaron-jack-manning 2022-08-30 18:23:23 +10:00
parent ab12d393a1
commit c18d47298a
2 changed files with 19 additions and 5 deletions

View File

@ -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<chrono::NaiveDate>,
/// Message to identify the time entry.
#[clap(short, long)]
message : Option<String>,
},
/// 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()?;
},

View File

@ -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<String>,
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<chrono::NaiveDate>, message : Option<String>) -> 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,