tabled/features/height/
table_height_increase.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
use papergrid::records::Records;

use crate::{
    measurment::Measurment,
    peaker::{Peaker, PriorityNone},
    Height, Table, TableOption,
};

use super::get_table_total_height2;

/// A modification of a table to increase the table height.
#[derive(Debug, Clone)]
pub struct TableHeightIncrease<W = usize, P = PriorityNone> {
    height: W,
    priority: P,
}

impl<W> TableHeightIncrease<W, PriorityNone> {
    /// Creates a new object.
    pub fn new(height: W) -> Self
    where
        W: Measurment<Height>,
    {
        Self {
            height,
            priority: PriorityNone::default(),
        }
    }

    /// Sets a different priority logic.
    pub fn priority<P>(self) -> TableHeightIncrease<W, P>
    where
        P: Peaker,
    {
        TableHeightIncrease {
            priority: P::create(),
            height: self.height,
        }
    }
}

impl<R, W, P> TableOption<R> for TableHeightIncrease<W, P>
where
    R: Records,
    W: Measurment<Height>,
    P: Peaker + Clone,
{
    fn change(&mut self, table: &mut Table<R>) {
        if table.is_empty() {
            return;
        }

        let height = self.height.measure(table.get_records(), table.get_config());
        let (total, heights) = get_table_total_height2(table.get_records(), table.get_config());
        if total >= height {
            return;
        }

        increase_total_height(table, heights, total, height, self.priority.clone());
    }
}

fn increase_total_height<P, R>(
    table: &mut Table<R>,
    mut list: Vec<usize>,
    total: usize,
    expected: usize,
    priority: P,
) where
    P: Peaker,
    R: Records,
{
    get_increase_list(&mut list, expected, total, priority);
    table.cache_height(list);
}

fn get_increase_list<P>(list: &mut [usize], total: usize, mut value: usize, mut peaker: P)
where
    P: Peaker,
{
    while value != total {
        let col = match peaker.peak(&[], list) {
            Some(col) => col,
            None => break,
        };

        list[col] += 1;
        value += 1;
    }
}