pub struct Width;
Expand description
Width allows you to set a min and max width of an object on a Table
using different strategies.
It also allows you to set a min and max width for a whole table.
You can apply a min and max strategy at the same time with the same value, the value will be a total table width.
It is an abstract factory.
Beware that borders are not removed when you set a size value to very small. For example if you set size to 0 the table still be rendered but with all content removed.
Also be aware that it doesn’t changes Padding
settings nor it considers them.
The function is color aware if a color
feature is on.
§Examples
§Cell change
use tabled::{object::Segment, Width, Modify, Style, Table};
let data = ["Hello", "World", "!"];
let table = Table::new(&data)
.with(Style::markdown())
.with(Modify::new(Segment::all()).with(Width::truncate(3).suffix("...")));
§Table change
use tabled::{Width, Table};
let table = Table::new(&["Hello World!"]).with(Width::wrap(5));
§Total width
use tabled::{Width, Table};
let table = Table::new(&["Hello World!"])
.with(Width::wrap(5))
.with(Width::increase(5));
Implementations§
Source§impl Width
impl Width
Sourcepub fn truncate<W>(width: W) -> Truncate<'static, W>where
W: Measurment<Width>,
pub fn truncate<W>(width: W) -> Truncate<'static, W>where
W: Measurment<Width>,
Returns a Truncate
structure.
Sourcepub fn increase<W>(width: W) -> MinWidth<W>where
W: Measurment<Width>,
pub fn increase<W>(width: W) -> MinWidth<W>where
W: Measurment<Width>,
Returns a MinWidth
structure.
Sourcepub fn justify<W>(width: W) -> Justify<W>where
W: Measurment<Width>,
pub fn justify<W>(width: W) -> Justify<W>where
W: Measurment<Width>,
Returns a Justify
structure.
Sourcepub fn list<I>(rows: I) -> WidthListwhere
I: IntoIterator<Item = usize>,
pub fn list<I>(rows: I) -> WidthListwhere
I: IntoIterator<Item = usize>,
Create WidthList
to set a table width to a constant list of column widths.
Notice if you provide a list with .len()
smaller than Table::count_columns
then it will have no affect.
Also notice that you must provide values bigger than or equal to a real content width, otherwise it may panic.
§Example
use tabled::{Table, Width};
let data = vec![
("Some\ndata", "here", "and here"),
("Some\ndata on a next", "line", "right here"),
];
let table = Table::new(data)
.with(Width::list([20, 10, 12]))
.to_string();
assert_eq!(
table,
"+--------------------+----------+------------+\n\
| &str | &str | &str |\n\
+--------------------+----------+------------+\n\
| Some | here | and here |\n\
| data | | |\n\
+--------------------+----------+------------+\n\
| Some | line | right here |\n\
| data on a next | | |\n\
+--------------------+----------+------------+"
)