tabled/features/height/
height_list.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
use std::iter::FromIterator;

use papergrid::records::Records;

use crate::{Table, TableOption};

/// A structure used to set [`Table`] height via a list of rows heights.
#[derive(Debug)]
pub struct HeightList {
    list: Vec<usize>,
}

impl HeightList {
    /// Creates a new object.
    pub fn new(list: Vec<usize>) -> Self {
        Self { list }
    }
}

impl From<Vec<usize>> for HeightList {
    fn from(list: Vec<usize>) -> Self {
        Self::new(list)
    }
}

impl FromIterator<usize> for HeightList {
    fn from_iter<T>(iter: T) -> Self
    where
        T: IntoIterator<Item = usize>,
    {
        Self::new(iter.into_iter().collect())
    }
}

impl<R> TableOption<R> for HeightList
where
    R: Records,
{
    fn change(&mut self, table: &mut Table<R>) {
        if self.list.len() < table.count_rows() {
            return;
        }

        table.cache_height(self.list.clone());
        table.destroy_width_cache();
    }
}