pub struct Table { /* private fields */ }
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
impl Table
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.
sourcepub fn builder<I, T>(iter: I) -> Builderwhere
T: Tabled,
I: IntoIterator<Item = T>,
pub fn builder<I, T>(iter: I) -> Builderwhere
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\
+----------------+------+---------+------+"
)
sourcepub fn with<O>(&mut self, option: O) -> &mut Selfwhere
for<'a> O: TableOption<VecRecords<CellInfo<String>>, ColoredConfig, CompleteDimensionVecRecords<'a>>,
pub fn with<O>(&mut self, option: O) -> &mut Selfwhere
for<'a> O: TableOption<VecRecords<CellInfo<String>>, ColoredConfig, CompleteDimensionVecRecords<'a>>,
It’s a generic function which applies options to the Table
.
It applies settings immediately.
sourcepub fn modify<T, O>(&mut self, target: T, option: O) -> &mut Selfwhere
T: Object<VecRecords<CellInfo<String>>>,
O: CellOption<VecRecords<CellInfo<String>>, ColoredConfig> + Clone,
pub fn modify<T, O>(&mut self, target: T, option: O) -> &mut Selfwhere
T: Object<VecRecords<CellInfo<String>>>,
O: CellOption<VecRecords<CellInfo<String>>, ColoredConfig> + Clone,
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_height(&self) -> usize
pub fn total_height(&self) -> usize
Returns total widths of a table, including margin and horizontal lines.
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 get_config(&self) -> &ColoredConfig
pub fn get_config(&self) -> &ColoredConfig
Returns a table config.
sourcepub fn get_config_mut(&mut self) -> &mut ColoredConfig
pub fn get_config_mut(&mut self) -> &mut ColoredConfig
Returns a table config.
sourcepub fn get_records(&self) -> &VecRecords<CellInfo<String>>
pub fn get_records(&self) -> &VecRecords<CellInfo<String>>
Returns a used records.
sourcepub fn get_records_mut(&mut self) -> &mut VecRecords<CellInfo<String>>
pub fn get_records_mut(&mut self) -> &mut VecRecords<CellInfo<String>>
Returns a used records.