diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | examples/basic.rs | 34 | ||||
-rw-r--r-- | examples/slices.rs | 22 | ||||
-rw-r--r-- | examples/style.rs | 16 | ||||
-rw-r--r-- | examples/tictactoe.rs | 22 | ||||
-rw-r--r-- | src/cell.rs | 89 | ||||
-rw-r--r-- | src/format.rs | 400 | ||||
-rw-r--r-- | src/lib.rs | 196 | ||||
-rw-r--r-- | src/main.rs | 12 | ||||
-rw-r--r-- | src/row.rs | 39 | ||||
-rw-r--r-- | src/utils.rs | 20 |
11 files changed, 443 insertions, 409 deletions
@@ -1,2 +1,4 @@ /target/ /Cargo.lock + +\.vscode/ diff --git a/examples/basic.rs b/examples/basic.rs index 4ca5d39..fe397d8 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -4,23 +4,23 @@ use prettytable::Table; use prettytable::row::Row; use prettytable::cell::Cell; -/*
- Following main function will print :
- +---------+------+---------+
- | ABC | DEFG | HIJKLMN |
- +---------+------+---------+
- | foobar | bar | foo |
- +---------+------+---------+
- | foobar2 | bar2 | foo2 |
- +---------+------+---------+
- Modified :
- +---------+------+---------+
- | ABC | DEFG | HIJKLMN |
- +---------+------+---------+
- | foobar | bar | foo |
- +---------+------+---------+
- | foobar2 | bar2 | new_foo |
- +---------+------+---------+
+/* + Following main function will print : + +---------+------+---------+ + | ABC | DEFG | HIJKLMN | + +---------+------+---------+ + | foobar | bar | foo | + +---------+------+---------+ + | foobar2 | bar2 | foo2 | + +---------+------+---------+ + Modified : + +---------+------+---------+ + | ABC | DEFG | HIJKLMN | + +---------+------+---------+ + | foobar | bar | foo | + +---------+------+---------+ + | foobar2 | bar2 | new_foo | + +---------+------+---------+ */ fn main() { let mut table = Table::new(); diff --git a/examples/slices.rs b/examples/slices.rs index 38e07ee..e66ba36 100644 --- a/examples/slices.rs +++ b/examples/slices.rs @@ -11,17 +11,17 @@ fn main() { let slice = slice.slice(2..); let slice = slice.slice(..3); - /*
- Will print
- +----+----+----+
- | t1 | t2 | t3 |
- +====+====+====+
- | 2 | 2 | 2 |
- +----+----+----+
- | 3 | 3 | 3 |
- +----+----+----+
- | 4 | 4 | 4 |
- +----+----+----+
+ /* + Will print + +----+----+----+ + | t1 | t2 | t3 | + +====+====+====+ + | 2 | 2 | 2 | + +----+----+----+ + | 3 | 3 | 3 | + +----+----+----+ + | 4 | 4 | 4 | + +----+----+----+ */ slice.printstd(); diff --git a/examples/style.rs b/examples/style.rs index 843061f..d001fca 100644 --- a/examples/style.rs +++ b/examples/style.rs @@ -15,14 +15,14 @@ fn main() { table.add_row(row![FrByb->"ABC", "DEFG", "HIJKLMN"]); // Add style to a full row table.add_row(row![FY => "styled", "bar", "foo"]); - table.add_row(Row::new(vec![
- Cell::new("foobar2"),
- // Create a cell with a red foreground color
- Cell::new("bar2").with_style(Attr::ForegroundColor(color::RED)),
- // Create a cell with red foreground color, yellow background color, with bold characters
- Cell::new("foo2").style_spec("FrByb"),
- // Using the cell! macro
- cell!(Fr->"red")])
+ table.add_row(Row::new(vec![ + Cell::new("foobar2"), + // Create a cell with a red foreground color + Cell::new("bar2").with_style(Attr::ForegroundColor(color::RED)), + // Create a cell with red foreground color, yellow background color, with bold characters + Cell::new("foo2").style_spec("FrByb"), + // Using the cell! macro + cell!(Fr->"red")]) ); table.printstd(); diff --git a/examples/tictactoe.rs b/examples/tictactoe.rs index 49444eb..c8ffdc5 100644 --- a/examples/tictactoe.rs +++ b/examples/tictactoe.rs @@ -23,12 +23,12 @@ fn main() { print!("{} plays > ", current); stdout.flush().unwrap(); stdin.read_line(&mut line).expect("Cannot read input"); - let i = match usize::from_str(line.trim()) {
- Ok(i) => i,
+ let i = match usize::from_str(line.trim()) { + Ok(i) => i, _ => { println!("Bad input"); continue; - }
+ } }; if i < 1 || i > 9 { println!("Bad input, should be between 1 and 9"); @@ -57,14 +57,14 @@ fn main() { } fn get(table: &Table, x: usize, y: usize) -> String { - match table.get_row(y) {
- Some(ref r) => { - match r.get_cell(x) {
- Some(ref c) => c.to_string(),
- _ => EMPTY.to_string(),
+ match table.get_row(y) { + Some(r) => { + match r.get_cell(x) { + Some(c) => c.to_string(), + _ => EMPTY.to_string(), } - }
- _ => EMPTY.to_string(),
+ } + _ => EMPTY.to_string(), } } @@ -94,5 +94,5 @@ fn check(table: &Table) -> bool { if full { println!("Game is over. It's a draw"); } - return full; + full } diff --git a/src/cell.rs b/src/cell.rs index 18aadd0..fd0e0f0 100644 --- a/src/cell.rs +++ b/src/cell.rs @@ -23,7 +23,7 @@ impl Cell { /// Create a new `Cell` initialized with content from `string`. /// Text alignment in cell is configurable with the `align` argument pub fn new_align(string: &str, align: Alignment) -> Cell { - let content: Vec<String> = string.lines().map(|ref x| x.to_string()).collect(); + let content: Vec<String> = string.lines().map(|x| x.to_string()).collect(); let mut width = 0; for cont in &content { let l = UnicodeWidthStr::width(&cont[..]); @@ -109,29 +109,29 @@ impl Cell { let mut background = false; for c in spec.chars() { if foreground || background { - let color = match c {
- 'r' => color::RED,
- 'R' => color::BRIGHT_RED,
- 'b' => color::BLUE,
- 'B' => color::BRIGHT_BLUE,
- 'g' => color::GREEN,
- 'G' => color::BRIGHT_GREEN,
- 'y' => color::YELLOW,
- 'Y' => color::BRIGHT_YELLOW,
- 'c' => color::CYAN,
- 'C' => color::BRIGHT_CYAN,
- 'm' => color::MAGENTA,
- 'M' => color::BRIGHT_MAGENTA,
- 'w' => color::WHITE,
- 'W' => color::BRIGHT_WHITE,
- 'd' => color::BLACK,
- 'D' => color::BRIGHT_BLACK,
+ let color = match c { + 'r' => color::RED, + 'R' => color::BRIGHT_RED, + 'b' => color::BLUE, + 'B' => color::BRIGHT_BLUE, + 'g' => color::GREEN, + 'G' => color::BRIGHT_GREEN, + 'y' => color::YELLOW, + 'Y' => color::BRIGHT_YELLOW, + 'c' => color::CYAN, + 'C' => color::BRIGHT_CYAN, + 'm' => color::MAGENTA, + 'M' => color::BRIGHT_MAGENTA, + 'w' => color::WHITE, + 'W' => color::BRIGHT_WHITE, + 'd' => color::BLACK, + 'D' => color::BRIGHT_BLACK, _ => { // Silently ignore unknown tags foreground = false; background = false; continue; - }
+ } }; if foreground { self.style(Attr::ForegroundColor(color)); @@ -141,21 +141,20 @@ impl Cell { foreground = false; background = false; } else { - match c {
- 'F' => foreground = true,
- 'B' => background = true,
- 'b' => self.style(Attr::Bold),
- 'i' => self.style(Attr::Italic(true)),
- 'u' => self.style(Attr::Underline(true)),
- 'c' => self.align(Alignment::CENTER),
- 'l' => self.align(Alignment::LEFT),
- 'r' => self.align(Alignment::RIGHT),
- 'd' => { /* Default : do nothing */ }
- _ => { /* Silently ignore unknown tags */ }
+ match c { + 'F' => foreground = true, + 'B' => background = true, + 'b' => self.style(Attr::Bold), + 'i' => self.style(Attr::Italic(true)), + 'u' => self.style(Attr::Underline(true)), + 'c' => self.align(Alignment::CENTER), + 'l' => self.align(Alignment::LEFT), + 'r' => self.align(Alignment::RIGHT), + _ => { /* Silently ignore unknown tags */ } } } } - return self; + self } /// Return the height of the cell @@ -195,27 +194,27 @@ impl Cell { skip_right_fill: bool) -> Result<(), Error> { for a in &self.style { - match out.attr(a.clone()) {
+ match out.attr(*a) { Ok(..) | Err(::term::Error::NotSupported) | - Err(::term::Error::ColorOutOfRange) => (), // Ignore unsupported atrributes
- Err(e) => return Err(term_error_to_io_error(e)),
+ Err(::term::Error::ColorOutOfRange) => (), // Ignore unsupported atrributes + Err(e) => return Err(term_error_to_io_error(e)), }; } try!(self.print(out, idx, col_width, skip_right_fill)); - match out.reset() {
+ match out.reset() { Ok(..) | Err(::term::Error::NotSupported) | - Err(::term::Error::ColorOutOfRange) => Ok(()),
- Err(e) => Err(term_error_to_io_error(e)),
+ Err(::term::Error::ColorOutOfRange) => Ok(()), + Err(e) => Err(term_error_to_io_error(e)), } } } fn term_error_to_io_error(te: ::term::Error) -> Error { - match te {
- ::term::Error::Io(why) => why,
- _ => Error::new(::std::io::ErrorKind::Other, te),
+ match te { + ::term::Error::Io(why) => why, + _ => Error::new(::std::io::ErrorKind::Other, te), } } @@ -257,7 +256,7 @@ impl Default for Cell { /// ``` /// Value must implement the `std::string::ToString` trait /// -/// 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 /// # Example /// ``` /// # #[macro_use] extern crate prettytable; @@ -271,10 +270,10 @@ impl Default for Cell { /// # } /// ``` #[macro_export] -macro_rules! cell {
- () => ($crate::cell::Cell::default());
- ($value:expr) => ($crate::cell::Cell::new(&$value.to_string()));
- ($style:ident -> $value:expr) => (cell!($value).style_spec(stringify!($style)));
+macro_rules! cell { + () => ($crate::cell::Cell::default()); + ($value:expr) => ($crate::cell::Cell::new(&$value.to_string())); + ($style:ident -> $value:expr) => (cell!($value).style_spec(stringify!($style))); } #[cfg(test)] diff --git a/src/format.rs b/src/format.rs index 25f477e..4160737 100644 --- a/src/format.rs +++ b/src/format.rs @@ -9,25 +9,36 @@ use super::utils::NEWLINE; /// Alignment for cell's content #[derive(Clone, Debug, PartialEq, Copy)] pub enum Alignment { + /// Align left LEFT, + /// Align in the center CENTER, + /// Align right RIGHT, } /// Position of a line separator in a table #[derive(Clone, Debug, PartialEq, Copy)] pub enum LinePosition { + /// Table's border on top Top, + /// Line separator between the titles row, + /// and the first data row Title, + /// Line separator between data rows Intern, + /// Bottom table's border Bottom, } /// Position of a column separator in a row #[derive(Clone, Debug, PartialEq, Copy)] pub enum ColumnPosition { + /// Left table's border Left, + /// Internal column separators Intern, + /// Rigth table's border Right, } @@ -164,11 +175,11 @@ impl TableFormat { /// Set a line separator pub fn separator(&mut self, what: LinePosition, separator: LineSeparator) { - *match what {
- LinePosition::Top => &mut self.top_sep,
- LinePosition::Bottom => &mut self.bottom_sep,
- LinePosition::Title => &mut self.tsep,
- LinePosition::Intern => &mut self.lsep,
+ *match what { + LinePosition::Top => &mut self.top_sep, + LinePosition::Bottom => &mut self.bottom_sep, + LinePosition::Title => &mut self.tsep, + LinePosition::Intern => &mut self.lsep, } = Some(separator); } @@ -180,16 +191,16 @@ impl TableFormat { } fn get_sep_for_line(&self, pos: LinePosition) -> &Option<LineSeparator> { - match pos {
- LinePosition::Intern => return &self.lsep,
- LinePosition::Top => return &self.top_sep,
- LinePosition::Bottom => return &self.bottom_sep,
+ match pos { + LinePosition::Intern => &self.lsep, + LinePosition::Top => &self.top_sep, + LinePosition::Bottom => &self.bottom_sep, LinePosition::Title => { - match &self.tsep {
- s @ &Some(_) => s,
- &None => &self.lsep,
+ match &self.tsep { + s @ &Some(_) => s, + &None => &self.lsep, } - }
+ } } } @@ -199,7 +210,7 @@ impl TableFormat { col_width: &[usize], pos: LinePosition) -> Result<(), Error> { - match *self.get_sep_for_line(pos) {
+ match *self.get_sep_for_line(pos) { Some(ref l) => { l._print(out, col_width, @@ -207,16 +218,18 @@ impl TableFormat { self.csep.is_some(), self.lborder.is_some(), self.rborder.is_some()) - }
- None => Ok(()),
+ } + None => Ok(()), } } + /// Returns the character used to separate columns. + /// `pos` specify if the separator is left/right final or internal to the table pub fn get_column_separator(&self, pos: ColumnPosition) -> Option<char> { - match pos {
- ColumnPosition::Left => self.lborder,
- ColumnPosition::Intern => self.csep,
- ColumnPosition::Right => self.rborder,
+ match pos { + ColumnPosition::Left => self.lborder, + ColumnPosition::Intern => self.csep, + ColumnPosition::Right => self.rborder, } } @@ -225,9 +238,9 @@ impl TableFormat { out: &mut T, pos: ColumnPosition) -> Result<(), Error> { - match self.get_column_separator(pos) {
- Some(s) => out.write_all(Utf8Char::from(s).as_bytes()),
- None => Ok(()),
+ match self.get_column_separator(pos) { + Some(s) => out.write_all(Utf8Char::from(s).as_bytes()), + None => Ok(()), } } } @@ -244,6 +257,7 @@ pub struct FormatBuilder { } impl FormatBuilder { + /// Creates a new builder pub fn new() -> FormatBuilder { FormatBuilder { format: Box::new(TableFormat::new()) } } @@ -289,175 +303,175 @@ impl FormatBuilder { pub mod consts { use super::{TableFormat, LineSeparator, FormatBuilder, LinePosition}; - lazy_static! {
- /// A line separator made of `-` and `+`
- static ref MINUS_PLUS_SEP: LineSeparator = LineSeparator::new('-', '+', '+', '+');
- /// A line separator made of `=` and `+`
- static ref EQU_PLUS_SEP: LineSeparator = LineSeparator::new('=', '+', '+', '+');
-
- /// Default table format
- ///
- /// # Example
- /// ```text
- /// +----+----+
- /// | T1 | T2 |
- /// +====+====+
- /// | a | b |
- /// +----+----+
- /// | d | c |
- /// +----+----+
- /// ```
- pub static ref FORMAT_DEFAULT: TableFormat = FormatBuilder::new()
- .column_separator('|')
- .borders('|')
- .separator(LinePosition::Intern, *MINUS_PLUS_SEP)
- .separator(LinePosition::Title, *EQU_PLUS_SEP)
- .separator(LinePosition::Bottom, *MINUS_PLUS_SEP)
- .separator(LinePosition::Top, *MINUS_PLUS_SEP)
- .padding(1, 1)
- .build();
-
- /// Similar to `FORMAT_DEFAULT` but without special separator after title line
- ///
- /// # Example
- /// ```text
- /// +----+----+
- /// | T1 | T2 |
- /// +----+----+
- /// | a | b |
- /// +----+----+
- /// | c | d |
- /// +----+----+
- /// ```
- pub static ref FORMAT_NO_TITLE: TableFormat = FormatBuilder::new()
- .column_separator('|')
- .borders('|')
- .separator(LinePosition::Intern, *MINUS_PLUS_SEP)
- .separator(LinePosition::Title, *MINUS_PLUS_SEP)
- .separator(LinePosition::Bottom, *MINUS_PLUS_SEP)
- .separator(LinePosition::Top, *MINUS_PLUS_SEP)
- .padding(1, 1)
- .build();
-
- /// With no line separator, but with title separator
- ///
- /// # Example
- /// ```text
- /// +----+----+
- /// | T1 | T2 |
- /// +----+----+
- /// | a | b |
- /// | c | d |
- /// +----+----+
- /// ```
- pub static ref FORMAT_NO_LINESEP_WITH_TITLE: TableFormat = FormatBuilder::new()
- .column_separator('|')
- .borders('|')
- .separator(LinePosition::Title, *MINUS_PLUS_SEP)
- .separator(LinePosition::Bottom, *MINUS_PLUS_SEP)
- .separator(LinePosition::Top, *MINUS_PLUS_SEP)
- .padding(1, 1)
- .build();
-
- /// With no line or title separator
- ///
- /// # Example
- /// ```text
- /// +----+----+
- /// | T1 | T2 |
- /// | a | b |
- /// | c | d |
- /// +----+----+
- /// ```
- pub static ref FORMAT_NO_LINESEP: TableFormat = FormatBuilder::new()
- .column_separator('|')
- .borders('|')
- .separator(LinePosition::Bottom, *MINUS_PLUS_SEP)
- .separator(LinePosition::Top, *MINUS_PLUS_SEP)
- .padding(1, 1)
- .build();
-
- /// No column separator
- ///
- /// # Example
- /// ```text
- /// --------
- /// T1 T2
- /// ========
- /// a b
- /// --------
- /// d c
- /// --------
- /// ```
- pub static ref FORMAT_NO_COLSEP: TableFormat = FormatBuilder::new()
- .separator(LinePosition::Intern, *MINUS_PLUS_SEP)
- .separator(LinePosition::Title, *EQU_PLUS_SEP)
- .separator(LinePosition::Bottom, *MINUS_PLUS_SEP)
- .separator(LinePosition::Top, *MINUS_PLUS_SEP)
- .padding(1, 1)
- .build();
-
- /// Format for printing a table without any separators (only alignment)
- ///
- /// # Example
- /// ```text
- /// T1 T2
- /// a b
- /// d c
- /// ```
- pub static ref FORMAT_CLEAN: TableFormat = FormatBuilder::new()
- .padding(1, 1)
- .build();
-
- /// Format for a table with only external borders and title separator
- ///
- /// # Example
- /// ```text
- /// +--------+
- /// | T1 T2 |
- /// +========+
- /// | a b |
- /// | c d |
- /// +--------+
- /// ```
- pub static ref FORMAT_BORDERS_ONLY: TableFormat = FormatBuilder::new()
- .padding(1, 1)
- .separator(LinePosition::Title, *EQU_PLUS_SEP)
- .separator(LinePosition::Bottom, *MINUS_PLUS_SEP)
- .separator(LinePosition::Top, *MINUS_PLUS_SEP)
- .borders('|')
- .build();
-
- /// A table with no external border
- ///
- /// # Example
- /// ```text
- /// T1 | T2
- /// ====+====
- /// a | b
- /// ----+----
- /// c | d
- /// ```
- pub static ref FORMAT_NO_BORDER: TableFormat = FormatBuilder::new()
- .padding(1, 1)
- .separator(LinePosition::Intern, *MINUS_PLUS_SEP)
- .separator(LinePosition::Title, *EQU_PLUS_SEP)
- .column_separator('|')
- .build();
-
- /// A table with no external border and no line separation
- ///
- /// # Example
- /// ```text
- /// T1 | T2
- /// ----+----
- /// a | b
- /// c | d
- /// ```
- pub static ref FORMAT_NO_BORDER_LINE_SEPARATOR: TableFormat = FormatBuilder::new()
- .padding(1, 1)
- .separator(LinePosition::Title, *MINUS_PLUS_SEP)
- .column_separator('|')
- .build();
+ lazy_static! { + /// A line separator made of `-` and `+` + static ref MINUS_PLUS_SEP: LineSeparator = LineSeparator::new('-', '+', '+', '+'); + /// A line separator made of `=` and `+` + static ref EQU_PLUS_SEP: LineSeparator = LineSeparator::new('=', '+', '+', '+'); + + /// Default table format + /// + /// # Example + /// ```text + /// +----+----+ + /// | T1 | T2 | + /// +====+====+ + /// | a | b | + /// +----+----+ + /// | d | c | + /// +----+----+ + /// ``` + pub static ref FORMAT_DEFAULT: TableFormat = FormatBuilder::new() + .column_separator('|') + .borders('|') + .separator(LinePosition::Intern, *MINUS_PLUS_SEP) + .separator(LinePosition::Title, *EQU_PLUS_SEP) + .separator(LinePosition::Bottom, *MINUS_PLUS_SEP) + .separator(LinePosition::Top, *MINUS_PLUS_SEP) + .padding(1, 1) + .build(); + + /// Similar to `FORMAT_DEFAULT` but without special separator after title line + /// + /// # Example + /// ```text + /// +----+----+ + /// | T1 | T2 | + /// +----+----+ + /// | a | b | + /// +----+----+ + /// | c | d | + /// +----+----+ + /// ``` + pub static ref FORMAT_NO_TITLE: TableFormat = FormatBuilder::new() + .column_separator('|') + .borders('|') + .separator(LinePosition::Intern, *MINUS_PLUS_SEP) + .separator(LinePosition::Title, *MINUS_PLUS_SEP) + .separator(LinePosition::Bottom, *MINUS_PLUS_SEP) + .separator(LinePosition::Top, *MINUS_PLUS_SEP) + .padding(1, 1) + .build(); + + /// With no line separator, but with title separator + /// + /// # Example + /// ```text + /// +----+----+ + /// | T1 | T2 | + /// +----+----+ + /// | a | b | + /// | c | d | + /// +----+----+ + /// ``` + pub static ref FORMAT_NO_LINESEP_WITH_TITLE: TableFormat = FormatBuilder::new() + .column_separator('|') + .borders('|') + .separator(LinePosition::Title, *MINUS_PLUS_SEP) + .separator(LinePosition::Bottom, *MINUS_PLUS_SEP) + .separator(LinePosition::Top, *MINUS_PLUS_SEP) + .padding(1, 1) + .build(); + + /// With no line or title separator + /// + /// # Example + /// ```text + /// +----+----+ + /// | T1 | T2 | + /// | a | b | + /// | c | d | + /// +----+----+ + /// ``` + pub static ref FORMAT_NO_LINESEP: TableFormat = FormatBuilder::new() + .column_separator('|') + .borders('|') + .separator(LinePosition::Bottom, *MINUS_PLUS_SEP) + .separator(LinePosition::Top, *MINUS_PLUS_SEP) + .padding(1, 1) + .build(); + + /// No column separator + /// + /// # Example + /// ```text + /// -------- + /// T1 T2 + /// ======== + /// a b + /// -------- + /// d c + /// -------- + /// ``` + pub static ref FORMAT_NO_COLSEP: TableFormat = FormatBuilder::new() + .separator(LinePosition::Intern, *MINUS_PLUS_SEP) + .separator(LinePosition::Title, *EQU_PLUS_SEP) + .separator(LinePosition::Bottom, *MINUS_PLUS_SEP) + .separator(LinePosition::Top, *MINUS_PLUS_SEP) + .padding(1, 1) + .build(); + + /// Format for printing a table without any separators (only alignment) + /// + /// # Example + /// ```text + /// T1 T2 + /// a b + /// d c + /// ``` + pub static ref FORMAT_CLEAN: TableFormat = FormatBuilder::new() + .padding(1, 1) + .build(); + + /// Format for a table with only external borders and title separator + /// + /// # Example + /// ```text + /// +--------+ + /// | T1 T2 | + /// +========+ + /// | a b | + /// | c d | + /// +--------+ + /// ``` + pub static ref FORMAT_BORDERS_ONLY: TableFormat = FormatBuilder::new() + .padding(1, 1) + .separator(LinePosition::Title, *EQU_PLUS_SEP) + .separator(LinePosition::Bottom, *MINUS_PLUS_SEP) + .separator(LinePosition::Top, *MINUS_PLUS_SEP) + .borders('|') + .build(); + + /// A table with no external border + /// + /// # Example + /// ```text + /// T1 | T2 + /// ====+==== + /// a | b + /// ----+---- + /// c | d + /// ``` + pub static ref FORMAT_NO_BORDER: TableFormat = FormatBuilder::new() + .padding(1, 1) + .separator(LinePosition::Intern, *MINUS_PLUS_SEP) + .separator(LinePosition::Title, *EQU_PLUS_SEP) + .column_separator('|') + .build(); + + /// A table with no external border and no line separation + /// + /// # Example + /// ```text + /// T1 | T2 + /// ----+---- + /// a | b + /// c | d + /// ``` + pub static ref FORMAT_NO_BORDER_LINE_SEPARATOR: TableFormat = FormatBuilder::new() + .padding(1, 1) + .separator(LinePosition::Title, *MINUS_PLUS_SEP) + .column_separator('|') + .build(); } } @@ -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() { - |