Add flag and check whether to print empty table

This commit is contained in:
Matthias Beyer 2017-12-06 20:27:49 +01:00
parent b1cf058dcc
commit 13768322d2

View file

@ -34,6 +34,7 @@ pub struct TableLister<F: Fn(&FileLockEntry) -> Vec<String>> {
header: Option<Vec<String>>,
with_idx: bool,
print_empty: bool,
}
impl<F: Fn(&FileLockEntry) -> Vec<String>> TableLister<F> {
@ -43,6 +44,7 @@ impl<F: Fn(&FileLockEntry) -> Vec<String>> TableLister<F> {
line_generator: gen,
header: None,
with_idx: true,
print_empty: false,
}
}
@ -56,6 +58,11 @@ impl<F: Fn(&FileLockEntry) -> Vec<String>> TableLister<F> {
self
}
pub fn print_empty(mut self, b: bool) -> TableLister<F> {
self.print_empty = b;
self
}
}
impl<F: Fn(&FileLockEntry) -> Vec<String>> Lister for TableLister<F> {
@ -81,6 +88,8 @@ impl<F: Fn(&FileLockEntry) -> Vec<String>> Lister for TableLister<F> {
},
}
let mut entries_added = 0;
entries.enumerate().fold(Ok(table), |table, (i, entry)| {
table.and_then(|mut table| {
let mut v = (self.line_generator)(&entry);
@ -102,12 +111,17 @@ impl<F: Fn(&FileLockEntry) -> Vec<String>> Lister for TableLister<F> {
}
table.add_row(v.iter().map(|s| Cell::new(s)).collect());
entries_added += 1;
Ok(table)
})
})
.and_then(|tbl| {
if entries_added != 0 && !self.print_empty {
let mut io = stdout();
tbl.print(&mut io).chain_err(|| LEK::IOError)
} else {
Ok(())
}
})
}