summaryrefslogtreecommitdiffstats
path: root/vendor/tabled/src/settings/height
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-07 05:48:48 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-07 05:48:48 +0000
commitef24de24a82fe681581cc130f342363c47c0969a (patch)
tree0d494f7e1a38b95c92426f58fe6eaa877303a86c /vendor/tabled/src/settings/height
parentReleasing progress-linux version 1.74.1+dfsg1-1~progress7.99u1. (diff)
downloadrustc-ef24de24a82fe681581cc130f342363c47c0969a.tar.xz
rustc-ef24de24a82fe681581cc130f342363c47c0969a.zip
Merging upstream version 1.75.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/tabled/src/settings/height')
-rw-r--r--vendor/tabled/src/settings/height/cell_height_increase.rs98
-rw-r--r--vendor/tabled/src/settings/height/cell_height_limit.rs103
-rw-r--r--vendor/tabled/src/settings/height/height_list.rs47
-rw-r--r--vendor/tabled/src/settings/height/mod.rs228
-rw-r--r--vendor/tabled/src/settings/height/table_height_increase.rs90
-rw-r--r--vendor/tabled/src/settings/height/table_height_limit.rs123
-rw-r--r--vendor/tabled/src/settings/height/util.rs22
7 files changed, 711 insertions, 0 deletions
diff --git a/vendor/tabled/src/settings/height/cell_height_increase.rs b/vendor/tabled/src/settings/height/cell_height_increase.rs
new file mode 100644
index 000000000..39887f9f6
--- /dev/null
+++ b/vendor/tabled/src/settings/height/cell_height_increase.rs
@@ -0,0 +1,98 @@
+use crate::{
+ grid::config::ColoredConfig,
+ grid::config::Entity,
+ grid::dimension::CompleteDimensionVecRecords,
+ grid::records::{ExactRecords, PeekableRecords, Records, RecordsMut},
+ grid::util::string::count_lines,
+ settings::{measurement::Measurement, peaker::Peaker, CellOption, Height, TableOption},
+};
+
+use super::TableHeightIncrease;
+
+/// A modification for cell/table to increase its height.
+///
+/// If used for a [`Table`] [`PriorityNone`] is used.
+///
+/// [`PriorityNone`]: crate::settings::peaker::PriorityNone
+/// [`Table`]: crate::Table
+#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
+pub struct CellHeightIncrease<W = usize> {
+ height: W,
+}
+
+impl<W> CellHeightIncrease<W> {
+ /// Creates a new object of the structure.
+ pub fn new(height: W) -> Self
+ where
+ W: Measurement<Height>,
+ {
+ Self { height }
+ }
+
+ /// The priority makes scence only for table, so the function
+ /// converts it to [`TableHeightIncrease`] with a given priority.
+ pub fn priority<P>(self) -> TableHeightIncrease<W, P>
+ where
+ P: Peaker,
+ W: Measurement<Height>,
+ {
+ TableHeightIncrease::new(self.height).priority::<P>()
+ }
+}
+
+impl<W, R> CellOption<R, ColoredConfig> for CellHeightIncrease<W>
+where
+ W: Measurement<Height>,
+ R: Records + ExactRecords + PeekableRecords + RecordsMut<String>,
+ for<'a> &'a R: Records,
+{
+ fn change(self, records: &mut R, cfg: &mut ColoredConfig, entity: Entity) {
+ let height = self.height.measure(&*records, cfg);
+
+ let count_rows = records.count_rows();
+ let count_columns = records.count_columns();
+
+ for pos in entity.iter(count_rows, count_columns) {
+ let is_valid_pos = pos.0 < count_rows && pos.1 < count_columns;
+ if !is_valid_pos {
+ continue;
+ }
+
+ let text = records.get_text(pos);
+
+ let cell_height = count_lines(text);
+ if cell_height >= height {
+ continue;
+ }
+
+ let content = add_lines(text, height - cell_height);
+ records.set(pos, content);
+ }
+ }
+}
+
+impl<R, W> TableOption<R, CompleteDimensionVecRecords<'static>, ColoredConfig>
+ for CellHeightIncrease<W>
+where
+ W: Measurement<Height>,
+ R: Records + ExactRecords + PeekableRecords,
+ for<'a> &'a R: Records,
+{
+ fn change(
+ self,
+ records: &mut R,
+ cfg: &mut ColoredConfig,
+ dims: &mut CompleteDimensionVecRecords<'static>,
+ ) {
+ let height = self.height.measure(&*records, cfg);
+ TableHeightIncrease::new(height).change(records, cfg, dims)
+ }
+}
+
+fn add_lines(s: &str, n: usize) -> String {
+ let mut text = String::with_capacity(s.len() + n);
+ text.push_str(s);
+ text.extend(std::iter::repeat('\n').take(n));
+
+ text
+}
diff --git a/vendor/tabled/src/settings/height/cell_height_limit.rs b/vendor/tabled/src/settings/height/cell_height_limit.rs
new file mode 100644
index 000000000..788f0300c
--- /dev/null
+++ b/vendor/tabled/src/settings/height/cell_height_limit.rs
@@ -0,0 +1,103 @@
+use crate::{
+ grid::{
+ config::{ColoredConfig, Entity},
+ dimension::CompleteDimensionVecRecords,
+ records::{ExactRecords, PeekableRecords, Records, RecordsMut},
+ util::string::{count_lines, get_lines},
+ },
+ settings::{measurement::Measurement, peaker::Peaker, CellOption, Height, TableOption},
+};
+
+use super::table_height_limit::TableHeightLimit;
+
+/// A modification for cell/table to increase its height.
+///
+/// If used for a [`Table`] [`PriorityNone`] is used.
+///
+/// [`PriorityNone`]: crate::settings::peaker::PriorityNone
+/// [`Table`]: crate::Table
+#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
+pub struct CellHeightLimit<W = usize> {
+ height: W,
+}
+
+impl<W> CellHeightLimit<W> {
+ /// Constructs a new object.
+ pub fn new(height: W) -> Self
+ where
+ W: Measurement<Height>,
+ {
+ Self { height }
+ }
+
+ /// Set's a priority by which the limit logic will be applied.
+ pub fn priority<P>(self) -> TableHeightLimit<W, P>
+ where
+ P: Peaker,
+ W: Measurement<Height>,
+ {
+ TableHeightLimit::new(self.height).priority::<P>()
+ }
+}
+
+impl<W, R> CellOption<R, ColoredConfig> for CellHeightLimit<W>
+where
+ W: Measurement<Height>,
+ R: Records + ExactRecords + PeekableRecords + RecordsMut<String>,
+ for<'a> &'a R: Records,
+{
+ fn change(self, records: &mut R, cfg: &mut ColoredConfig, entity: Entity) {
+ let height = self.height.measure(&*records, cfg);
+
+ let count_rows = records.count_rows();
+ let count_columns = records.count_columns();
+
+ for pos in entity.iter(count_rows, count_columns) {
+ let is_valid_pos = pos.0 < count_rows && pos.1 < count_columns;
+ if !is_valid_pos {
+ continue;
+ }
+
+ let text = records.get_text(pos);
+ let count_lines = count_lines(text);
+
+ if count_lines <= height {
+ continue;
+ }
+
+ let content = limit_lines(text, height);
+ records.set(pos, content);
+ }
+ }
+}
+
+impl<R, W> TableOption<R, CompleteDimensionVecRecords<'static>, ColoredConfig>
+ for CellHeightLimit<W>
+where
+ W: Measurement<Height>,
+ R: Records + ExactRecords + PeekableRecords + RecordsMut<String>,
+ for<'a> &'a R: Records,
+{
+ fn change(
+ self,
+ records: &mut R,
+ cfg: &mut ColoredConfig,
+ dims: &mut CompleteDimensionVecRecords<'static>,
+ ) {
+ let height = self.height.measure(&*records, cfg);
+ TableHeightLimit::new(height).change(records, cfg, dims)
+ }
+}
+
+fn limit_lines(s: &str, n: usize) -> String {
+ let mut text = String::new();
+ for (i, line) in get_lines(s).take(n).enumerate() {
+ if i > 0 {
+ text.push('\n');
+ }
+
+ text.push_str(&line);
+ }
+
+ text
+}
diff --git a/vendor/tabled/src/settings/height/height_list.rs b/vendor/tabled/src/settings/height/height_list.rs
new file mode 100644
index 000000000..7861dd4d2
--- /dev/null
+++ b/vendor/tabled/src/settings/height/height_list.rs
@@ -0,0 +1,47 @@
+use std::iter::FromIterator;
+
+use crate::{
+ grid::dimension::CompleteDimensionVecRecords,
+ grid::records::{ExactRecords, Records},
+ settings::TableOption,
+};
+
+/// A structure used to set [`Table`] height via a list of rows heights.
+///
+/// [`Table`]: crate::Table
+#[derive(Debug)]
+pub struct HeightList {
+ list: Vec<usize>,
+}
+
+impl HeightList {
+ /// Creates a new object.
+ pub fn new(list: Vec<usize>) -> Self {
+ Self { list }
+ }
+}
+
+impl From<Vec<usize>> for HeightList {
+ fn from(list: Vec<usize>) -> Self {
+ Self::new(list)
+ }
+}
+
+impl FromIterator<usize> for HeightList {
+ fn from_iter<T: IntoIterator<Item = usize>>(iter: T) -> Self {
+ Self::new(iter.into_iter().collect())
+ }
+}
+
+impl<R, C> TableOption<R, CompleteDimensionVecRecords<'static>, C> for HeightList
+where
+ R: ExactRecords + Records,
+{
+ fn change(self, records: &mut R, _: &mut C, dims: &mut CompleteDimensionVecRecords<'static>) {
+ if self.list.len() < records.count_rows() {
+ return;
+ }
+
+ let _ = dims.set_heights(self.list);
+ }
+}
diff --git a/vendor/tabled/src/settings/height/mod.rs b/vendor/tabled/src/settings/height/mod.rs
new file mode 100644
index 000000000..ad9caff77
--- /dev/null
+++ b/vendor/tabled/src/settings/height/mod.rs
@@ -0,0 +1,228 @@
+//! The module contains [`Height`] structure which is responsible for a table and cell height.
+
+mod cell_height_increase;
+mod cell_height_limit;
+mod height_list;
+mod table_height_increase;
+mod table_height_limit;
+mod util;
+
+use crate::settings::measurement::Measurement;
+
+pub use cell_height_increase::CellHeightIncrease;
+pub use cell_height_limit::CellHeightLimit;
+pub use height_list::HeightList;
+pub use table_height_increase::TableHeightIncrease;
+pub use table_height_limit::TableHeightLimit;
+
+/// Height is a abstract factory for height settings.
+///
+/// # Example
+///
+/// ```
+/// use tabled::{Table, settings::{Height, Settings}};
+///
+/// let data = vec![
+/// ("Some data", "here", "and here"),
+/// ("Some data on a next", "line", "right here"),
+/// ];
+///
+/// let table = Table::new(data)
+/// .with(Settings::new(Height::limit(10), Height::increase(10)))
+/// .to_string();
+///
+/// assert_eq!(
+/// table,
+/// "+---------------------+------+------------+\n\
+/// | &str | &str | &str |\n\
+/// | | | |\n\
+/// +---------------------+------+------------+\n\
+/// | Some data | here | and here |\n\
+/// | | | |\n\
+/// +---------------------+------+------------+\n\
+/// | Some data on a next | line | right here |\n\
+/// | | | |\n\
+/// +---------------------+------+------------+",
+/// )
+/// ```
+#[derive(Debug)]
+pub struct Height;
+
+impl Height {
+ /// Create [`CellHeightIncrease`] to set a table/cell height.
+ ///
+ /// # Example
+ ///
+ /// ## Cell height
+ ///
+ /// ```
+ /// use tabled::{Table, settings::{Height, Modify, object::Columns}};
+ ///
+ /// let data = vec![
+ /// ("Some data", "here", "and here"),
+ /// ("Some data on a next", "line", "right here"),
+ /// ];
+ ///
+ /// let table = Table::new(data)
+ /// .with(Modify::new(Columns::first()).with(Height::increase(5)))
+ /// .to_string();
+ ///
+ /// assert_eq!(
+ /// table,
+ /// "+---------------------+------+------------+\n\
+ /// | &str | &str | &str |\n\
+ /// | | | |\n\
+ /// | | | |\n\
+ /// | | | |\n\
+ /// | | | |\n\
+ /// +---------------------+------+------------+\n\
+ /// | Some data | here | and here |\n\
+ /// | | | |\n\
+ /// | | | |\n\
+ /// | | | |\n\
+ /// | | | |\n\
+ /// +---------------------+------+------------+\n\
+ /// | Some data on a next | line | right here |\n\
+ /// | | | |\n\
+ /// | | | |\n\
+ /// | | | |\n\
+ /// | | | |\n\
+ /// +---------------------+------+------------+"
+ /// )
+ /// ```
+ ///
+ /// ## Table height
+ ///
+ /// ```
+ /// use tabled::{Table, settings::Height};
+ ///
+ /// let data = vec![
+ /// ("Some data", "here", "and here"),
+ /// ("Some data on a next", "line", "right here"),
+ /// ];
+ ///
+ /// let table = Table::new(data)
+ /// .with(Height::increase(10))
+ /// .to_string();
+ ///
+ /// assert_eq!(
+ /// table,
+ /// "+---------------------+------+------------+\n\
+ /// | &str | &str | &str |\n\
+ /// | | | |\n\
+ /// +---------------------+------+------------+\n\
+ /// | Some data | here | and here |\n\
+ /// | | | |\n\
+ /// +---------------------+------+------------+\n\
+ /// | Some data on a next | line | right here |\n\
+ /// | | | |\n\
+ /// +---------------------+------+------------+",
+ /// )
+ /// ```
+ pub fn increase<W: Measurement<Height>>(width: W) -> CellHeightIncrease<W> {
+ CellHeightIncrease::new(width)
+ }
+
+ /// Create [`CellHeightLimit`] to set a table/cell height.
+ ///
+ /// # Example
+ ///
+ /// ## Cell height
+ ///
+ /// ```
+ /// use tabled::{Table, settings::{Height, Modify, object::Columns}};
+ ///
+ /// let data = vec![
+ /// ("Some\ndata", "here", "and here"),
+ /// ("Some\ndata on a next", "line", "right here"),
+ /// ];
+ ///
+ /// let table = Table::new(data)
+ /// .with(Modify::new(Columns::first()).with(Height::limit(1)))
+ /// .to_string();
+ ///
+ /// assert_eq!(
+ /// table,
+ /// "+------+------+------------+\n\
+ /// | &str | &str | &str |\n\
+ /// +------+------+------------+\n\
+ /// | Some | here | and here |\n\
+ /// +------+------+------------+\n\
+ /// | Some | line | right here |\n\
+ /// +------+------+------------+"
+ /// )
+ /// ```
+ ///
+ /// ## Table height
+ ///
+ /// ```
+ /// use tabled::{Table, settings::Height};
+ ///
+ /// let data = vec![
+ /// ("Some\ndata", "here", "and here"),
+ /// ("Some\ndata on a next", "line", "right here"),
+ /// ];
+ ///
+ /// let table = Table::new(&data)
+ /// .with(Height::limit(6))
+ /// .to_string();
+ ///
+ /// assert_eq!(
+ /// table,
+ /// "+------+------+------------+\n\
+ /// +------+------+------------+\n\
+ /// | Some | here | and here |\n\
+ /// +------+------+------------+\n\
+ /// | Some | line | right here |\n\
+ /// +------+------+------------+",
+ /// );
+ ///
+ /// let table = Table::new(&data)
+ /// .with(Height::limit(1))
+ /// .to_string();
+ ///
+ /// assert_eq!(
+ /// table,
+ /// "+--+--+--+\n\
+ /// +--+--+--+\n\
+ /// +--+--+--+\n\
+ /// +--+--+--+",
+ /// );
+ /// ```
+ pub fn limit<W: Measurement<Height>>(width: W) -> CellHeightLimit<W> {
+ CellHeightLimit::new(width)
+ }
+
+ /// Create [`HeightList`] to set a table height to a constant list of row heights.
+ ///
+ /// Notice if you provide a list with `.len()` less than `Table::count_rows` then it will have no affect.
+ ///
+ /// # Example
+ ///
+ /// ```
+ /// use tabled::{Table, settings::{Height, Modify, object::Columns}};
+ ///
+ /// let data = vec![
+ /// ("Some\ndata", "here", "and here"),
+ /// ("Some\ndata on a next", "line", "right here"),
+ /// ];
+ ///
+ /// let table = Table::new(data)
+ /// .with(Height::list([1, 0, 2]))
+ /// .to_string();
+ ///
+ /// assert_eq!(
+ /// table,
+ /// "+----------------+------+------------+\n\
+ /// | &str | &str | &str |\n\
+ /// +----------------+------+------------+\n\
+ /// +----------------+------+------------+\n\
+ /// | Some | line | right here |\n\
+ /// | data on a next | | |\n\
+ /// +----------------+------+------------+",
+ /// )
+ /// ```
+ pub fn list<I: IntoIterator<Item = usize>>(rows: I) -> HeightList {
+ HeightList::new(rows.into_iter().collect())
+ }
+}
diff --git a/vendor/tabled/src/settings/height/table_height_increase.rs b/vendor/tabled/src/settings/height/table_height_increase.rs
new file mode 100644
index 000000000..f727bf041
--- /dev/null
+++ b/vendor/tabled/src/settings/height/table_height_increase.rs
@@ -0,0 +1,90 @@
+use crate::{
+ grid::{
+ config::ColoredConfig,
+ dimension::CompleteDimensionVecRecords,
+ records::{ExactRecords, PeekableRecords, Records},
+ },
+ settings::{
+ measurement::Measurement,
+ peaker::{Peaker, PriorityNone},
+ Height, TableOption,
+ },
+};
+
+use super::util::get_table_height;
+
+/// A modification of a table to increase the table height.
+#[derive(Debug, Clone)]
+pub struct TableHeightIncrease<W = usize, P = PriorityNone> {
+ height: W,
+ priority: P,
+}
+
+impl<W> TableHeightIncrease<W, PriorityNone> {
+ /// Creates a new object.
+ pub fn new(height: W) -> Self
+ where
+ W: Measurement<Height>,
+ {
+ Self {
+ height,
+ priority: PriorityNone::default(),
+ }
+ }
+
+ /// Sets a different priority logic.
+ pub fn priority<P>(self) -> TableHeightIncrease<W, P>
+ where
+ P: Peaker,
+ {
+ TableHeightIncrease {
+ priority: P::create(),
+ height: self.height,
+ }
+ }
+}
+
+impl<R, W, P> TableOption<R, CompleteDimensionVecRecords<'static>, ColoredConfig>
+ for TableHeightIncrease<W, P>
+where
+ W: Measurement<Height>,
+ P: Peaker + Clone,
+ R: Records + ExactRecords + PeekableRecords,
+ for<'a> &'a R: Records,
+{
+ fn change(
+ self,
+ records: &mut R,
+ cfg: &mut ColoredConfig,
+ dims: &mut CompleteDimensionVecRecords<'static>,
+ ) {
+ if records.count_rows() == 0 || records.count_columns() == 0 {
+ return;
+ }
+
+ let height = self.height.measure(&*records, cfg);
+ let (total, mut heights) = get_table_height(&*records, cfg);
+ if total >= height {
+ return;
+ }
+
+ get_increase_list(&mut heights, height, total, self.priority);
+
+ let _ = dims.set_heights(heights);
+ }
+}
+
+fn get_increase_list<P>(list: &mut [usize], total: usize, mut current: usize, mut peaker: P)
+where
+ P: Peaker,
+{
+ while current != total {
+ let col = match peaker.peak(&[], list) {
+ Some(col) => col,
+ None => break,
+ };
+
+ list[col] += 1;
+ current += 1;
+ }
+}
diff --git a/vendor/tabled/src/settings/height/table_height_limit.rs b/vendor/tabled/src/settings/height/table_height_limit.rs
new file mode 100644
index 000000000..2a3d19bb7
--- /dev/null
+++ b/vendor/tabled/src/settings/height/table_height_limit.rs
@@ -0,0 +1,123 @@
+use crate::{
+ grid::{
+ config::ColoredConfig,
+ dimension::CompleteDimensionVecRecords,
+ records::{ExactRecords, PeekableRecords, Records, RecordsMut},
+ util::string::{count_lines, get_lines},
+ },
+ settings::{
+ measurement::Measurement,
+ peaker::{Peaker, PriorityNone},
+ Height, TableOption,
+ },
+};
+
+use super::util::get_table_height;
+
+/// A modification of a table to decrease the table height.
+#[derive(Debug)]
+pub struct TableHeightLimit<W = usize, P = PriorityNone> {
+ height: W,
+ priority: P,
+}
+
+impl<W> TableHeightLimit<W, PriorityNone> {
+ /// Creates a new object.
+ pub fn new(height: W) -> Self
+ where
+ W: Measurement<Height>,
+ {
+ Self {
+ height,
+ priority: PriorityNone::default(),
+ }
+ }
+
+ /// Sets a different priority logic.
+ pub fn priority<P>(self) -> TableHeightLimit<W, P>
+ where
+ P: Peaker,
+ {
+ TableHeightLimit {
+ priority: P::create(),
+ height: self.height,
+ }
+ }
+}
+
+impl<R, W, P> TableOption<R, CompleteDimensionVecRecords<'static>, ColoredConfig>
+ for TableHeightLimit<W, P>
+where
+ W: Measurement<Height>,
+ P: Peaker + Clone,
+ R: ExactRecords + PeekableRecords + RecordsMut<String>,
+ for<'a> &'a R: Records,
+{
+ fn change(
+ self,
+ records: &mut R,
+ cfg: &mut ColoredConfig,
+ dims: &mut CompleteDimensionVecRecords<'static>,
+ ) {
+ let count_rows = records.count_rows();
+ let count_cols = (&*records).count_columns();
+
+ if count_rows == 0 || count_cols == 0 {
+ return;
+ }
+
+ let height = self.height.measure(&*records, cfg);
+ let (total, mut heights) = get_table_height(&*records, cfg);
+ if total <= height {
+ return;
+ }
+
+ decrease_list(&mut heights, total, height, self.priority);
+
+ for (row, &height) in heights.iter().enumerate() {
+ for col in 0..count_cols {
+ let text = records.get_text((row, col));
+ let count_lines = count_lines(text);
+
+ if count_lines <= height {
+ continue;
+ }
+
+ let text = limit_lines(text, height);
+
+ records.set((row, col), text);
+ }
+ }
+
+ let _ = dims.set_heights(heights);
+ }
+}
+
+fn decrease_list<P>(list: &mut [usize], total: usize, mut value: usize, mut peaker: P)
+where
+ P: Peaker,
+{
+ while value != total {
+ let p = peaker.peak(&[], list);
+ let row = match p {
+ Some(row) => row,
+ None => break,
+ };
+
+ list[row] -= 1;
+ value += 1;
+ }
+}
+
+fn limit_lines(s: &str, n: usize) -> String {
+ let mut text = String::new();
+ for (i, line) in get_lines(s).take(n).enumerate() {
+ if i > 0 {
+ text.push('\n');
+ }
+
+ text.push_str(&line);
+ }
+
+ text
+}
diff --git a/vendor/tabled/src/settings/height/util.rs b/vendor/tabled/src/settings/height/util.rs
new file mode 100644
index 000000000..c6917d925
--- /dev/null
+++ b/vendor/tabled/src/settings/height/util.rs
@@ -0,0 +1,22 @@
+use crate::grid::{
+ config::SpannedConfig,
+ dimension::SpannedGridDimension,
+ records::{ExactRecords, Records},
+};
+
+pub(crate) fn get_table_height<R: Records + ExactRecords>(
+ records: R,
+ cfg: &SpannedConfig,
+) -> (usize, Vec<usize>) {
+ let count_horizontals = cfg.count_horizontal(records.count_rows());
+
+ let margin = cfg.get_margin();
+ let margin_size = margin.top.size + margin.bottom.size;
+
+ let list = SpannedGridDimension::height(records, cfg);
+ let total = list.iter().sum::<usize>();
+
+ let total = total + count_horizontals + margin_size;
+
+ (total, list)
+}