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>>>
impl Table<VecRecords<CellInfo<'static>>>
Sourcepub fn new<I, T>(iter: I) -> Selfwhere
I: IntoIterator<Item = T>,
T: Tabled,
pub fn new<I, T>(iter: I) -> Selfwhere
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<()>
impl Table<()>
Sourcepub fn builder<I, T>(iter: I) -> Builder<'static>where
T: Tabled,
I: IntoIterator<Item = T>,
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>
impl<R> Table<R>
Sourcepub fn get_config(&self) -> &GridConfig
pub fn get_config(&self) -> &GridConfig
Get a reference to the table’s cfg.
Sourcepub fn get_config_mut(&mut self) -> &mut GridConfig
pub fn get_config_mut(&mut self) -> &mut GridConfig
Get a reference to the table’s cfg.
Sourcepub fn get_records(&self) -> &R
pub fn get_records(&self) -> &R
Get a reference to the table’s records.
Sourcepub fn get_records_mut(&mut self) -> &mut R
pub fn get_records_mut(&mut self) -> &mut R
Get a reference to the table’s records.
Sourcepub fn with<O>(&mut self, option: O) -> &mut Selfwhere
O: TableOption<R>,
pub fn with<O>(&mut self, option: O) -> &mut Selfwhere
O: TableOption<R>,
With is a generic function which applies options to the Table
.
It applies settings immediately.
Sourcepub fn has_header(&self) -> bool
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,
impl<R> Table<R>where
R: Records,
Sourcepub fn count_rows(&self) -> usize
pub fn count_rows(&self) -> usize
Returns an amount of rows in the table.
Sourcepub fn count_columns(&self) -> usize
pub fn count_columns(&self) -> usize
Returns an amount of columns in the table.
Sourcepub fn total_width(&self) -> usize
pub fn total_width(&self) -> usize
Returns total widths of a table, including margin and vertical lines.
Sourcepub fn total_height(&self) -> usize
pub fn total_height(&self) -> usize
Returns total widths of a table, including margin and horizontal lines.