pub struct Builder { /* 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.push_record(["index", "measure", "value"]);
builder.push_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 Builder
impl Builder
Sourcepub fn new() -> Builder
pub fn new() -> Builder
Creates a Builder instance.
use tabled::builder::Builder;
let builder = Builder::new();Sourcepub fn with_capacity(count_records: usize, count_columns: usize) -> Builder
pub fn with_capacity(count_records: usize, count_columns: usize) -> Builder
Creates a Builder instance with a given row capacity.
use tabled::builder::Builder;
let mut builder = Builder::with_capacity(2, 3);
builder.push_record((0..3).map(|i| i.to_string()));
builder.push_record(["i", "surname", "lastname"]);Sourcepub fn set_empty<T>(&mut self, text: T)
pub fn set_empty<T>(&mut self, text: T)
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_empty("undefined");
builder.push_record((0..3).map(|i| i.to_string()));
builder.push_record(["i"]);Sourcepub fn build(self) -> Table
pub fn build(self) -> Table
Build creates a Table instance.
use tabled::builder::Builder;
let mut builder = Builder::default();
builder.push_record(["i", "column1", "column2"]);
builder.push_record(["0", "value1", "value2"]);Sourcepub fn index(self) -> IndexBuilder
pub fn index(self) -> IndexBuilder
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 push_record<R>(&mut self, record: R)
pub fn push_record<R>(&mut self, record: R)
Adds a row to a Table.
use tabled::builder::Builder;
let mut builder = Builder::default();
builder.push_record((0..3).map(|i| i.to_string()));
builder.push_record(["i", "surname", "lastname"]);Sourcepub fn insert_record<R>(&mut self, index: usize, record: R)
pub fn insert_record<R>(&mut self, index: usize, record: R)
Sourcepub fn clean(&mut self)
pub fn clean(&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 remove_record(&mut self, index: usize)
pub fn remove_record(&mut self, index: usize)
Removes a row with a specific position.
Index expected to be in range.
Builder::count_records() < x >= 0
§Panics
Panics if row_index > count_rows.
Sourcepub fn remove_column(&mut self, index: usize)
pub fn remove_column(&mut self, index: usize)
Removes a column with a specific position.
Index expected to be in range.
Builder::count_columns() < x >= 0
§Panics
Panics if index > count_columns.
Sourcepub fn push_column<I>(&mut self, column: I)
pub fn push_column<I>(&mut self, column: I)
Push a column.
Sourcepub fn insert_column<I>(&mut self, index: usize, column: I)
pub fn insert_column<I>(&mut self, index: usize, column: I)
Insert a column with a specific position.
In case a column is bigger then the total amount of rows it will be truncated.
§Panics
Panics if index > count_columns.
Sourcepub fn count_columns(&self) -> usize
pub fn count_columns(&self) -> usize
Returns an amount of columns which would be present in a built table.
Sourcepub fn count_records(&self) -> usize
pub fn count_records(&self) -> usize
Returns an amount of rows which would be present in a built table.
Notice that it does not include header if present; It returns only amount of records.
Trait Implementations§
Source§impl<D> Extend<D> for Builder
impl<D> Extend<D> for Builder
Source§fn extend<T>(&mut self, iter: T)where
T: IntoIterator<Item = D>,
fn extend<T>(&mut self, iter: T)where
T: IntoIterator<Item = D>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl From<IndexBuilder> for Builder
impl From<IndexBuilder> for Builder
Source§fn from(b: IndexBuilder) -> Builder
fn from(b: IndexBuilder) -> Builder
Source§impl<R> FromIterator<R> for Builder
impl<R> FromIterator<R> for Builder
Auto Trait Implementations§
impl Freeze for Builder
impl RefUnwindSafe for Builder
impl Send for Builder
impl Sync for Builder
impl Unpin for Builder
impl UnwindSafe for Builder
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);