From 57f3894f7616c7e5bc356ea76481a8e771e96e7e Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 2 Aug 2016 16:27:53 +0200 Subject: [PATCH 1/4] Add dependency: prettytable = 0.6.* --- libimagentrylist/Cargo.toml | 1 + libimagentrylist/src/lib.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/libimagentrylist/Cargo.toml b/libimagentrylist/Cargo.toml index 97c099c9..b99f113b 100644 --- a/libimagentrylist/Cargo.toml +++ b/libimagentrylist/Cargo.toml @@ -7,6 +7,7 @@ authors = ["Matthias Beyer "] clap = "2.1.1" log = "0.3" toml = "0.1.25" +prettytable-rs = "0.6.*" [dependencies.libimagstore] path = "../libimagstore" diff --git a/libimagentrylist/src/lib.rs b/libimagentrylist/src/lib.rs index 14bf910c..8c6b8869 100644 --- a/libimagentrylist/src/lib.rs +++ b/libimagentrylist/src/lib.rs @@ -17,6 +17,7 @@ extern crate clap; #[macro_use] extern crate log; extern crate toml; +extern crate prettytable; extern crate libimagstore; extern crate libimagutil; From a9f0ee090306b5560a2b208dcef4df411e31240d Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 2 Aug 2016 16:28:11 +0200 Subject: [PATCH 2/4] Add error type: IOError --- libimagentrylist/src/error.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/libimagentrylist/src/error.rs b/libimagentrylist/src/error.rs index 2cd5cde5..ea454909 100644 --- a/libimagentrylist/src/error.rs +++ b/libimagentrylist/src/error.rs @@ -1,5 +1,6 @@ generate_error_module!( generate_error_types!(ListError, ListErrorKind, + IOError => "IO Error", FormatError => "FormatError", EntryError => "EntryError", IterationError => "IterationError", From e88082f270384a925df673c93bef17bd088f4e14 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 2 Aug 2016 16:28:21 +0200 Subject: [PATCH 3/4] Export MapErrInto trait here as well --- libimagentrylist/src/error.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/libimagentrylist/src/error.rs b/libimagentrylist/src/error.rs index ea454909..32e19f43 100644 --- a/libimagentrylist/src/error.rs +++ b/libimagentrylist/src/error.rs @@ -10,4 +10,5 @@ generate_error_module!( pub use self::error::ListError; pub use self::error::ListErrorKind; +pub use self::error::MapErrInto; From 9fd65dbe4a36d390bf55f51d80b5277d34e9a5e1 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 2 Aug 2016 16:28:32 +0200 Subject: [PATCH 4/4] Add TableLister --- libimagentrylist/src/listers/mod.rs | 1 + libimagentrylist/src/listers/table.rs | 91 +++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 libimagentrylist/src/listers/table.rs diff --git a/libimagentrylist/src/listers/mod.rs b/libimagentrylist/src/listers/mod.rs index d4e06f93..4c43b657 100644 --- a/libimagentrylist/src/listers/mod.rs +++ b/libimagentrylist/src/listers/mod.rs @@ -1,3 +1,4 @@ pub mod core; pub mod line; pub mod path; +pub mod table; diff --git a/libimagentrylist/src/listers/table.rs b/libimagentrylist/src/listers/table.rs new file mode 100644 index 00000000..231f708d --- /dev/null +++ b/libimagentrylist/src/listers/table.rs @@ -0,0 +1,91 @@ +use std::io::stdout; + +use lister::Lister; +use result::Result; +use error::MapErrInto; + +use libimagstore::store::FileLockEntry; +use libimagerror::into::IntoError; + +use prettytable::Table; +use prettytable::cell::Cell; +use prettytable::row::Row; + +pub struct TableLister Vec> { + line_generator: F, + header: Option>, + + with_idx: bool, +} + +impl Vec> TableLister { + + pub fn new(gen: F) -> TableLister { + TableLister { + line_generator: gen, + header: None, + with_idx: true, + } + } + + pub fn with_header(mut self, hdr: Vec) -> TableLister { + self.header = Some(hdr); + self + } + + pub fn with_idx(mut self, b: bool) -> TableLister { + self.with_idx = b; + self + } + +} + +impl Vec> Lister for TableLister { + + fn list<'b, I: Iterator>>(&self, entries: I) -> Result<()> { + use error::ListErrorKind as LEK; + + let mut table = Table::new(); + let mut header_len : Option = None; + match self.header { + Some(ref s) => { + debug!("We have a header... preparing"); + let mut cells : Vec = s.iter().map(|s| Cell::new(s)).collect(); + if self.with_idx { + cells.insert(0, Cell::new("#")); + } + table.set_titles(Row::new(cells)); + header_len = Some(s.len()); + }, + None => { + debug!("No header for table found... continuing without"); + }, + } + + entries.fold(Ok(table), |table, entry| { + table.and_then(|mut table| { + let mut v = (self.line_generator)(&entry); + { + let v_len = v.len(); + if header_len.is_none() { + header_len = Some(v_len); + } + if header_len.map(|l| v_len > l).unwrap_or(false) { + return Err(LEK::FormatError.into_error()); + } + while header_len.map(|l| v.len() != l).unwrap_or(false) { + v.push(String::from("")); + } + } + + table.add_row(v.iter().map(|s| Cell::new(s)).collect()); + Ok(table) + }) + }) + .and_then(|tbl| { + let mut io = stdout(); + tbl.print(&mut io).map_err_into(LEK::IOError) + }) + } + +}