Struct tabular::Table[][src]

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]

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()));

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

The number of columns in the table.

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\
                     " );

Convenience function for calling add_heading.

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().

Convenience function for calling add_row.

Panics

The same as add_row.

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]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl Debug for Table
[src]

Formats the value using the given formatter. Read more

impl Display for Table
[src]

Formats the value using the given formatter. Read more

Auto Trait Implementations

impl Send for Table

impl Sync for Table