tabled/features/height/
mod.rs

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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//! The module contains [`Height`] structure which is responsible for a table and cell height.

use papergrid::{height::HeightEstimator, records::Records, Estimate, GridConfig};

use crate::measurment::Measurment;

mod cell_height_increase;
mod cell_height_limit;
mod height_list;
mod table_height_increase;
mod table_height_limit;

pub use cell_height_increase::CellHeightIncrease;
pub use cell_height_limit::CellHeightLimit;
pub use height_list::HeightList;
pub use table_height_increase::TableHeightIncrease;
pub use table_height_limit::TableHeightLimit;

/// Height is a abstract factory for height settings.
///
/// # Example
///
/// ```
/// use tabled::{Table, Height};
///
/// let data = vec![
///     ("Some data", "here", "and here"),
///     ("Some data on a next", "line", "right here"),
/// ];
///
/// let table = Table::new(data)
///     .with(Height::increase(10))
///     .with(Height::limit(10))
///     .to_string();
///
/// assert_eq!(
///     table,
///     "+---------------------+------+------------+\n\
///      | &str                | &str | &str       |\n\
///      |                     |      |            |\n\
///      +---------------------+------+------------+\n\
///      | Some data           | here | and here   |\n\
///      |                     |      |            |\n\
///      +---------------------+------+------------+\n\
///      | Some data on a next | line | right here |\n\
///      |                     |      |            |\n\
///      +---------------------+------+------------+",
/// )
/// ```
#[derive(Debug)]
pub struct Height;

impl Height {
    /// Create [`CellHeightIncrease`] to set a table/cell height.
    ///
    /// # Example
    ///
    /// ## Cell height
    ///
    /// ```
    /// use tabled::{Table, Height, Modify, object::Columns};
    ///
    /// let data = vec![
    ///     ("Some data", "here", "and here"),
    ///     ("Some data on a next", "line", "right here"),
    /// ];
    ///
    /// let table = Table::new(data)
    ///     .with(Modify::new(Columns::first()).with(Height::increase(5)))
    ///     .to_string();
    ///
    /// assert_eq!(
    ///     table,
    ///     "+---------------------+------+------------+\n\
    ///      | &str                | &str | &str       |\n\
    ///      |                     |      |            |\n\
    ///      |                     |      |            |\n\
    ///      |                     |      |            |\n\
    ///      |                     |      |            |\n\
    ///      +---------------------+------+------------+\n\
    ///      | Some data           | here | and here   |\n\
    ///      |                     |      |            |\n\
    ///      |                     |      |            |\n\
    ///      |                     |      |            |\n\
    ///      |                     |      |            |\n\
    ///      +---------------------+------+------------+\n\
    ///      | Some data on a next | line | right here |\n\
    ///      |                     |      |            |\n\
    ///      |                     |      |            |\n\
    ///      |                     |      |            |\n\
    ///      |                     |      |            |\n\
    ///      +---------------------+------+------------+"
    /// )
    /// ```
    ///
    /// ## Table height
    ///
    /// ```
    /// use tabled::{Table, Height};
    ///
    /// let data = vec![
    ///     ("Some data", "here", "and here"),
    ///     ("Some data on a next", "line", "right here"),
    /// ];
    ///
    /// let table = Table::new(data)
    ///     .with(Height::increase(10))
    ///     .to_string();
    ///
    /// assert_eq!(
    ///     table,
    ///     "+---------------------+------+------------+\n\
    ///      | &str                | &str | &str       |\n\
    ///      |                     |      |            |\n\
    ///      +---------------------+------+------------+\n\
    ///      | Some data           | here | and here   |\n\
    ///      |                     |      |            |\n\
    ///      +---------------------+------+------------+\n\
    ///      | Some data on a next | line | right here |\n\
    ///      |                     |      |            |\n\
    ///      +---------------------+------+------------+",
    /// )
    /// ```
    pub fn increase<W>(width: W) -> CellHeightIncrease<W>
    where
        W: Measurment<Height>,
    {
        CellHeightIncrease::new(width)
    }

    /// Create [`CellHeightLimit`] to set a table/cell height.
    ///
    /// # Example
    ///
    /// ## Cell height
    ///
    /// ```
    /// use tabled::{Table, Height, Modify, object::Columns};
    ///
    /// let data = vec![
    ///     ("Some\ndata", "here", "and here"),
    ///     ("Some\ndata on a next", "line", "right here"),
    /// ];
    ///
    /// let table = Table::new(data)
    ///     .with(Modify::new(Columns::first()).with(Height::limit(1)))
    ///     .to_string();
    ///
    /// assert_eq!(
    ///     table,
    ///     "+------+------+------------+\n\
    ///      | &str | &str | &str       |\n\
    ///      +------+------+------------+\n\
    ///      | Some | here | and here   |\n\
    ///      +------+------+------------+\n\
    ///      | Some | line | right here |\n\
    ///      +------+------+------------+"
    /// )
    /// ```
    ///
    /// ## Table height
    ///
    /// ```
    /// use tabled::{Table, Height};
    ///
    /// let data = vec![
    ///     ("Some\ndata", "here", "and here"),
    ///     ("Some\ndata on a next", "line", "right here"),
    /// ];
    ///
    /// let table = Table::new(&data)
    ///     .with(Height::limit(6))
    ///     .to_string();
    ///
    /// assert_eq!(
    ///     table,
    ///     "+----------------+------+------------+\n\
    ///      +----------------+------+------------+\n\
    ///      | Some           | here | and here   |\n\
    ///      +----------------+------+------------+\n\
    ///      | Some           | line | right here |\n\
    ///      +----------------+------+------------+",
    /// );
    ///
    /// let table = Table::new(&data)
    ///     .with(Height::limit(1))
    ///     .to_string();
    ///
    /// assert_eq!(
    ///     table,
    ///     "+----------------+------+------------+\n\
    ///      +----------------+------+------------+\n\
    ///      +----------------+------+------------+\n\
    ///      +----------------+------+------------+",
    /// );
    /// ```
    pub fn limit<W>(width: W) -> CellHeightLimit<W>
    where
        W: Measurment<Height>,
    {
        CellHeightLimit::new(width)
    }

    /// Create [`HeightList`] to set a table height to a constant list of row heights.
    ///
    /// Notice if you provide a list with `.len()` less than `Table::count_rows` then it will have no affect.
    ///
    /// # Example
    ///
    /// ```
    /// use tabled::{Table, Height, Modify, object::Columns};
    ///
    /// let data = vec![
    ///     ("Some\ndata", "here", "and here"),
    ///     ("Some\ndata on a next", "line", "right here"),
    /// ];
    ///
    /// let table = Table::new(data)
    ///     .with(Height::list([1, 0, 2]))
    ///     .to_string();
    ///
    /// assert_eq!(
    ///     table,
    ///     "+----------------+------+------------+\n\
    ///      | &str           | &str | &str       |\n\
    ///      +----------------+------+------------+\n\
    ///      +----------------+------+------------+\n\
    ///      | Some           | line | right here |\n\
    ///      | data on a next |      |            |\n\
    ///      +----------------+------+------------+",
    /// )
    /// ```
    pub fn list<I>(rows: I) -> HeightList
    where
        I: IntoIterator<Item = usize>,
    {
        HeightList::new(rows.into_iter().collect())
    }
}

pub(crate) fn get_table_total_height<R, E>(records: &R, cfg: &GridConfig, ctrl: &E) -> usize
where
    R: Records,
    E: Estimate<R>,
{
    ctrl.total()
        + cfg.count_horizontal(records.count_rows())
        + cfg.get_margin().top.size
        + cfg.get_margin().bottom.size
}

pub(crate) fn get_table_total_height2<R>(records: &R, cfg: &GridConfig) -> (usize, Vec<usize>)
where
    R: Records,
{
    let mut ctrl = HeightEstimator::default();
    ctrl.estimate(records, cfg);
    let total = <HeightEstimator as Estimate<R>>::total(&ctrl)
        + cfg.count_horizontal(records.count_rows())
        + cfg.get_margin().top.size
        + cfg.get_margin().bottom.size;

    (total, ctrl.into())
}