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