Merge pull request #1418 from matthiasbeyer/libimagentryview/pass-sink

Rewrite libimagentryview interface
This commit is contained in:
Matthias Beyer 2018-04-24 18:00:57 +02:00 committed by GitHub
commit f882f6eeaf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 78 additions and 29 deletions

View file

@ -185,6 +185,9 @@ fn main() {
viewer.wrap_at(width); viewer.wrap_at(width);
} }
let output = rt.stdout();
let mut lockout = output.lock();
entry_ids entry_ids
.into_iter() .into_iter()
.into_get_iter(rt.store()) .into_get_iter(rt.store())
@ -195,7 +198,7 @@ fn main() {
.map_err_trace_exit_unwrap(1) .map_err_trace_exit_unwrap(1)
}) })
.for_each(|e| { .for_each(|e| {
viewer.view_entry(&e).map_err_trace_exit_unwrap(1); viewer.view_entry(&e, &mut lockout).map_err_trace_exit_unwrap(1);
}); });
} }
} }

View file

@ -33,6 +33,7 @@ libimagstore = { version = "0.8.0", path = "../../../lib/core/libimagstore
libimagrt = { version = "0.8.0", path = "../../../lib/core/libimagrt" } libimagrt = { version = "0.8.0", path = "../../../lib/core/libimagrt" }
libimagdiary = { version = "0.8.0", path = "../../../lib/domain/libimagdiary" } libimagdiary = { version = "0.8.0", path = "../../../lib/domain/libimagdiary" }
libimagentryedit = { version = "0.8.0", path = "../../../lib/entry/libimagentryedit" } libimagentryedit = { version = "0.8.0", path = "../../../lib/entry/libimagentryedit" }
libimagentryview = { version = "0.8.0", path = "../../../lib/entry/libimagentryview" }
libimaginteraction = { version = "0.8.0", path = "../../../lib/etc/libimaginteraction" } libimaginteraction = { version = "0.8.0", path = "../../../lib/etc/libimaginteraction" }
libimagutil = { version = "0.8.0", path = "../../../lib/etc/libimagutil" } libimagutil = { version = "0.8.0", path = "../../../lib/etc/libimagutil" }
libimagtimeui = { version = "0.8.0", path = "../../../lib/etc/libimagtimeui" } libimagtimeui = { version = "0.8.0", path = "../../../lib/etc/libimagtimeui" }

View file

@ -41,6 +41,7 @@ extern crate itertools;
extern crate libimagdiary; extern crate libimagdiary;
extern crate libimagentryedit; extern crate libimagentryedit;
extern crate libimagentryview;
extern crate libimagerror; extern crate libimagerror;
extern crate libimaginteraction; extern crate libimaginteraction;
#[macro_use] extern crate libimagrt; #[macro_use] extern crate libimagrt;

View file

@ -23,6 +23,7 @@ use libimagrt::runtime::Runtime;
use libimagerror::trace::MapErrTrace; use libimagerror::trace::MapErrTrace;
use libimagutil::warn_exit::warn_exit; use libimagutil::warn_exit::warn_exit;
use libimagstore::iter::get::StoreIdGetIteratorExtension; use libimagstore::iter::get::StoreIdGetIteratorExtension;
use libimagentryview::viewer::Viewer;
use util::get_diary_name; use util::get_diary_name;
@ -39,7 +40,8 @@ pub fn view(rt: &Runtime) {
::std::process::exit(1) ::std::process::exit(1)
})); }));
DV::new(hdr).view_entries(entries) let out = rt.stdout();
DV::new(hdr).view_entries(entries, &mut out.lock())
.map_err_trace_exit_unwrap(1); .map_err_trace_exit_unwrap(1);
} }

View file

@ -22,6 +22,10 @@ error_chain! {
DiaryError, DiaryErrorKind, ResultExt, Result; DiaryError, DiaryErrorKind, ResultExt, Result;
} }
foreign_links {
Io(::std::io::Error);
}
links { links {
StoreError(::libimagstore::error::StoreError, ::libimagstore::error::StoreErrorKind); StoreError(::libimagstore::error::StoreError, ::libimagstore::error::StoreErrorKind);
EntryUtilError(::libimagentryutil::error::EntryUtilError, ::libimagentryutil::error::EntryUtilErrorKind); EntryUtilError(::libimagentryutil::error::EntryUtilError, ::libimagentryutil::error::EntryUtilErrorKind);

View file

@ -19,14 +19,16 @@
//! A diary viewer built on libimagentryview. //! A diary viewer built on libimagentryview.
use entry::DiaryEntry; use std::io::Write;
use error::DiaryErrorKind as DEK; use std::ops::Deref;
use error::ResultExt;
use error::Result;
use libimagstore::store::FileLockEntry; use libimagstore::store::Entry;
use libimagentryview::viewer::Viewer; use libimagentryview::viewer::Viewer;
use libimagentryview::error::ViewErrorKind as VEK;
use libimagentryview::error::ResultExt;
use libimagentryview::error::Result as ViewResult;
use libimagentryview::builtin::plain::PlainViewer; use libimagentryview::builtin::plain::PlainViewer;
use entry::DiaryEntry;
/// This viewer does _not_ implement libimagentryview::viewer::Viewer because we need to be able to /// This viewer does _not_ implement libimagentryview::viewer::Viewer because we need to be able to
/// call some diary-type specific functions on the entries passed to this. /// call some diary-type specific functions on the entries passed to this.
@ -45,24 +47,35 @@ impl DiaryViewer {
DiaryViewer(PlainViewer::new(show_header)) DiaryViewer(PlainViewer::new(show_header))
} }
}
impl Viewer for DiaryViewer {
fn view_entry<W>(&self, e: &Entry, sink: &mut W) -> ViewResult<()>
where W: Write
{
self.0.view_entry(e, sink)
}
/// View all entries from the iterator, or stop immediately if an error occurs, returning that /// View all entries from the iterator, or stop immediately if an error occurs, returning that
/// error. /// error.
pub fn view_entries<'a, I: Iterator<Item = FileLockEntry<'a>>>(&self, entries: I) -> Result<()> { fn view_entries<I, E, W>(&self, entries: I, sink: &mut W) -> ViewResult<()>
where I: Iterator<Item = E>,
E: Deref<Target = Entry>,
W: Write
{
let mut entries = entries let mut entries = entries
.map(|e| e.diary_id().map(|id| (id, e))) .map(|e| e.deref().diary_id().map(|id| (id, e)).chain_err(|| VEK::ViewError))
.collect::<Result<Vec<_>>>()?; .collect::<ViewResult<Vec<_>>>()?;
entries.sort_by_key(|&(ref id, _)| { entries.sort_by_key(|&(ref id, _)| {
[id.year() as u32, id.month(), id.day(), id.hour(), id.minute(), id.second()] [id.year() as u32, id.month(), id.day(), id.hour(), id.minute(), id.second()]
}); });
for (id, entry) in entries.into_iter() { for (id, entry) in entries.into_iter() {
println!("{} :\n", id); writeln!(sink, "{} :\n", id)?;
let _ = self.0 let _ = self.0.view_entry(entry.deref(), sink)?;
.view_entry(&entry) writeln!(sink, "\n---\n")?;
.chain_err(|| DEK::ViewError)
.chain_err(|| DEK::IOError)?;
println!("\n---\n");
} }
Ok(()) Ok(())

View file

@ -17,6 +17,8 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// //
use std::io::Write;
use libimagstore::store::Entry; use libimagstore::store::Entry;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagentryedit::edit::edit_in_tmpfile; use libimagentryedit::edit::edit_in_tmpfile;
@ -35,7 +37,9 @@ impl<'a> EditorView<'a> {
} }
impl<'a> Viewer for EditorView<'a> { impl<'a> Viewer for EditorView<'a> {
fn view_entry(&self, e: &Entry) -> Result<()> { fn view_entry<W>(&self, e: &Entry, _sink: &mut W) -> Result<()>
where W: Write
{
let mut entry = e.to_str()?.clone().to_string(); let mut entry = e.to_str()?.clone().to_string();
edit_in_tmpfile(self.0, &mut entry).chain_err(|| VEK::ViewError) edit_in_tmpfile(self.0, &mut entry).chain_err(|| VEK::ViewError)
} }

View file

@ -17,6 +17,8 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// //
use std::io::Write;
use libimagstore::store::Entry; use libimagstore::store::Entry;
use viewer::Viewer; use viewer::Viewer;
@ -38,11 +40,13 @@ impl PlainViewer {
impl Viewer for PlainViewer { impl Viewer for PlainViewer {
fn view_entry(&self, e: &Entry) -> Result<()> { fn view_entry<W>(&self, e: &Entry, sink: &mut W) -> Result<()>
where W: Write
{
if self.show_header { 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(()) Ok(())
} }

View file

@ -17,6 +17,8 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// //
use std::io::Write;
use libimagstore::store::Entry; use libimagstore::store::Entry;
use toml::ser::to_string; use toml::ser::to_string;
@ -48,17 +50,20 @@ impl StdoutViewer {
impl Viewer for StdoutViewer { impl Viewer for StdoutViewer {
fn view_entry(&self, e: &Entry) -> Result<()> { fn view_entry<W>(&self, e: &Entry, sink: &mut W) -> Result<()>
where W: Write
{
if self.view_header { 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 { if self.view_content {
match self.wrap_content { match self.wrap_content {
Some(limit) => ::textwrap::wrap(e.get_content(), limit).iter().for_each(|line| { Some(limit) => for line in ::textwrap::wrap(e.get_content(), limit).iter() {
println!("{}", line) let _ = writeln!(sink, "{}", line)?;
}), },
None => println!("{}", e.get_content()), None => writeln!(sink, "{}", e.get_content())?,
} }
} }

View file

@ -22,6 +22,10 @@ error_chain! {
ViewError, ViewErrorKind, ResultExt, Result; ViewError, ViewErrorKind, ResultExt, Result;
} }
foreign_links {
IO(::std::io::Error);
}
links { links {
StoreError(::libimagstore::error::StoreError, ::libimagstore::error::StoreErrorKind); StoreError(::libimagstore::error::StoreError, ::libimagstore::error::StoreErrorKind);
} }

View file

@ -17,17 +17,25 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // 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 libimagstore::store::Entry;
use error::Result; use error::Result;
pub trait Viewer { pub trait Viewer {
fn view_entry(&self, e: &Entry) -> Result<()>; fn view_entry<W>(&self, e: &Entry, sink: &mut W) -> Result<()>
where W: Write;
fn view_entries<I: Iterator<Item = Entry>>(&self, entries: I) -> Result<()> { fn view_entries<I, E, W>(&self, entries: I, sink: &mut W) -> Result<()>
where I: Iterator<Item = E>,
E: Deref<Target = Entry>,
W: Write
{
for entry in entries { 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); return Err(e);
} }
} }