1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
//! This module contains a list of primitives to help to modify a [`Table`].
//!
//! [`Table`]: crate::Table
mod format_config;
mod format_content;
mod format_positioned;
pub use format_config::FormatConfig;
pub use format_content::FormatContent;
pub use format_positioned::FormatContentPositioned;
/// A formatting function of particular cells on a [`Table`].
///
/// [`Table`]: crate::Table
#[derive(Debug)]
pub struct Format;
impl Format {
/// 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\
/// +-------+-------------+-----------+"
/// );
/// ```
pub fn content<F>(f: F) -> FormatContent<F>
where
F: FnMut(&str) -> String,
{
FormatContent::new(f)
}
/// 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\
/// +---+---------+-------+"
/// );
/// ```
pub fn positioned<F>(f: F) -> FormatContentPositioned<F>
where
F: FnMut(&str, (usize, usize)) -> String,
{
FormatContentPositioned::new(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\
/// +-----+---------+-------+"
/// );
/// ```
pub fn config<F>(f: F) -> FormatConfig<F> {
FormatConfig(f)
}
}