Struct tabled::settings::width::Width

source ·
pub struct Width;
Available on crate feature std only.
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::{Table, settings::{object::Segment, Width, Style, Modify}};

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::{Table, settings::Width};

let table = Table::new(&["Hello World!"]).with(Width::wrap(5));

Total width

use tabled::{Table, settings::Width};

let table = Table::new(&["Hello World!"])
    .with(Width::wrap(5))
    .with(Width::increase(5));

Implementations§

source§

impl Width

source

pub fn wrap<W: Measurement<Width>>(width: W) -> Wrap<W>

Returns a Wrap structure.

source

pub fn truncate<W: Measurement<Width>>(width: W) -> Truncate<'static, W>

Returns a Truncate structure.

source

pub fn increase<W: Measurement<Width>>(width: W) -> MinWidth<W>

Returns a MinWidth structure.

source

pub fn justify<W: Measurement<Width>>(width: W) -> Justify<W>

Returns a Justify structure.

source

pub fn list<I: IntoIterator<Item = usize>>(rows: I) -> WidthList

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, settings::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\
     +--------------------+----------+------------+"
)

Trait Implementations§

source§

impl Debug for Width

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Measurement<Width> for Max

source§

fn measure<R: Records + ExactRecords + PeekableRecords>( &self, records: R, _: &SpannedConfig ) -> usize

Returns a measurement value.
source§

impl Measurement<Width> for Min

source§

fn measure<R: Records + ExactRecords + PeekableRecords>( &self, records: R, _: &SpannedConfig ) -> usize

Returns a measurement value.
source§

impl Measurement<Width> for Percent

source§

fn measure<R>(&self, records: R, cfg: &SpannedConfig) -> usize
where R: Records, <R::Iter as IntoRecords>::Cell: AsRef<str>,

Returns a measurement value.

Auto Trait Implementations§

§

impl RefUnwindSafe for Width

§

impl Send for Width

§

impl Sync for Width

§

impl Unpin for Width

§

impl UnwindSafe for Width

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.