summaryrefslogtreecommitdiffstats
path: root/vendor/tabled/src/settings/width/justify.rs
blob: 03b2afe0d65465dc3b3f7ec73d7dcc01836d0f2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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<W> {
    width: W,
}

impl<W> Justify<W>
where
    W: Measurement<Width>,
{
    /// 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<Max> {
    /// Creates a new Justify instance with a Max width used as a value.
    pub fn max() -> Self {
        Self { width: Max }
    }
}

impl Justify<Min> {
    /// Creates a new Justify instance with a Min width used as a value.
    pub fn min() -> Self {
        Self { width: Min }
    }
}

impl<R, D, W> TableOption<R, D, ColoredConfig> for Justify<W>
where
    W: Measurement<Width>,
    R: Records + ExactRecords + PeekableRecords + RecordsMut<String>,
    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);
            }
        }
    }
}