tabled/settings/format/mod.rs
1//! This module contains a list of primitives to help to modify a [`Table`].
2//!
3//! [`Table`]: crate::Table
4
5mod format_config;
6mod format_content;
7mod format_positioned;
8
9pub use format_config::FormatConfig;
10pub use format_content::FormatContent;
11pub use format_positioned::FormatContentPositioned;
12use papergrid::config::Position;
13
14/// A formatting function of particular cells on a [`Table`].
15///
16/// [`Table`]: crate::Table
17#[derive(Debug)]
18pub struct Format;
19
20impl Format {
21 /// This function creates a new [`Format`] instance, so
22 /// it can be used as a grid setting.
23 ///
24 /// # Example
25 ///
26 /// ```
27 /// use tabled::{Table, settings::{Format, object::Rows, Modify}};
28 ///
29 /// let data = vec![
30 /// (0, "Grodno", true),
31 /// (1, "Minsk", true),
32 /// (2, "Hamburg", false),
33 /// (3, "Brest", true),
34 /// ];
35 ///
36 /// let table = Table::new(&data)
37 /// .with(Modify::new(Rows::new(1..)).with(Format::content(|s| format!(": {} :", s))))
38 /// .to_string();
39 ///
40 /// assert_eq!(
41 /// table,
42 /// "+-------+-------------+-----------+\n\
43 /// | i32 | &str | bool |\n\
44 /// +-------+-------------+-----------+\n\
45 /// | : 0 : | : Grodno : | : true : |\n\
46 /// +-------+-------------+-----------+\n\
47 /// | : 1 : | : Minsk : | : true : |\n\
48 /// +-------+-------------+-----------+\n\
49 /// | : 2 : | : Hamburg : | : false : |\n\
50 /// +-------+-------------+-----------+\n\
51 /// | : 3 : | : Brest : | : true : |\n\
52 /// +-------+-------------+-----------+"
53 /// );
54 /// ```
55 pub fn content<F>(f: F) -> FormatContent<F>
56 where
57 F: FnMut(&str) -> String,
58 {
59 FormatContent::new(f)
60 }
61
62 /// This function creates a new [`FormatContentPositioned`], so
63 /// it can be used as a grid setting.
64 ///
65 /// It's different from [`Format::content`] as it also provides a row and column index.
66 ///
67 /// # Example
68 ///
69 /// ```
70 /// use tabled::{Table, settings::{Format, object::Rows, Modify}};
71 ///
72 /// let data = vec![
73 /// (0, "Grodno", true),
74 /// (1, "Minsk", true),
75 /// (2, "Hamburg", false),
76 /// (3, "Brest", true),
77 /// ];
78 ///
79 /// let table = Table::new(&data)
80 /// .modify(Rows::one(0), Format::positioned(|_, p| p.col.to_string()))
81 /// .to_string();
82 ///
83 /// assert_eq!(
84 /// table,
85 /// "+---+---------+-------+\n\
86 /// | 0 | 1 | 2 |\n\
87 /// +---+---------+-------+\n\
88 /// | 0 | Grodno | true |\n\
89 /// +---+---------+-------+\n\
90 /// | 1 | Minsk | true |\n\
91 /// +---+---------+-------+\n\
92 /// | 2 | Hamburg | false |\n\
93 /// +---+---------+-------+\n\
94 /// | 3 | Brest | true |\n\
95 /// +---+---------+-------+"
96 /// );
97 /// ```
98 pub fn positioned<F>(f: F) -> FormatContentPositioned<F>
99 where
100 F: FnMut(&str, Position) -> String,
101 {
102 FormatContentPositioned::new(f)
103 }
104
105 /// This function creates [`FormatConfig`] function to modify a table config.
106 ///
107 /// # Example
108 ///
109 /// ```
110 /// use tabled::{
111 /// Table,
112 /// settings::{Format, object::Rows, Modify},
113 /// grid::config::ColoredConfig,
114 /// };
115 ///
116 /// let data = vec![
117 /// (0, "Grodno", true),
118 /// (1, "Minsk", true),
119 /// (2, "Hamburg", false),
120 /// (3, "Brest", true),
121 /// ];
122 ///
123 /// let table = Table::new(&data)
124 /// .with(Format::config(|cfg: &mut ColoredConfig| cfg.set_justification((0,1).into(), '.')))
125 /// .to_string();
126 ///
127 /// assert_eq!(
128 /// table,
129 /// "+-----+---------+-------+\n\
130 /// | i32 | &str... | bool |\n\
131 /// +-----+---------+-------+\n\
132 /// | 0 | Grodno | true |\n\
133 /// +-----+---------+-------+\n\
134 /// | 1 | Minsk | true |\n\
135 /// +-----+---------+-------+\n\
136 /// | 2 | Hamburg | false |\n\
137 /// +-----+---------+-------+\n\
138 /// | 3 | Brest | true |\n\
139 /// +-----+---------+-------+"
140 /// );
141 /// ```
142 pub fn config<F>(f: F) -> FormatConfig<F> {
143 FormatConfig(f)
144 }
145}
146
147// todo: Add a lambda with all arguments