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]
impl Table
pub fn new(row_spec: &str) -> Self
[src]
pub fn new(row_spec: &str) -> Self
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]
pub fn new_safe(row_spec: &str) -> Result<Self>
pub fn column_count(&self) -> usize
[src]
pub fn column_count(&self) -> usize
The number of columns in the table.
pub fn add_heading<S: Into<String>>(&mut self, heading: S) -> &mut Self
[src]
pub fn add_heading<S: Into<String>>(&mut self, heading: S) -> &mut Self
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]
pub fn with_heading<S: Into<String>>(self, heading: S) -> Self
Convenience function for calling add_heading
.
pub fn add_row(&mut self, row: Row) -> &mut Self
[src]
pub fn add_row(&mut self, row: Row) -> &mut Self
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]
pub fn with_row(self, row: Row) -> Self
pub fn set_line_end<S: Into<String>>(self, line_end: S) -> Self
[src]
pub fn set_line_end<S: Into<String>>(self, line_end: S) -> Self
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 Clone for Table
fn clone(&self) -> Table
[src]
fn clone(&self) -> Table
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
impl Debug for Table
[src]
impl Debug for Table
fn fmt(&self, f: &mut Formatter) -> Result
[src]
fn fmt(&self, f: &mut Formatter) -> Result
Formats the value using the given formatter. Read more
impl Display for Table
[src]
impl Display for Table