pub struct Builder<'a> { /* private fields */ }
Expand description
Builder creates a Table
from dynamic data set.
It useful when the amount of columns or rows is not known statically.
use tabled::builder::Builder;
let mut builder = Builder::default();
builder.set_columns(["index", "measure", "value"]);
builder.add_record(["0", "weight", "0.443"]);
let table = builder.build();
println!("{}", table);
It may be useful to use FromIterator
for building.
use tabled::builder::Builder;
use std::iter::FromIterator;
let data = vec![
["column1", "column2"],
["data1", "data2"],
["data3", "data4"],
];
let table = Builder::from_iter(data).build();
println!("{}", table);
Implementations§
Source§impl<'a> Builder<'a>
impl<'a> Builder<'a>
Sourcepub fn hint_column_size(&mut self, size: usize)
pub fn hint_column_size(&mut self, size: usize)
Set a column size.
If it make it lower then it was originally it is considered NOP.
Sourcepub fn set_columns<H, T>(&mut self, columns: H) -> &mut Self
pub fn set_columns<H, T>(&mut self, columns: H) -> &mut Self
Sets a Table
header.
use tabled::builder::Builder;
let mut builder = Builder::default();
builder
.set_columns((0..3).map(|i| i.to_string()))
.add_record(["i", "surname", "lastname"]);
Sourcepub fn remove_columns(&mut self) -> &mut Self
pub fn remove_columns(&mut self) -> &mut Self
Sets off a Table
header.
If not set its a nop.
use tabled::Table;
let data = [("Hello", 1u8, false), ("World", 21u8, true)];
let table = Table::builder(data).build().to_string();
assert_eq!(
table,
"+-------+----+-------+\n\
| &str | u8 | bool |\n\
+-------+----+-------+\n\
| Hello | 1 | false |\n\
+-------+----+-------+\n\
| World | 21 | true |\n\
+-------+----+-------+"
);
let mut builder = Table::builder(data);
builder.remove_columns();
let table = builder.build().to_string();
assert_eq!(
table,
"+-------+----+-------+\n\
| Hello | 1 | false |\n\
+-------+----+-------+\n\
| World | 21 | true |\n\
+-------+----+-------+"
);
Sourcepub fn add_record<R, T>(&mut self, row: R) -> &mut Self
pub fn add_record<R, T>(&mut self, row: R) -> &mut Self
Adds a row to a Table
.
If Self::set_columns
is not set the first row will be considered a header.
use tabled::builder::Builder;
let mut builder = Builder::default();
builder.add_record((0..3).map(|i| i.to_string()));
builder.add_record(["i", "surname", "lastname"]);
Sourcepub fn set_default_text<T>(&mut self, text: T) -> &mut Self
pub fn set_default_text<T>(&mut self, text: T) -> &mut Self
Sets a content of cells which are created in case rows has different length.
use tabled::builder::Builder;
let mut builder = Builder::default();
builder.set_default_text("undefined");
builder.set_columns((0..3).map(|i| i.to_string()));
builder.add_record(["i"]);
Sourcepub fn build(self) -> Table<VecRecords<CellInfo<'a>>>
pub fn build(self) -> Table<VecRecords<CellInfo<'a>>>
Build creates a Table
instance.
use tabled::builder::Builder;
let mut builder = Builder::default();
builder.set_columns(["i", "column1", "column2"]);
builder.add_record(["0", "value1", "value2"]);
Sourcepub fn index(self) -> IndexBuilder<'a>
pub fn index(self) -> IndexBuilder<'a>
Add an index to the Table
.
Default index is a range 0-N where N is amount of records.
§Example
use tabled::Table;
let table = Table::builder(&["Hello", "World", "!"]).index().build();
assert_eq!(
table.to_string(),
"+---+-------+\n\
| | &str |\n\
+---+-------+\n\
| 0 | Hello |\n\
+---+-------+\n\
| 1 | World |\n\
+---+-------+\n\
| 2 | ! |\n\
+---+-------+"
)
Sourcepub fn clean(&mut self) -> &mut Self
pub fn clean(&mut self) -> &mut Self
Clean removes empty columns and rows.
§Example
use tabled::Table;
let mut builder = Table::builder(&["Hello", "World", ""]);
builder.clean();
let table = builder.build();
assert_eq!(
table.to_string(),
"+-------+\n\
| &str |\n\
+-------+\n\
| Hello |\n\
+-------+\n\
| World |\n\
+-------+"
)
Sourcepub fn custom<R>(records: R) -> CustomRecords<R>
pub fn custom<R>(records: R) -> CustomRecords<R>
Creates a Builder from a built Records
Trait Implementations§
Source§impl<'a, D> Extend<D> for Builder<'a>
impl<'a, D> Extend<D> for Builder<'a>
Source§fn extend<T: IntoIterator<Item = D>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = D>>(&mut self, iter: T)
Extends a collection with the contents of an iterator. Read more
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
🔬This is a nightly-only experimental API. (
extend_one
)Extends a collection with exactly one element.
Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
🔬This is a nightly-only experimental API. (
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
Source§impl<'a> From<IndexBuilder<'a>> for Builder<'a>
impl<'a> From<IndexBuilder<'a>> for Builder<'a>
Source§fn from(index_builder: IndexBuilder<'a>) -> Self
fn from(index_builder: IndexBuilder<'a>) -> Self
Converts to this type from the input type.
Source§impl<'a, R, V> FromIterator<R> for Builder<'a>
impl<'a, R, V> FromIterator<R> for Builder<'a>
Source§fn from_iter<T: IntoIterator<Item = R>>(iter: T) -> Self
fn from_iter<T: IntoIterator<Item = R>>(iter: T) -> Self
Creates a value from an iterator. Read more
Auto Trait Implementations§
impl<'a> Freeze for Builder<'a>
impl<'a> RefUnwindSafe for Builder<'a>
impl<'a> Send for Builder<'a>
impl<'a> Sync for Builder<'a>
impl<'a> Unpin for Builder<'a>
impl<'a> UnwindSafe for Builder<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more