[][src]Struct tabular::Table

pub struct Table { /* fields omitted */ }

Builder type for constructing a formatted table.

Construct this with Table::new() or Table::new_safe(). Then add rows to it with Table::add_row() and Table::add_heading().

Methods

impl Table[src]

pub fn new(row_spec: &str) -> Self[src]

Constructs a new table with the format of each row specified by row_spec.

Unlike format! and friends, row_spec is processed dynamically, but it uses a small subset of the syntax to determine how columns are laid out. In particular:

  • {:<} produces a left-aligned column.

  • {:^} produces a centered column.

  • {:>} produces a right-aligned column.

  • {{ produces a literal { character.

  • }} produces a literal } character.

  • Any other appearances of { or } are errors.

  • Everything else stands for itself.

Examples

let table = Table::new("{{:<}} produces ‘{:<}’ and {{:>}} produces ‘{:>}’")
    .with_row(Row::from_cells(["a", "bc"].iter().cloned()));

pub fn new_safe(row_spec: &str) -> Result<Self>[src]

Like new, but returns a Result instead of panicking if parsing row_spec fails.

pub fn column_count(&self) -> usize[src]

The number of columns in the table.

pub fn add_heading<S: Into<String>>(&mut self, heading: S) -> &mut Self[src]

Adds a pre-formatted row that spans all columns.

A heading does not interact with the formatting of rows made of cells. This is like \intertext in LaTeX, not like <head> or <th> in HTML.

Examples

        let mut table = Table::new("{:<}  {:>}");
        table
            .add_heading("./:")
            .add_row(Row::new().with_cell("Cargo.lock").with_cell(433))
            .add_row(Row::new().with_cell("Cargo.toml").with_cell(204))
            .add_heading("")
            .add_heading("src/:")
            .add_row(Row::new().with_cell("lib.rs").with_cell(10257))
            .add_heading("")
            .add_heading("target/:")
            .add_row(Row::new().with_cell("debug/").with_cell(672));

        assert_eq!( format!("{}", table),
                    "./:\n\
                     Cargo.lock    433\n\
                     Cargo.toml    204\n\
                     \n\
                     src/:\n\
                     lib.rs      10257\n\
                     \n\
                     target/:\n\
                     debug/        672\n\
                     " );

pub fn with_heading<S: Into<String>>(self, heading: S) -> Self[src]

Convenience function for calling add_heading.

pub fn add_row(&mut self, row: Row) -> &mut Self[src]

Adds a row made up of cells.

When printed, each cell will be padded to the size of its column, which is the maximum of the width of its cells.

Panics

If self.column_count() != row.len().

pub fn with_row(self, row: Row) -> Self[src]

Convenience function for calling add_row.

Panics

The same as add_row.

pub fn set_line_end<S: Into<String>>(self, line_end: S) -> Self[src]

Sets the string to output at the end of every line.

By default this is "\n" on all platforms, like println!.

Examples

#[cfg(windows)]
const DEFAULT_LINE_END: &'static str = "\r\n";
#[cfg(not(windows))]
const DEFAULT_LINE_END: &'static str = "\n";

let table = Table::new("{:>} {:<}").set_line_end(DEFAULT_LINE_END)
    .with_row(Row::new().with_cell("x").with_cell("x"))
    .with_row(Row::new().with_cell("yy").with_cell("yy"))
    .with_row(Row::new().with_cell("zzz").with_cell("zzz"));

assert_eq!( table.to_string(),
            format!("  x x{nl} yy yy{nl}zzz zzz{nl}", nl = DEFAULT_LINE_END) );

This works better than putting the carriage return in the format string:

let table = Table::new("{:>} {:<}\r")
    .with_row(Row::new().with_cell("x").with_cell("x"))
    .with_row(Row::new().with_cell("yy").with_cell("yy"))
    .with_row(Row::new().with_cell("zzz").with_cell("zzz"));

assert_eq!( table.to_string(),
            format!("  x x  \r\n yy yy \r\nzzz zzz\r\n") );

Note the trailing spaces. Trailing spaces mean that if any lines are wrapped then all lines are wrapped.

Trait Implementations

impl Clone for Table[src]

impl Debug for Table[src]

impl Display for Table[src]

Auto Trait Implementations

impl RefUnwindSafe for Table

impl Send for Table

impl Sync for Table

impl Unpin for Table

impl UnwindSafe for Table

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.