Struct tabled::settings::format::Format

source ·
pub struct Format;
Available on crate feature std only.
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, (usize, usize)) -> 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)
               .with(Modify::new(Rows::single(0)).with(Format::positioned(|_, (_, col)| 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§

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.