tabled/features/height/
table_height_increase.rs

1use papergrid::records::Records;
2
3use crate::{
4    measurment::Measurment,
5    peaker::{Peaker, PriorityNone},
6    Height, Table, TableOption,
7};
8
9use super::get_table_total_height2;
10
11/// A modification of a table to increase the table height.
12#[derive(Debug, Clone)]
13pub struct TableHeightIncrease<W = usize, P = PriorityNone> {
14    height: W,
15    priority: P,
16}
17
18impl<W> TableHeightIncrease<W, PriorityNone> {
19    /// Creates a new object.
20    pub fn new(height: W) -> Self
21    where
22        W: Measurment<Height>,
23    {
24        Self {
25            height,
26            priority: PriorityNone::default(),
27        }
28    }
29
30    /// Sets a different priority logic.
31    pub fn priority<P>(self) -> TableHeightIncrease<W, P>
32    where
33        P: Peaker,
34    {
35        TableHeightIncrease {
36            priority: P::create(),
37            height: self.height,
38        }
39    }
40}
41
42impl<R, W, P> TableOption<R> for TableHeightIncrease<W, P>
43where
44    R: Records,
45    W: Measurment<Height>,
46    P: Peaker + Clone,
47{
48    fn change(&mut self, table: &mut Table<R>) {
49        if table.is_empty() {
50            return;
51        }
52
53        let height = self.height.measure(table.get_records(), table.get_config());
54        let (total, heights) = get_table_total_height2(table.get_records(), table.get_config());
55        if total >= height {
56            return;
57        }
58
59        increase_total_height(table, heights, total, height, self.priority.clone());
60    }
61}
62
63fn increase_total_height<P, R>(
64    table: &mut Table<R>,
65    mut list: Vec<usize>,
66    total: usize,
67    expected: usize,
68    priority: P,
69) where
70    P: Peaker,
71    R: Records,
72{
73    get_increase_list(&mut list, expected, total, priority);
74    table.cache_height(list);
75}
76
77fn get_increase_list<P>(list: &mut [usize], total: usize, mut value: usize, mut peaker: P)
78where
79    P: Peaker,
80{
81    while value != total {
82        let col = match peaker.peak(&[], list) {
83            Some(col) => col,
84            None => break,
85        };
86
87        list[col] += 1;
88        value += 1;
89    }
90}