tabled/features/width/
width_list.rs

1use std::iter::FromIterator;
2
3use papergrid::records::Records;
4
5use crate::{Table, TableOption};
6
7/// A structure used to set [`Table`] width via a list of columns widths.
8#[derive(Debug)]
9pub struct WidthList {
10    list: Vec<usize>,
11}
12
13impl WidthList {
14    /// Creates a new object.
15    pub fn new(list: Vec<usize>) -> Self {
16        Self { list }
17    }
18}
19
20impl From<Vec<usize>> for WidthList {
21    fn from(list: Vec<usize>) -> Self {
22        Self::new(list)
23    }
24}
25
26impl FromIterator<usize> for WidthList {
27    fn from_iter<T>(iter: T) -> Self
28    where
29        T: IntoIterator<Item = usize>,
30    {
31        Self::new(iter.into_iter().collect())
32    }
33}
34
35impl<R> TableOption<R> for WidthList
36where
37    R: Records,
38{
39    fn change(&mut self, table: &mut Table<R>) {
40        if self.list.len() < table.count_columns() {
41            return;
42        }
43
44        table.cache_width(self.list.clone());
45        table.destroy_height_cache();
46    }
47}