summaryrefslogtreecommitdiffstats
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs196
1 files changed, 105 insertions, 91 deletions
diff --git a/src/lib.rs b/src/lib.rs
index c0184bd..f825e23 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,3 +1,7 @@
+#![warn(missing_docs,
+ unused_extern_crates,
+ unused_import_braces,
+ unused_qualifications)]
//! A formatted and aligned table printer written in rust
extern crate unicode_width;
extern crate term;
@@ -83,6 +87,11 @@ impl<'a> TableSlice<'a> {
self.rows.len()
}
+ /// Check if the table slice is empty
+ pub fn is_empty(&self) -> bool {
+ self.rows.is_empty()
+ }
+
/// Get an immutable reference to a row
pub fn get_row(&self, row: usize) -> Option<&Row> {
self.rows.get(row)
@@ -91,9 +100,9 @@ impl<'a> TableSlice<'a> {
/// Get the width of the column at position `col_idx`.
/// Return 0 if the column does not exists;
fn get_column_width(&self, col_idx: usize) -> usize {
- let mut width = match *self.titles {
- Some(ref t) => t.get_cell_width(col_idx),
- None => 0,
+ let mut width = match *self.titles {
+ Some(ref t) => t.get_cell_width(col_idx),
+ None => 0,
};
for r in self.rows {
let l = r.get_cell_width(col_idx);
@@ -134,14 +143,14 @@ impl<'a> TableSlice<'a> {
try!(self.format
.print_line_separator(out, &col_width, LinePosition::Top));
if let Some(ref t) = *self.titles {
- try!(f(t, out, &self.format, &col_width));
+ try!(f(t, out, self.format, &col_width));
try!(self.format
.print_line_separator(out, &col_width, LinePosition::Title));
}
// Print rows
let mut iter = self.rows.into_iter().peekable();
while let Some(r) = iter.next() {
- try!(f(r, out, &self.format, &col_width));
+ try!(f(r, out, self.format, &col_width));
if iter.peek().is_some() {
try!(self.format
.print_line_separator(out, &col_width, LinePosition::Intern));
@@ -171,9 +180,9 @@ impl<'a> TableSlice<'a> {
/// # Panic
/// Panic if writing to standard output fails
pub fn print_tty(&self, force_colorize: bool) {
- let r = match (stdout(), atty::is(atty::Stream::Stdout) || force_colorize) {
- (Some(mut o), true) => self.print_term(&mut *o),
- _ => self.print(&mut io::stdout()),
+ let r = match (stdout(), atty::is(atty::Stream::Stdout) || force_colorize) {
+ (Some(mut o), true) => self.print_term(&mut *o),
+ _ => self.print(&mut io::stdout()),
};
if let Err(e) = r {
panic!("Cannot print table to standard output : {}", e);
@@ -284,6 +293,11 @@ impl Table {
self.rows.len()
}
+ /// Check if the table is empty
+ pub fn is_empty(&self) -> bool {
+ self.rows.is_empty()
+ }
+
/// Set the optional title lines
pub fn set_titles(&mut self, titles: Row) {
*self.titles = Some(titles);
@@ -353,12 +367,12 @@ impl Table {
}
/// Returns an iterator over immutable rows
- pub fn row_iter<'a>(&'a self) -> Iter<'a, Row> {
+ pub fn row_iter(&self) -> Iter<Row> {
self.rows.iter()
}
/// Returns an iterator over mutable rows
- pub fn row_iter_mut<'a>(&'a mut self) -> IterMut<'a, Row> {
+ pub fn row_iter_mut(&mut self) -> IterMut<Row> {
self.rows.iter_mut()
}
@@ -439,7 +453,7 @@ impl fmt::Display for Table {
impl<'a> fmt::Display for TableSlice<'a> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
let mut writer = StringWriter::new();
- if let Err(_) = self.print(&mut writer) {
+ if self.print(&mut writer).is_err() {
return Err(fmt::Error);
}
fmt.write_str(writer.as_string())
@@ -450,7 +464,7 @@ impl<B: ToString, A: IntoIterator<Item = B>> FromIterator<A> for Table {
fn from_iter<T>(iterator: T) -> Table
where T: IntoIterator<Item = A>
{
- Self::init(iterator.into_iter().map(|r| Row::from(r)).collect())
+ Self::init(iterator.into_iter().map(Row::from).collect())
}
}
@@ -481,9 +495,9 @@ impl<'a> IntoIterator for &'a mut Table {
}
/// Iterator over immutable cells in a column
-pub struct ColumnIter<'a>(std::slice::Iter<'a, Row>, usize);
+pub struct ColumnIter<'a>(Iter<'a, Row>, usize);
-impl<'a> std::iter::Iterator for ColumnIter<'a> {
+impl<'a> Iterator for ColumnIter<'a> {
type Item = &'a Cell;
fn next(&mut self) -> Option<&'a Cell> {
self.0.next().and_then(|row| row.get_cell(self.1))
@@ -491,9 +505,9 @@ impl<'a> std::iter::Iterator for ColumnIter<'a> {
}
/// Iterator over mutable cells in a column
-pub struct ColumnIterMut<'a>(std::slice::IterMut<'a, Row>, usize);
+pub struct ColumnIterMut<'a>(IterMut<'a, Row>, usize);
-impl<'a> std::iter::Iterator for ColumnIterMut<'a> {
+impl<'a> Iterator for ColumnIterMut<'a> {
type Item = &'a mut Cell;
fn next(&mut self) -> Option<&'a mut Cell> {
self.0.next().and_then(|row| row.get_mut_cell(self.1))
@@ -574,26 +588,26 @@ impl<'a, T, E> Slice<'a, E> for T
/// # }
/// ```
///
-/// For details about style specifier syntax, check doc for [Cell::style_spec](cell/struct.Cell.html#method.style_spec) method
+/// For details about style specifier syntax, check doc for [`Cell::style_spec`](cell/struct.Cell.html#method.style_spec) method
#[macro_export]
-macro_rules! table {
- ($([$($content:tt)*]), *) => (
- $crate::Table::init(vec![$(row![$($content)*]), *])
- );
+macro_rules! table {
+ ($([$($content:tt)*]), *) => (
+ $crate::Table::init(vec![$(row![$($content)*]), *])
+ );
}
/// Create a table with `table!` macro, print it to standard output, then return this table for future usage.
///
/// The syntax is the same that the one for the `table!` macro
#[macro_export]
-macro_rules! ptable {
- ($($content:tt)*) => (
- {
- let tab = table!($($content)*);
- tab.printstd();
- tab
- }
- );
+macro_rules! ptable {
+ ($($content:tt)*) => (
+ {
+ let tab = table!($($content)*);
+ tab.printstd();
+ tab
+ }
+ );
}
#[cfg(test)]
@@ -611,14 +625,14 @@ mod tests {
table.add_row(Row::new(vec![Cell::new("a"), Cell::new("bc"), Cell::new("def")]));
table.add_row(Row::new(vec![Cell::new("def"), Cell::new("bc"), Cell::new("a")]));
table.set_titles(Row::new(vec![Cell::new("t1"), Cell::new("t2"), Cell::new("t3")]));
- let out = "\
-+-----+----+-----+
-| t1 | t2 | t3 |
-+=====+====+=====+
-| a | bc | def |
-+-----+----+-----+
-| def | bc | a |
-+-----+----+-----+
+ let out = "\
++-----+----+-----+
+| t1 | t2 | t3 |
++=====+====+=====+
+| a | bc | def |
++-----+----+-----+
+| def | bc | a |
++-----+----+-----+
";
assert_eq!(table.to_string().replace("\r\n", "\n"), out);
}
@@ -634,14 +648,14 @@ mod tests {
table[1][1] = Cell::new("newval");
assert_eq!(table[1][1].get_content(), "newval");
- let out = "\
-+-----+--------+-----+
-| t1 | t2 | t3 |
-+=====+========+=====+
-| a | bc | def |
-+-----+--------+-----+
-| def | newval | a |
-+-----+--------+-----+
+ let out = "\
++-----+--------+-----+
+| t1 | t2 | t3 |
++=====+========+=====+
+| a | bc | def |
++-----+--------+-----+
+| def | newval | a |
++-----+--------+-----+
";
assert_eq!(table.to_string().replace("\r\n", "\n"), out);
}
@@ -658,12 +672,12 @@ mod tests {
table[1][1] = Cell::new("newval");
assert_eq!(table[1][1].get_content(), "newval");
- let out = "\
-+-----+--------+-----+
-| t1 | t2 | t3 |
-| a | bc | def |
-| def | newval | a |
-+-----+--------+-----+
+ let out = "\
++-----+--------+-----+
+| t1 | t2 | t3 |
+| a | bc | def |
+| def | newval | a |
++-----+--------+-----+
";
assert_eq!(table.to_string().replace("\r\n", "\n"), out);
}
@@ -680,14 +694,14 @@ mod tests {
table[1][1] = Cell::new("newval");
assert_eq!(table[1][1].get_content(), "newval");
- let out = "\
-------------------
- t1 t2 t3 \n\
-==================
- a bc def \n\
-------------------
- def newval a \n\
-------------------
+ let out = "\
+------------------
+ t1 t2 t3 \n\
+==================
+ a bc def \n\
+------------------
+ def newval a \n\
+------------------
";
println!("{}", out);
println!("____");
@@ -707,10 +721,10 @@ mod tests {
table[1][1] = Cell::new("newval");
assert_eq!(table[1][1].get_content(), "newval");
- let out = "\
-\u{0020}t1 t2 t3 \n\
-\u{0020}a bc def \n\
-\u{0020}def newval a \n\
+ let out = "\
+\u{0020}t1 t2 t3 \n\
+\u{0020}a bc def \n\
+\u{0020}def newval a \n\
";
println!("{}", out);
println!("____");
@@ -732,14 +746,14 @@ mod tests {
table[1][1] = Cell::new("newval");
assert_eq!(table[1][1].get_content(), "newval");
- let out = "\
-+-------+----------+-------+
-| t1 | t2 | t3 |
-+=======+==========+=======+
-| a | bc | def |
-+-------+----------+-------+
-| def | newval | a |
-+-------+----------+-------+
+ let out = "\
++-------+----------+-------+
+| t1 | t2 | t3 |
++=======+==========+=======+
+| a | bc | def |
++-------+----------+-------+
+| def | newval | a |
++-------+----------+-------+
";
println!("{}", out);
println!("____");
@@ -757,16 +771,16 @@ mod tests {
table.add_row(Row::new(vec![Cell::new("3"), Cell::new("3"), Cell::new("3")]));
table.add_row(Row::new(vec![Cell::new("4"), Cell::new("4"), Cell::new("4")]));
table.add_row(Row::new(vec![Cell::new("5"), Cell::new("5"), Cell::new("5")]));
- let out = "\
-+----+----+----+
-| t1 | t2 | t3 |
-+====+====+====+
-| 1 | 1 | 1 |
-+----+----+----+
-| 2 | 2 | 2 |
-+----+----+----+
-| 3 | 3 | 3 |
-+----+----+----+
+ let out = "\
++----+----+----+
+| t1 | t2 | t3 |
++====+====+====+
+| 1 | 1 | 1 |
++----+----+----+
+| 2 | 2 | 2 |
++----+----+----+
+| 3 | 3 | 3 |
++----+----+----+
";
let slice = table.slice(..);
let slice = slice.slice(1..);
@@ -801,14 +815,14 @@ mod tests {
table.add_row(Row::new(vec![Cell::new("1"), Cell::new("1"), Cell::new("1")]));
table.add_row(Row::new(vec![Cell::new("2"), Cell::new("2"), Cell::new("2")]));
table.set_titles(Row::new(vec![Cell::new("t1"), Cell::new("t2"), Cell::new("t3")]));
- let out = "\
-┌────┬────┬────┐
-| t1 | t2 | t3 |
-├────┼────┼────┤
-| 1 | 1 | 1 |
-├────┼────┼────┤
-| 2 | 2 | 2 |
-└────┴────┴────┘
+ let out = "\
+┌────┬────┬────┐
+| t1 | t2 | t3 |
+├────┼────┼────┤
+| 1 | 1 | 1 |
+├────┼────┼────┤
+| 2 | 2 | 2 |
+└────┴────┴────┘
";
println!("{}", out);
println!("____");
@@ -822,8 +836,8 @@ mod tests {
use row::Row;
use cell::Cell;
- static CSV_S: &'static str = "ABC,DEFG,HIJKLMN\n\
- foobar,bar,foo\n\
+ static CSV_S: &'static str = "ABC,DEFG,HIJKLMN\n\
+ foobar,bar,foo\n\
foobar2,bar2,foo2\n";
fn test_table() -> Table {