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/width/justify.rs | 96 +++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 vendor/tabled/src/settings/width/justify.rs (limited to 'vendor/tabled/src/settings/width/justify.rs') diff --git a/vendor/tabled/src/settings/width/justify.rs b/vendor/tabled/src/settings/width/justify.rs new file mode 100644 index 000000000..03b2afe0d --- /dev/null +++ b/vendor/tabled/src/settings/width/justify.rs @@ -0,0 +1,96 @@ +//! This module contains [`Justify`] structure, used to set an exact width to each column. + +use crate::{ + grid::config::ColoredConfig, + grid::records::{ExactRecords, PeekableRecords, Records, RecordsMut}, + settings::{ + measurement::{Max, Measurement, Min}, + CellOption, TableOption, Width, + }, +}; + +/// Justify sets all columns widths to the set value. +/// +/// Be aware that it doesn't consider padding. +/// So if you want to set a exact width you might need to use [`Padding`] to set it to 0. +/// +/// ## Examples +/// +/// ``` +/// use tabled::{Table, settings::{Width, Style, object::Segment, Padding, Modify}}; +/// +/// let data = ["Hello", "World", "!"]; +/// +/// let table = Table::new(&data) +/// .with(Style::markdown()) +/// .with(Modify::new(Segment::all()).with(Padding::zero())) +/// .with(Width::justify(3)); +/// ``` +/// +/// [`Max`] usage to justify by a max column width. +/// +/// ``` +/// use tabled::{Table, settings::{width::Justify, Style}}; +/// +/// let data = ["Hello", "World", "!"]; +/// +/// let table = Table::new(&data) +/// .with(Style::markdown()) +/// .with(Justify::max()); +/// ``` +/// +/// [`Padding`]: crate::settings::Padding +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +pub struct Justify { + width: W, +} + +impl Justify +where + W: Measurement, +{ + /// Creates a new [`Justify`] instance. + /// + /// Be aware that [`Padding`] is not considered when comparing the width. + /// + /// [`Padding`]: crate::settings::Padding + pub fn new(width: W) -> Self { + Self { width } + } +} + +impl Justify { + /// Creates a new Justify instance with a Max width used as a value. + pub fn max() -> Self { + Self { width: Max } + } +} + +impl Justify { + /// Creates a new Justify instance with a Min width used as a value. + pub fn min() -> Self { + Self { width: Min } + } +} + +impl TableOption for Justify +where + W: Measurement, + R: Records + ExactRecords + PeekableRecords + RecordsMut, + for<'a> &'a R: Records, +{ + fn change(self, records: &mut R, cfg: &mut ColoredConfig, _: &mut D) { + let width = self.width.measure(&*records, cfg); + + let count_rows = records.count_rows(); + let count_columns = records.count_columns(); + + for row in 0..count_rows { + for col in 0..count_columns { + let pos = (row, col).into(); + CellOption::change(Width::increase(width), records, cfg, pos); + CellOption::change(Width::truncate(width), records, cfg, pos); + } + } + } +} -- cgit v1.2.3