[−][src]Struct tabular::Table
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]
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]
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
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]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,