summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2017-12-07 22:08:47 +0100
committerGitHub <noreply@github.com>2017-12-07 22:08:47 +0100
commit3fa2995c5f9981fcb15d925ea40f172b892972cf (patch)
treedf060c5353034a1520052b1b37cc756f03c82c1d
parent65620d6a0b442d03b860be8a788b2c8b53bad909 (diff)
parent13768322d213f63a59ca2030d4deb81f2795555c (diff)
downloadimag-3fa2995c5f9981fcb15d925ea40f172b892972cf.zip
imag-3fa2995c5f9981fcb15d925ea40f172b892972cf.tar.gz
Merge pull request #1168 from matthiasbeyer/libimagentrylist/print-empty-list
Add flag and check whether to print empty table
-rw-r--r--lib/entry/libimagentrylist/src/listers/table.rs18
1 files changed, 16 insertions, 2 deletions
diff --git a/lib/entry/libimagentrylist/src/listers/table.rs b/lib/entry/libimagentrylist/src/listers/table.rs
index 46585c6..6301e18 100644
--- a/lib/entry/libimagentrylist/src/listers/table.rs
+++ b/lib/entry/libimagentrylist/src/listers/table.rs
@@ -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| {
- let mut io = stdout();
- tbl.print(&mut io).chain_err(|| LEK::IOError)
+ if entries_added != 0 && !self.print_empty {
+ let mut io = stdout();
+ tbl.print(&mut io).chain_err(|| LEK::IOError)
+ } else {
+ Ok(())
+ }
})
}