diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2019-02-04 00:01:57 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2019-02-11 03:22:56 +0100 |
commit | c8e74193b36de09c378f5c31840c2dc45f04d62f (patch) | |
tree | 76250f3698336b08928691503fbca1ddfb5fd429 | |
parent | f1a639ea8ca400db5de0864d76a8c2f374bc2bb4 (diff) |
Optimize: Do not attempt to print if output is a pipe
This is a small optimization so that we do not print the information if
the output is a pipe anyways.
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | bin/domain/imag-contact/src/main.rs | 92 |
1 files changed, 48 insertions, 44 deletions
diff --git a/bin/domain/imag-contact/src/main.rs b/bin/domain/imag-contact/src/main.rs index a68ee6a6..e97bd9bb 100644 --- a/bin/domain/imag-contact/src/main.rs +++ b/bin/domain/imag-contact/src/main.rs @@ -288,52 +288,56 @@ fn find(rt: &Runtime) { }) .enumerate(); - if scmd.is_present("json") { - let v : Vec<DeserVcard> = iterator.map(|(_, tlp)| tlp.1).collect(); - - match ::serde_json::to_string(&v) { - Ok(s) => writeln!(rt.stdout(), "{}", s).to_exit_code().unwrap_or_exit(), - Err(e) => { - error!("Error generating JSON: {:?}", e); - ::std::process::exit(1) + if !rt.output_is_pipe() { + if scmd.is_present("json") { + let v : Vec<DeserVcard> = iterator.map(|(_, tlp)| tlp.1).collect(); + + match ::serde_json::to_string(&v) { + Ok(s) => writeln!(rt.stdout(), "{}", s).to_exit_code().unwrap_or_exit(), + Err(e) => { + error!("Error generating JSON: {:?}", e); + ::std::process::exit(1) + } } - } - } else if scmd.is_present("find-id") { - iterator - .for_each(|(_i, (entry, _))| { - writeln!(rt.stdout(), "{}", entry.get_location()) - .to_exit_code() - .unwrap_or_exit(); - }) - } else if scmd.is_present("find-full-id") { - let storepath = rt.store().path().display(); - iterator - .for_each(|(_i, (entry, _))| { - writeln!(rt.stdout(), "{}/{}", storepath, entry.get_location()) - .to_exit_code() - .unwrap_or_exit(); - }) - } else { - iterator - .for_each(|(i, (_, card))| { - let fmt = if scmd.is_present("find-show") { - &show_format - } else if scmd.is_present("find-list") { - &list_format - } else { // default: find-list - &list_format - }; - - let data = build_data_object_for_handlebars(i, &card); - let s = fmt - .render("format", &data) - .map_err(Error::from) - .map_err_trace_exit_unwrap(1); + } else if scmd.is_present("find-id") { + iterator + .for_each(|(_i, (entry, _))| { + writeln!(rt.stdout(), "{}", entry.get_location()) + .to_exit_code() + .unwrap_or_exit(); + }) + } else if scmd.is_present("find-full-id") { + let storepath = rt.store().path().display(); + iterator + .for_each(|(_i, (entry, _))| { + writeln!(rt.stdout(), "{}/{}", storepath, entry.get_location()) + .to_exit_code() + .unwrap_or_exit(); + }) + } else { + iterator + .for_each(|(i, (_, card))| { + let fmt = if scmd.is_present("find-show") { + &show_format + } else if scmd.is_present("find-list") { + &list_format + } else { // default: find-list + &list_format + }; + + let data = build_data_object_for_handlebars(i, &card); + let s = fmt + .render("format", &data) + .map_err(Error::from) + .map_err_trace_exit_unwrap(1); - let _ = writeln!(rt.stdout(), "{}", s) - .to_exit_code() - .unwrap_or_exit(); - }); + let _ = writeln!(rt.stdout(), "{}", s) + .to_exit_code() + .unwrap_or_exit(); + }); + } + } else { // if not printing, we still have to consume the iterator to report the touched IDs + let _ = iterator.collect::<Vec<_>>(); } } |