tabled/settings/height/
table_height_increase.rs

1use crate::{
2    grid::{
3        config::{ColoredConfig, Entity},
4        dimension::CompleteDimension,
5        records::{ExactRecords, IntoRecords, PeekableRecords, Records},
6    },
7    settings::{
8        measurement::Measurement,
9        peaker::{Peaker, PriorityNone},
10        Height, TableOption,
11    },
12};
13
14use super::util::get_table_height;
15
16/// A modification of a table to increase the table height.
17#[derive(Debug, Clone)]
18pub struct TableHeightIncrease<W = usize, P = PriorityNone> {
19    height: W,
20    priority: P,
21}
22
23impl<W> TableHeightIncrease<W, PriorityNone> {
24    /// Creates a new object.
25    pub fn new(height: W) -> Self
26    where
27        W: Measurement<Height>,
28    {
29        Self {
30            height,
31            priority: PriorityNone::default(),
32        }
33    }
34
35    /// Sets a different priority logic.
36    pub fn priority<P>(self, priority: P) -> TableHeightIncrease<W, P>
37    where
38        P: Peaker,
39    {
40        TableHeightIncrease {
41            priority,
42            height: self.height,
43        }
44    }
45}
46
47impl<R, W, P> TableOption<R, ColoredConfig, CompleteDimension> for TableHeightIncrease<W, P>
48where
49    W: Measurement<Height>,
50    P: Peaker + Clone,
51    R: Records + ExactRecords + PeekableRecords,
52    for<'a> &'a R: Records,
53    for<'a> <<&'a R as Records>::Iter as IntoRecords>::Cell: AsRef<str>,
54{
55    fn change(self, records: &mut R, cfg: &mut ColoredConfig, dims: &mut CompleteDimension) {
56        if records.count_rows() == 0 || records.count_columns() == 0 {
57            return;
58        }
59
60        let height = self.height.measure(&*records, cfg);
61        let (total, mut heights) = get_table_height(&*records, cfg);
62        if total >= height {
63            return;
64        }
65
66        get_increase_list(&mut heights, height, total, self.priority);
67
68        dims.set_heights(heights);
69    }
70
71    fn hint_change(&self) -> Option<Entity> {
72        // NOTE: We set correct heights and did not touched widths
73        None
74    }
75}
76
77fn get_increase_list<P>(list: &mut [usize], total: usize, mut current: usize, mut peaker: P)
78where
79    P: Peaker,
80{
81    while current != total {
82        let col = match peaker.peak(&[], list) {
83            Some(col) => col,
84            None => break,
85        };
86
87        list[col] += 1;
88        current += 1;
89    }
90}