Struct tabled::tables::Table

source ·
pub struct Table { /* private fields */ }
Available on crate feature std only.
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, settings::{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

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

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

Creates a builder from a data set given.

Example
use tabled::{
    Table, Tabled,
    settings::{object::Segment, Modify, 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 table = Table::builder(data)
    .index()
    .column(0)
    .transpose()
    .build()
    .with(Modify::new(Segment::new(1.., 1..)).with(Alignment::center()))
    .to_string();

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

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

It’s a generic function which applies options to the Table.

It applies settings immediately.

source

pub fn modify<T, O>(&mut self, target: T, option: O) -> &mut Self

It’s a generic function which applies options to a particalar cells on the Table. Target cells using Objects such as Cell, Rows, Location and more.

It applies settings immediately.

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_height(&self) -> usize

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

source

pub fn total_width(&self) -> usize

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

source

pub fn get_config(&self) -> &ColoredConfig

Returns a table config.

source

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

Returns a table config.

source

pub fn get_records(&self) -> &VecRecords<CellInfo<String>>

Returns a used records.

source

pub fn get_records_mut(&mut self) -> &mut VecRecords<CellInfo<String>>

Returns a used records.

Trait Implementations§

source§

impl Clone for Table

source§

fn clone(&self) -> Table

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 Debug for Table

source§

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

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

impl Default for Table

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl Display for Table

source§

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

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

impl From<Builder> for Table

source§

fn from(builder: Builder) -> Self

Converts to this type from the input type.
source§

impl From<Table> for Builder

source§

fn from(val: Table) -> Self

Converts to this type from the input type.
source§

impl<T> FromIterator<T> for Table
where T: IntoIterator, T::Item: Into<String>,

source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more
source§

impl PartialEq for Table

source§

fn eq(&self, other: &Table) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for Table

source§

impl StructuralEq for Table

source§

impl StructuralPartialEq for Table

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§

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> 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,

§

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>,

§

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>,

§

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.