diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2016-06-08 13:58:16 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2016-06-09 15:41:42 +0200 |
commit | d8071a56246493e012629358afa7159949840e98 (patch) | |
tree | 9550d91ce71de57d74087fbf7b4a3f5e5bc2e00a /imag-diary | |
parent | 0f32957ce84ebd701023b6940a4c0ad77d3f6ac4 (diff) |
Add functionality so one can create diary entries hourly and minutely
Diffstat (limited to 'imag-diary')
-rw-r--r-- | imag-diary/src/main.rs | 72 |
1 files changed, 71 insertions, 1 deletions
diff --git a/imag-diary/src/main.rs b/imag-diary/src/main.rs index a3942051..b696f552 100644 --- a/imag-diary/src/main.rs +++ b/imag-diary/src/main.rs @@ -67,6 +67,9 @@ fn main() { } fn create(rt: &Runtime) { + use libimagdiary::entry::Entry; + use libimagdiary::result::Result; + let diaryname = get_diary_name(rt); if diaryname.is_none() { warn!("No diary selected. Use either the configuration file or the commandline option"); @@ -76,8 +79,75 @@ fn create(rt: &Runtime) { let prevent_edit = rt.cli().subcommand_matches("create").unwrap().is_present("no-edit"); + fn create_entry<'a>(diary: &'a Diary, rt: &Runtime) -> Result<Entry<'a>> { + use std::str::FromStr; + + let create = rt.cli().subcommand_matches("create").unwrap(); + if !create.is_present("timed") { + debug!("Creating non-timed entry"); + diary.new_entry_today() + } else { + let id = match create.value_of("timed") { + Some("h") | Some("hourly") => { + debug!("Creating hourly-timed entry"); + let mut time = DiaryId::now(String::from(diary.name())); + let hr = create + .value_of("hour") + .map(|v| { debug!("Creating hourly entry with hour = {:?}", v); v }) + .and_then(|s| { + FromStr::from_str(s) + .map_err(|_| warn!("Could not parse hour: '{}'", s)) + .ok() + }) + .unwrap_or(time.hour()); + + time.with_hour(hr).with_minute(0) + }, + + Some("m") | Some("minutely") => { + debug!("Creating minutely-timed entry"); + let mut time = DiaryId::now(String::from(diary.name())); + let hr = create + .value_of("hour") + .map(|h| { debug!("hour = {:?}", h); h }) + .and_then(|s| { + FromStr::from_str(s) + .map_err(|_| warn!("Could not parse hour: '{}'", s)) + .ok() + }) + .unwrap_or(time.hour()); + + let min = create + .value_of("minute") + .map(|m| { debug!("minute = {:?}", m); m }) + .and_then(|s| { + FromStr::from_str(s) + .map_err(|_| warn!("Could not parse minute: '{}'", s)) + .ok() + }) + .unwrap_or(time.minute()); + + time.with_hour(hr).with_minute(min) + }, + + Some(_) => { + warn!("Timed creation failed: Unknown spec '{}'", + create.value_of("timed").unwrap()); + exit(1); + }, + + None => { + warn!("Unexpected error, cannot continue"); + exit(1); + }, + }; + + diary.new_entry_by_id(id) + } + } + let diary = Diary::open(rt.store(), &diaryname[..]); - let res = diary.new_entry_today() + let res = create_entry(&diary, rt) .and_then(|mut entry| { if prevent_edit { debug!("Not editing new diary entry"); |