tabled/settings/width/
width_list.rs

1use std::iter::FromIterator;
2
3use crate::{
4    grid::{config::Entity, dimension::CompleteDimension, records::Records},
5    settings::TableOption,
6};
7
8/// A structure used to set [`Table`] width via a list of columns widths.
9///
10/// [`Table`]: crate::Table
11#[derive(Debug)]
12pub struct WidthList {
13    list: Vec<usize>,
14}
15
16impl WidthList {
17    /// Creates a new object.
18    pub fn new(list: Vec<usize>) -> Self {
19        Self { list }
20    }
21}
22
23impl From<Vec<usize>> for WidthList {
24    fn from(list: Vec<usize>) -> Self {
25        Self::new(list)
26    }
27}
28
29impl FromIterator<usize> for WidthList {
30    fn from_iter<T: IntoIterator<Item = usize>>(iter: T) -> Self {
31        Self::new(iter.into_iter().collect())
32    }
33}
34
35impl<R, C> TableOption<R, C, CompleteDimension> for WidthList
36where
37    R: Records,
38{
39    fn change(self, records: &mut R, _: &mut C, dimension: &mut CompleteDimension) {
40        if self.list.len() < records.count_columns() {
41            return;
42        }
43
44        dimension.set_widths(self.list);
45    }
46
47    fn hint_change(&self) -> Option<Entity> {
48        // NOTE: is this correct?
49        None
50    }
51}
52
53// TODO: I'd rework it to support percent?
54//       This one is not very usefull AT ALL