From 780dd90c8fc98345a700a5da691e2c30719e2af6 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 18 Apr 2018 14:47:06 +0200 Subject: [PATCH] Rewrite libimagentryview interface In the previous versions, the sink (where the entries should be written to) was not passed. This did conflict with the libimagrt holding the stdout/stderr handles, because it automatically writes to stdout (which we don't want to do in some cases). Passing the sink is way nicer. This patch changes libimagentryview so that the sink is passed to the viewer. --- .../libimagentryview/src/builtin/editor.rs | 6 +++++- lib/entry/libimagentryview/src/builtin/plain.rs | 10 +++++++--- .../libimagentryview/src/builtin/stdout.rs | 17 +++++++++++------ lib/entry/libimagentryview/src/error.rs | 4 ++++ lib/entry/libimagentryview/src/viewer.rs | 14 +++++++++++--- 5 files changed, 38 insertions(+), 13 deletions(-) diff --git a/lib/entry/libimagentryview/src/builtin/editor.rs b/lib/entry/libimagentryview/src/builtin/editor.rs index 25d2992b..c5cd7394 100644 --- a/lib/entry/libimagentryview/src/builtin/editor.rs +++ b/lib/entry/libimagentryview/src/builtin/editor.rs @@ -17,6 +17,8 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // +use std::io::Write; + use libimagstore::store::Entry; use libimagrt::runtime::Runtime; use libimagentryedit::edit::edit_in_tmpfile; @@ -35,7 +37,9 @@ impl<'a> EditorView<'a> { } impl<'a> Viewer for EditorView<'a> { - fn view_entry(&self, e: &Entry) -> Result<()> { + fn view_entry(&self, e: &Entry, _sink: &mut W) -> Result<()> + where W: Write + { let mut entry = e.to_str()?.clone().to_string(); edit_in_tmpfile(self.0, &mut entry).chain_err(|| VEK::ViewError) } diff --git a/lib/entry/libimagentryview/src/builtin/plain.rs b/lib/entry/libimagentryview/src/builtin/plain.rs index 3c170a87..b276ed93 100644 --- a/lib/entry/libimagentryview/src/builtin/plain.rs +++ b/lib/entry/libimagentryview/src/builtin/plain.rs @@ -17,6 +17,8 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // +use std::io::Write; + use libimagstore::store::Entry; use viewer::Viewer; @@ -38,11 +40,13 @@ impl PlainViewer { impl Viewer for PlainViewer { - fn view_entry(&self, e: &Entry) -> Result<()> { + fn view_entry(&self, e: &Entry, sink: &mut W) -> Result<()> + where W: Write + { if self.show_header { - println!("{}", e.get_header()); + let _ = writeln!(sink, "{}", e.get_header())?; } - println!("{}", e.get_content()); + let _ = writeln!(sink, "{}", e.get_content())?; Ok(()) } diff --git a/lib/entry/libimagentryview/src/builtin/stdout.rs b/lib/entry/libimagentryview/src/builtin/stdout.rs index 5a270745..f74f6b14 100644 --- a/lib/entry/libimagentryview/src/builtin/stdout.rs +++ b/lib/entry/libimagentryview/src/builtin/stdout.rs @@ -17,6 +17,8 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // +use std::io::Write; + use libimagstore::store::Entry; use toml::ser::to_string; @@ -48,17 +50,20 @@ impl StdoutViewer { impl Viewer for StdoutViewer { - fn view_entry(&self, e: &Entry) -> Result<()> { + fn view_entry(&self, e: &Entry, sink: &mut W) -> Result<()> + where W: Write + { if self.view_header { - println!("{}", to_string(e.get_header()).unwrap_or(String::from("TOML Parser error"))); + let header = to_string(e.get_header()).unwrap_or(String::from("TOML Parser error")); + let _ = writeln!(sink, "{}", header)?; } if self.view_content { match self.wrap_content { - Some(limit) => ::textwrap::wrap(e.get_content(), limit).iter().for_each(|line| { - println!("{}", line) - }), - None => println!("{}", e.get_content()), + Some(limit) => for line in ::textwrap::wrap(e.get_content(), limit).iter() { + let _ = writeln!(sink, "{}", line)?; + }, + None => writeln!(sink, "{}", e.get_content())?, } } diff --git a/lib/entry/libimagentryview/src/error.rs b/lib/entry/libimagentryview/src/error.rs index 7c313f76..e910fd87 100644 --- a/lib/entry/libimagentryview/src/error.rs +++ b/lib/entry/libimagentryview/src/error.rs @@ -22,6 +22,10 @@ error_chain! { ViewError, ViewErrorKind, ResultExt, Result; } + foreign_links { + IO(::std::io::Error); + } + links { StoreError(::libimagstore::error::StoreError, ::libimagstore::error::StoreErrorKind); } diff --git a/lib/entry/libimagentryview/src/viewer.rs b/lib/entry/libimagentryview/src/viewer.rs index a6997f0f..dc0194cd 100644 --- a/lib/entry/libimagentryview/src/viewer.rs +++ b/lib/entry/libimagentryview/src/viewer.rs @@ -17,17 +17,25 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // +use std::io::Write; +use std::ops::Deref; + use libimagstore::store::Entry; use error::Result; pub trait Viewer { - fn view_entry(&self, e: &Entry) -> Result<()>; + fn view_entry(&self, e: &Entry, sink: &mut W) -> Result<()> + where W: Write; - fn view_entries>(&self, entries: I) -> Result<()> { + fn view_entries(&self, entries: I, sink: &mut W) -> Result<()> + where I: Iterator, + E: Deref, + W: Write + { for entry in entries { - if let Err(e) = self.view_entry(&entry) { + if let Err(e) = self.view_entry(entry.deref(), sink) { return Err(e); } }