1 | use std::borrow::Cow; |
2 | |
3 | use papergrid::{ |
4 | dimension::spanned_vec_records::SpannedVecRecordsDimension, records::vec_records::VecRecords, |
5 | }; |
6 | |
7 | use crate::grid::{ |
8 | config::{ColoredConfig, SpannedConfig}, |
9 | dimension::{Dimension, Estimate}, |
10 | records::vec_records::Cell, |
11 | }; |
12 | |
13 | /// CompleteDimension is a [`Dimension`] implementation for a [`Table`] |
14 | /// |
15 | /// [`Table`]: crate::Table |
16 | #[derive (Debug, Default, PartialEq, Eq, PartialOrd, Ord, Clone)] |
17 | pub struct CompleteDimensionVecRecords<'a> { |
18 | width: Option<Cow<'a, [usize]>>, |
19 | height: Option<Cow<'a, [usize]>>, |
20 | } |
21 | |
22 | impl CompleteDimensionVecRecords<'_> { |
23 | /// Checks whether is the dimensions is set. |
24 | pub fn is_complete(&self) -> bool { |
25 | self.width.is_some() && self.height.is_some() |
26 | } |
27 | |
28 | /// Checks whether is nothing was set. |
29 | pub fn is_empty(&self) -> bool { |
30 | self.width.is_none() && self.height.is_none() |
31 | } |
32 | |
33 | /// Set column widths. |
34 | /// |
35 | /// In general the method is only considered to be useful to a [`TableOption`]. |
36 | /// |
37 | /// BE CAREFUL WITH THIS METHOD as it supposed that the content is not bigger than the provided widths. |
38 | /// |
39 | /// [`TableOption`]: crate::settings::TableOption |
40 | pub fn set_widths(&mut self, columns: Vec<usize>) -> bool { |
41 | self.width = Some(Cow::Owned(columns)); |
42 | |
43 | true |
44 | } |
45 | |
46 | /// Set rows heights. |
47 | /// |
48 | /// In general the method is only considered to be useful to a [`TableOption`]. |
49 | /// |
50 | /// BE CAREFUL WITH THIS METHOD as it supposed that the content is not bigger than the provided heights. |
51 | /// |
52 | /// [`TableOption`]: crate::settings::TableOption |
53 | pub fn set_heights(&mut self, rows: Vec<usize>) -> bool { |
54 | self.height = Some(Cow::Owned(rows)); |
55 | |
56 | true |
57 | } |
58 | |
59 | /// Force width estimation. |
60 | pub fn clear_width(&mut self) { |
61 | self.width = None; |
62 | } |
63 | |
64 | /// Force height estimation. |
65 | pub fn clear_height(&mut self) { |
66 | self.height = None; |
67 | } |
68 | |
69 | /// Copies a reference from self. |
70 | pub fn from_origin(&self) -> CompleteDimensionVecRecords<'_> { |
71 | let width = self.width.as_deref().map(Cow::Borrowed); |
72 | let height = self.height.as_deref().map(Cow::Borrowed); |
73 | |
74 | CompleteDimensionVecRecords { width, height } |
75 | } |
76 | } |
77 | |
78 | impl Dimension for CompleteDimensionVecRecords<'_> { |
79 | fn get_width(&self, column: usize) -> usize { |
80 | let width: &Cow<'_, [usize]> = self |
81 | .width |
82 | .as_ref() |
83 | .expect(msg:"It must always be Some at this point" ); |
84 | |
85 | width[column] |
86 | } |
87 | |
88 | fn get_height(&self, row: usize) -> usize { |
89 | let height: &Cow<'_, [usize]> = self |
90 | .height |
91 | .as_ref() |
92 | .expect(msg:"It must always be Some at this point" ); |
93 | |
94 | height[row] |
95 | } |
96 | } |
97 | |
98 | impl<T: AsRef<str> + Cell> Estimate<&VecRecords<T>, SpannedConfig> |
99 | for CompleteDimensionVecRecords<'_> |
100 | { |
101 | fn estimate(&mut self, records: &VecRecords<T>, cfg: &SpannedConfig) { |
102 | match (self.width.is_some(), self.height.is_some()) { |
103 | (true, true) => {} |
104 | (true, false) => { |
105 | self.height = Some(Cow::Owned(SpannedVecRecordsDimension::height(records, cfg))); |
106 | } |
107 | (false, true) => { |
108 | self.width = Some(Cow::Owned(SpannedVecRecordsDimension::width(records, cfg))); |
109 | } |
110 | (false, false) => { |
111 | let mut dims: SpannedVecRecordsDimension = SpannedVecRecordsDimension::default(); |
112 | dims.estimate(records, config:cfg); |
113 | |
114 | let (width: Vec, height: Vec) = dims.get_values(); |
115 | self.width = Some(Cow::Owned(width)); |
116 | self.height = Some(Cow::Owned(height)); |
117 | } |
118 | } |
119 | } |
120 | } |
121 | |
122 | impl<T: AsRef<str> + Cell> Estimate<&VecRecords<T>, ColoredConfig> |
123 | for CompleteDimensionVecRecords<'_> |
124 | { |
125 | fn estimate(&mut self, records: &VecRecords<T>, cfg: &ColoredConfig) { |
126 | self.estimate(records, config:cfg.as_ref()) |
127 | } |
128 | } |
129 | |