Struct Format

Source
pub struct Format;
Expand description

A formatting function of particular cells on a Table.

Implementations§

Source§

impl Format

Source

pub fn content<F>(f: F) -> FormatContent<F>
where F: FnMut(&str) -> String,

This function creates a new Format instance, so it can be used as a grid setting.

§Example
use tabled::{Table, settings::{Format, object::Rows, Modify}};

let data = vec![
    (0, "Grodno", true),
    (1, "Minsk", true),
    (2, "Hamburg", false),
    (3, "Brest", true),
];

let table = Table::new(&data)
               .with(Modify::new(Rows::new(1..)).with(Format::content(|s| format!(": {} :", s))))
               .to_string();

assert_eq!(
    table,
    "+-------+-------------+-----------+\n\
     | i32   | &str        | bool      |\n\
     +-------+-------------+-----------+\n\
     | : 0 : | : Grodno :  | : true :  |\n\
     +-------+-------------+-----------+\n\
     | : 1 : | : Minsk :   | : true :  |\n\
     +-------+-------------+-----------+\n\
     | : 2 : | : Hamburg : | : false : |\n\
     +-------+-------------+-----------+\n\
     | : 3 : | : Brest :   | : true :  |\n\
     +-------+-------------+-----------+"
);
Source

pub fn positioned<F>(f: F) -> FormatContentPositioned<F>
where F: FnMut(&str, Position) -> String,

This function creates a new FormatContentPositioned, so it can be used as a grid setting.

It’s different from Format::content as it also provides a row and column index.

§Example
use tabled::{Table, settings::{Format, object::Rows, Modify}};

let data = vec![
    (0, "Grodno", true),
    (1, "Minsk", true),
    (2, "Hamburg", false),
    (3, "Brest", true),
];

let table = Table::new(&data)
               .modify(Rows::one(0), Format::positioned(|_, p| p.col.to_string()))
               .to_string();

assert_eq!(
    table,
    "+---+---------+-------+\n\
     | 0 | 1       | 2     |\n\
     +---+---------+-------+\n\
     | 0 | Grodno  | true  |\n\
     +---+---------+-------+\n\
     | 1 | Minsk   | true  |\n\
     +---+---------+-------+\n\
     | 2 | Hamburg | false |\n\
     +---+---------+-------+\n\
     | 3 | Brest   | true  |\n\
     +---+---------+-------+"
);
Source

pub fn config<F>(f: F) -> FormatConfig<F>

This function creates FormatConfig function to modify a table config.

§Example
use tabled::{
    Table,
    settings::{Format, object::Rows, Modify},
    grid::config::ColoredConfig,
};

let data = vec![
    (0, "Grodno", true),
    (1, "Minsk", true),
    (2, "Hamburg", false),
    (3, "Brest", true),
];

let table = Table::new(&data)
               .with(Format::config(|cfg: &mut ColoredConfig| cfg.set_justification((0,1).into(), '.')))
               .to_string();

assert_eq!(
    table,
    "+-----+---------+-------+\n\
     | i32 | &str... | bool  |\n\
     +-----+---------+-------+\n\
     | 0   | Grodno  | true  |\n\
     +-----+---------+-------+\n\
     | 1   | Minsk   | true  |\n\
     +-----+---------+-------+\n\
     | 2   | Hamburg | false |\n\
     +-----+---------+-------+\n\
     | 3   | Brest   | true  |\n\
     +-----+---------+-------+"
);

Trait Implementations§

Source§

impl Debug for Format

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Format

§

impl RefUnwindSafe for Format

§

impl Send for Format

§

impl Sync for Format

§

impl Unpin for Format

§

impl UnwindSafe for Format

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>,

Source§

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>,

Source§

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.