Merge pull request #1229 from matthiasbeyer/imag-diary/timed-seconds

imag-diary: Add second granularity support
This commit is contained in:
Matthias Beyer 2018-02-05 16:20:50 +01:00 committed by GitHub
commit 3a54fc220c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 10 deletions

View file

@ -65,14 +65,15 @@ fn create_entry<'a>(diary: &'a Store, diaryname: &str, rt: &Runtime) -> FileLock
let create_timed = create.value_of("timed") let create_timed = create.value_of("timed")
.map(|t| parse_timed_string(t, diaryname).map_err_trace_exit_unwrap(1)) .map(|t| parse_timed_string(t, diaryname).map_err_trace_exit_unwrap(1))
.map(Some) .map(Some)
.unwrap_or_else(|| match get_diary_timed_config(rt, diaryname) { .unwrap_or_else(|| {
Err(e) => trace_error_exit(&e, 1), match get_diary_timed_config(rt, diaryname).map_err_trace_exit_unwrap(1) {
Ok(Some(t)) => Some(t), Some(t) => Some(t),
Ok(None) => { None => {
warn!("Missing config: 'diary.diaries.{}.timed'", diaryname); warn!("Missing config: 'diary.diaries.{}.timed'", diaryname);
warn!("Assuming 'false'"); warn!("Assuming 'false'");
None None
} }
}
}); });
let entry = match create_timed { let entry = match create_timed {
@ -135,6 +136,31 @@ fn create_id_from_clispec(create: &ArgMatches, diaryname: &str, timed_type: Time
time.with_minute(min) time.with_minute(min)
}, },
Timed::Secondly => {
let time = get_hourly_id(create);
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());
let sec = create
.value_of("second")
.map(|s| { debug!("second = {:?}", s); s })
.and_then(|s| {
FromStr::from_str(s)
.map_err(|_| warn!("Could not parse second: '{}'", s))
.ok()
})
.unwrap_or(time.second());
time.with_minute(min).with_second(sec)
},
} }
} }

View file

@ -43,9 +43,7 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.short("t") .short("t")
.takes_value(true) .takes_value(true)
.required(false) .required(false)
.help("By default, one entry is created per day. With --timed=h[ourly] or .help("By default, one entry is created per day. With --timed=h[ourly] or --timed=m[inutely] one can create per-hour and per-minute entries (more like a microblog then"))
--timed=m[inutely] one can create per-hour and per-minute entries (more like
a microblog then"))
.arg(Arg::with_name("hour") .arg(Arg::with_name("hour")
.long("hour") .long("hour")
@ -57,6 +55,11 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.takes_value(true) .takes_value(true)
.required(false) .required(false)
.help("When using --timed, override the minute component")) .help("When using --timed, override the minute component"))
.arg(Arg::with_name("second")
.long("second")
.takes_value(true)
.required(false)
.help("When using --timed, override the second component"))
// When using --hour or --minute, --timed must be present // When using --hour or --minute, --timed must be present
.group(ArgGroup::with_name("timing-hourly") .group(ArgGroup::with_name("timing-hourly")
@ -65,6 +68,9 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.group(ArgGroup::with_name("timing-minutely") .group(ArgGroup::with_name("timing-minutely")
.args(&["minute"]) .args(&["minute"])
.requires("timed")) .requires("timed"))
.group(ArgGroup::with_name("timing-secondly")
.args(&["second"])
.requires("timed"))
) )
.subcommand(SubCommand::with_name("edit") .subcommand(SubCommand::with_name("edit")

View file

@ -33,6 +33,7 @@ pub fn get_diary_name(rt: &Runtime) -> Option<String> {
pub enum Timed { pub enum Timed {
Hourly, Hourly,
Minutely, Minutely,
Secondly,
} }
/// Returns true if the diary should always create timed entries, which is whenever /// Returns true if the diary should always create timed entries, which is whenever
@ -76,6 +77,8 @@ pub fn parse_timed_string(s: &str, diary_name: &str) -> Result<Timed> {
Ok(Timed::Hourly) Ok(Timed::Hourly)
} else if s == "m" || s == "minutely" { } else if s == "m" || s == "minutely" {
Ok(Timed::Minutely) Ok(Timed::Minutely)
} else if s == "s" || s == "secondly" {
Ok(Timed::Secondly)
} else { } else {
let s = format!("Cannot parse config: 'diary.diaries.{}.timed = {}'", let s = format!("Cannot parse config: 'diary.diaries.{}.timed = {}'",
diary_name, s); diary_name, s);

View file

@ -32,6 +32,7 @@ This section contains the changelog from the last release to the next release.
* `libimagentryutil` was introduced, a library for helpers for * `libimagentryutil` was introduced, a library for helpers for
`libimagstore::store::Entry` handling and writing extension-writing. `libimagstore::store::Entry` handling and writing extension-writing.
* `imag-edit` was introduced * `imag-edit` was introduced
* `imag-diary` got second-granularity support in the CLI.
* Minor changes * Minor changes
* Internals were refactored from `match`ing all the things into function * Internals were refactored from `match`ing all the things into function
chaining chaining