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