tabled

Struct Table

Source
pub struct Table<R = VecRecords<CellInfo<'static>>> { /* private fields */ }
Expand description

The structure provides an interface for building a table for types that implements Tabled.

To build a string representation of a table you must use a std::fmt::Display. Or simply call .to_string() method.

The default table Style is Style::ascii, with a 1 left and right Padding.

§Example

§Basic usage

use tabled::Table;

let table = Table::new(&["Year", "2021"]);

§With settings

use tabled::{Table, Style, Alignment};

let data = vec!["Hello", "2021"];
let mut table = Table::new(&data);
table.with(Style::psql()).with(Alignment::left());

println!("{}", table);

Implementations§

Source§

impl Table<VecRecords<CellInfo<'static>>>

Source

pub fn new<I, T>(iter: I) -> Self
where I: IntoIterator<Item = T>, T: Tabled,

New creates a Table instance.

If you use a reference iterator you’d better use FromIterator instead. As it has a different lifetime constraints and make less copies therefore.

Source§

impl Table<()>

Source

pub fn builder<I, T>(iter: I) -> Builder<'static>
where T: Tabled, I: IntoIterator<Item = T>,

Creates a builder from a data set given.

§Example
use tabled::{Table, Tabled, object::Segment, ModifyObject, Alignment};

#[derive(Tabled)]
struct User {
    name: &'static str,
    #[tabled(inline("device::"))]
    device: Device,
}

#[derive(Tabled)]
enum Device {
    PC,
    Mobile
}

let data = vec![
    User { name: "Vlad", device: Device::Mobile },
    User { name: "Dimitry", device: Device::PC },
    User { name: "John", device: Device::PC },
];

let mut builder = Table::builder(data).index();
builder.set_index(0);
builder.transpose();

let table = builder.build()
    .with(Segment::new(1.., 1..).modify().with(Alignment::center()))
    .to_string();

assert_eq!(
    table,
    "+----------------+------+---------+------+\n\
     | name           | Vlad | Dimitry | John |\n\
     +----------------+------+---------+------+\n\
     | device::PC     |      |    +    |  +   |\n\
     +----------------+------+---------+------+\n\
     | device::Mobile |  +   |         |      |\n\
     +----------------+------+---------+------+"
)
Source§

impl<R> Table<R>

Source

pub fn get_config(&self) -> &GridConfig

Get a reference to the table’s cfg.

Source

pub fn get_config_mut(&mut self) -> &mut GridConfig

Get a reference to the table’s cfg.

Source

pub fn get_records(&self) -> &R

Get a reference to the table’s records.

Source

pub fn get_records_mut(&mut self) -> &mut R

Get a reference to the table’s records.

Source

pub fn with<O>(&mut self, option: O) -> &mut Self
where O: TableOption<R>,

With is a generic function which applies options to the Table.

It applies settings immediately.

Source

pub fn has_header(&self) -> bool

A verification that first row is actually a header.

It’s true when Table::new and Table::builder is used. In many other cases it’s false.

Source§

impl<R> Table<R>
where R: Records,

Source

pub fn shape(&self) -> (usize, usize)

Returns a table shape (count rows, count columns).

Source

pub fn count_rows(&self) -> usize

Returns an amount of rows in the table.

Source

pub fn count_columns(&self) -> usize

Returns an amount of columns in the table.

Source

pub fn is_empty(&self) -> bool

Returns a table shape (count rows, count columns).

Source

pub fn total_width(&self) -> usize

Returns total widths of a table, including margin and vertical lines.

Source

pub fn total_height(&self) -> usize

Returns total widths of a table, including margin and horizontal lines.

Trait Implementations§

Source§

impl<R: Clone> Clone for Table<R>

Source§

fn clone(&self) -> Table<R>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<R: Debug> Debug for Table<R>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<R> Display for Table<R>
where R: Records,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<R> From<R> for Table<R>
where R: Records,

Source§

fn from(records: R) -> Self

Converts to this type from the input type.
Source§

impl<'a, T> FromIterator<&'a T> for Table<VecRecords<CellInfo<'a>>>
where T: Tabled + 'a,

Source§

fn from_iter<I>(iter: I) -> Self
where I: IntoIterator<Item = &'a T>,

Creates a value from an iterator. Read more

Auto Trait Implementations§

§

impl<R> Freeze for Table<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for Table<R>
where R: RefUnwindSafe,

§

impl<R> Send for Table<R>
where R: Send,

§

impl<R> Sync for Table<R>
where R: Sync,

§

impl<R> Unpin for Table<R>
where R: Unpin,

§

impl<R> UnwindSafe for Table<R>
where R: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.