From ef24de24a82fe681581cc130f342363c47c0969a Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 7 Jun 2024 07:48:48 +0200 Subject: Merging upstream version 1.75.0+dfsg1. Signed-off-by: Daniel Baumann --- vendor/tabled/src/settings/measurement/mod.rs | 161 ++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 vendor/tabled/src/settings/measurement/mod.rs (limited to 'vendor/tabled/src/settings/measurement') diff --git a/vendor/tabled/src/settings/measurement/mod.rs b/vendor/tabled/src/settings/measurement/mod.rs new file mode 100644 index 000000000..f93cc6b4e --- /dev/null +++ b/vendor/tabled/src/settings/measurement/mod.rs @@ -0,0 +1,161 @@ +//! The module contains [`Measurement`] trait and its implementations to be used in [`Height`] and [`Width`].; + +use crate::{ + grid::config::SpannedConfig, + grid::dimension::SpannedGridDimension, + grid::records::{ExactRecords, PeekableRecords, Records}, + grid::util::string::{self, string_width_multiline}, + settings::{Height, Width}, +}; + +/// A width value which can be obtained on behalf of [`Table`]. +/// +/// [`Table`]: crate::Table +pub trait Measurement { + /// Returns a measurement value. + fn measure( + &self, + records: R, + cfg: &SpannedConfig, + ) -> usize; +} + +impl Measurement for usize { + fn measure(&self, _: R, _: &SpannedConfig) -> usize { + *self + } +} + +/// Max width value. +#[derive(Debug)] +pub struct Max; + +impl Measurement for Max { + fn measure( + &self, + records: R, + _: &SpannedConfig, + ) -> usize { + grid_widths(&records) + .map(|r| r.max().unwrap_or(0)) + .max() + .unwrap_or(0) + } +} + +impl Measurement for Max { + fn measure( + &self, + records: R, + _: &SpannedConfig, + ) -> usize { + records_heights(&records) + .map(|r| r.max().unwrap_or(0)) + .max() + .unwrap_or(0) + } +} + +/// Min width value. +#[derive(Debug)] +pub struct Min; + +impl Measurement for Min { + fn measure( + &self, + records: R, + _: &SpannedConfig, + ) -> usize { + grid_widths(&records) + .map(|r| r.min().unwrap_or(0)) + .max() + .unwrap_or(0) + } +} + +impl Measurement for Min { + fn measure( + &self, + records: R, + _: &SpannedConfig, + ) -> usize { + records_heights(&records) + .map(|r| r.max().unwrap_or(0)) + .min() + .unwrap_or(0) + } +} + +/// Percent from a total table width. +#[derive(Debug)] +pub struct Percent(pub usize); + +impl Measurement for Percent { + fn measure(&self, records: R, cfg: &SpannedConfig) -> usize + where + R: Records, + { + let (_, total) = get_table_widths_with_total(records, cfg); + (total * self.0) / 100 + } +} + +impl Measurement for Percent { + fn measure(&self, records: R, cfg: &SpannedConfig) -> usize + where + R: Records + ExactRecords, + { + let (_, total) = get_table_heights_width_total(records, cfg); + (total * self.0) / 100 + } +} + +fn grid_widths( + records: &R, +) -> impl Iterator + '_> + '_ { + let (count_rows, count_cols) = (records.count_rows(), records.count_columns()); + (0..count_rows).map(move |row| { + (0..count_cols).map(move |col| string_width_multiline(records.get_text((row, col)))) + }) +} + +fn get_table_widths_with_total(records: R, cfg: &SpannedConfig) -> (Vec, usize) +where + R: Records, +{ + let widths = SpannedGridDimension::width(records, cfg); + let total_width = get_table_total_width(&widths, cfg); + (widths, total_width) +} + +fn get_table_total_width(list: &[usize], cfg: &SpannedConfig) -> usize { + let total = list.iter().sum::(); + + total + cfg.count_vertical(list.len()) +} + +fn records_heights(records: &R) -> impl Iterator + '_> + '_ +where + R: Records + ExactRecords + PeekableRecords, +{ + (0..records.count_rows()).map(move |row| { + (0..records.count_columns()) + .map(move |col| string::count_lines(records.get_text((row, col)))) + }) +} + +fn get_table_heights_width_total(records: R, cfg: &SpannedConfig) -> (Vec, usize) +where + R: Records, +{ + let list = SpannedGridDimension::height(records, cfg); + let total = get_table_total_height(&list, cfg); + (list, total) +} + +fn get_table_total_height(list: &[usize], cfg: &SpannedConfig) -> usize { + let total = list.iter().sum::(); + let counth = cfg.count_horizontal(list.len()); + + total + counth +} -- cgit v1.2.3