tabled/settings/height/
mod.rs

1//! The module contains [`Height`] structure which is responsible for a table and cell height.
2
3mod cell_height_increase;
4mod cell_height_limit;
5mod height_list;
6mod table_height_increase;
7mod table_height_limit;
8mod util;
9
10use crate::settings::measurement::Measurement;
11
12pub use cell_height_increase::CellHeightIncrease;
13pub use cell_height_limit::CellHeightLimit;
14pub use height_list::HeightList;
15pub use table_height_increase::TableHeightIncrease;
16pub use table_height_limit::TableHeightLimit;
17
18/// Height is a abstract factory for height settings.
19///
20/// # Example
21///
22/// ```
23/// use tabled::{Table, settings::{Height, Settings}};
24///
25/// let data = vec![
26///     ("Some data", "here", "and here"),
27///     ("Some data on a next", "line", "right here"),
28/// ];
29///
30/// let table = Table::new(data)
31///     .with(Settings::new(Height::limit(10), Height::increase(10)))
32///     .to_string();
33///
34/// assert_eq!(
35///     table,
36///     "+---------------------+------+------------+\n\
37///      | &str                | &str | &str       |\n\
38///      |                     |      |            |\n\
39///      +---------------------+------+------------+\n\
40///      | Some data           | here | and here   |\n\
41///      |                     |      |            |\n\
42///      +---------------------+------+------------+\n\
43///      | Some data on a next | line | right here |\n\
44///      |                     |      |            |\n\
45///      +---------------------+------+------------+",
46/// )
47/// ```
48#[derive(Debug)]
49pub struct Height;
50
51impl Height {
52    /// Create [`CellHeightIncrease`] to set a table/cell height.
53    ///
54    /// # Example
55    ///
56    /// ## Cell height
57    ///
58    /// ```
59    /// use tabled::{Table, settings::{Height, Modify, object::Columns}};
60    ///
61    /// let data = vec![
62    ///     ("Some data", "here", "and here"),
63    ///     ("Some data on a next", "line", "right here"),
64    /// ];
65    ///
66    /// let table = Table::new(data)
67    ///     .with(Modify::new(Columns::first()).with(Height::increase(5)))
68    ///     .to_string();
69    ///
70    /// assert_eq!(
71    ///     table,
72    ///     "+---------------------+------+------------+\n\
73    ///      | &str                | &str | &str       |\n\
74    ///      |                     |      |            |\n\
75    ///      |                     |      |            |\n\
76    ///      |                     |      |            |\n\
77    ///      |                     |      |            |\n\
78    ///      +---------------------+------+------------+\n\
79    ///      | Some data           | here | and here   |\n\
80    ///      |                     |      |            |\n\
81    ///      |                     |      |            |\n\
82    ///      |                     |      |            |\n\
83    ///      |                     |      |            |\n\
84    ///      +---------------------+------+------------+\n\
85    ///      | Some data on a next | line | right here |\n\
86    ///      |                     |      |            |\n\
87    ///      |                     |      |            |\n\
88    ///      |                     |      |            |\n\
89    ///      |                     |      |            |\n\
90    ///      +---------------------+------+------------+"
91    /// )
92    /// ```
93    ///
94    /// ## Table height
95    ///
96    /// ```
97    /// use tabled::{Table, settings::Height};
98    ///
99    /// let data = vec![
100    ///     ("Some data", "here", "and here"),
101    ///     ("Some data on a next", "line", "right here"),
102    /// ];
103    ///
104    /// let table = Table::new(data)
105    ///     .with(Height::increase(10))
106    ///     .to_string();
107    ///
108    /// assert_eq!(
109    ///     table,
110    ///     "+---------------------+------+------------+\n\
111    ///      | &str                | &str | &str       |\n\
112    ///      |                     |      |            |\n\
113    ///      +---------------------+------+------------+\n\
114    ///      | Some data           | here | and here   |\n\
115    ///      |                     |      |            |\n\
116    ///      +---------------------+------+------------+\n\
117    ///      | Some data on a next | line | right here |\n\
118    ///      |                     |      |            |\n\
119    ///      +---------------------+------+------------+",
120    /// )
121    /// ```
122    pub fn increase<W: Measurement<Height>>(height: W) -> CellHeightIncrease<W> {
123        CellHeightIncrease::new(height)
124    }
125
126    /// Create [`CellHeightLimit`] to set a table/cell height.
127    ///
128    /// # Example
129    ///
130    /// ## Cell height
131    ///
132    /// ```
133    /// use tabled::{Table, settings::{Height, Modify, object::Columns}};
134    ///
135    /// let data = vec![
136    ///     ("Some\ndata", "here", "and here"),
137    ///     ("Some\ndata on a next", "line", "right here"),
138    /// ];
139    ///
140    /// let table = Table::new(data)
141    ///     .with(Modify::new(Columns::first()).with(Height::limit(1)))
142    ///     .to_string();
143    ///
144    /// assert_eq!(
145    ///     table,
146    ///     "+------+------+------------+\n\
147    ///      | &str | &str | &str       |\n\
148    ///      +------+------+------------+\n\
149    ///      | Some | here | and here   |\n\
150    ///      +------+------+------------+\n\
151    ///      | Some | line | right here |\n\
152    ///      +------+------+------------+"
153    /// )
154    /// ```
155    ///
156    /// ## Table height
157    ///
158    /// ```
159    /// use tabled::{Table, settings::Height};
160    ///
161    /// let data = vec![
162    ///     ("Some\ndata", "here", "and here"),
163    ///     ("Some\ndata on a next", "line", "right here"),
164    /// ];
165    ///
166    /// let table = Table::new(&data)
167    ///     .with(Height::limit(6))
168    ///     .to_string();
169    ///
170    /// assert_eq!(
171    ///     table,
172    ///     "+------+------+------------+\n\
173    ///      +------+------+------------+\n\
174    ///      | Some | here | and here   |\n\
175    ///      +------+------+------------+\n\
176    ///      | Some | line | right here |\n\
177    ///      +------+------+------------+",
178    /// );
179    ///
180    /// let table = Table::new(&data)
181    ///     .with(Height::limit(1))
182    ///     .to_string();
183    ///
184    /// assert_eq!(
185    ///     table,
186    ///     "+--+--+--+\n\
187    ///      +--+--+--+\n\
188    ///      +--+--+--+\n\
189    ///      +--+--+--+",
190    /// );
191    /// ```
192    pub fn limit<W: Measurement<Height>>(height: W) -> CellHeightLimit<W> {
193        CellHeightLimit::new(height)
194    }
195
196    /// Create [`HeightList`] to set a table height to a constant list of row heights.
197    ///
198    /// Notice if you provide a list with `.len()` less than `Table::count_rows` then it will have no affect.
199    ///
200    /// # Example
201    ///
202    /// ```
203    /// use tabled::{Table, settings::{Height, Modify, object::Columns}};
204    ///
205    /// let data = vec![
206    ///     ("Some\ndata", "here", "and here"),
207    ///     ("Some\ndata on a next", "line", "right here"),
208    /// ];
209    ///
210    /// let table = Table::new(data)
211    ///     .with(Height::list([1, 0, 2]))
212    ///     .to_string();
213    ///
214    /// assert_eq!(
215    ///     table,
216    ///     "+----------------+------+------------+\n\
217    ///      | &str           | &str | &str       |\n\
218    ///      +----------------+------+------------+\n\
219    ///      +----------------+------+------------+\n\
220    ///      | Some           | line | right here |\n\
221    ///      | data on a next |      |            |\n\
222    ///      +----------------+------+------------+",
223    /// )
224    /// ```
225    pub fn list<I: IntoIterator<Item = usize>>(rows: I) -> HeightList {
226        HeightList::new(rows.into_iter().collect())
227    }
228}