From c8e74193b36de09c378f5c31840c2dc45f04d62f Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 4 Feb 2019 00:01:57 +0100 Subject: 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 --- bin/domain/imag-contact/src/main.rs | 92 +++++++++++++++++++------------------ 1 file 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 = 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 = 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::>(); } } -- cgit v1.2.3