comfy_table/
column.rs

1use crate::style::{CellAlignment, ColumnConstraint};
2
3/// A representation of a table's column.
4/// Useful for styling and specifying constraints how big a column should be.
5///
6/// 1. Content padding for cells in this column
7/// 2. Constraints on how wide this column shall be
8/// 3. Default alignment for cells in this column
9///
10/// Columns are generated when adding rows or a header to a table.\
11/// As a result columns can only be modified after the table is populated by some data.
12///
13/// ```
14/// use comfy_table::{Width::*, CellAlignment, ColumnConstraint::*, Table};
15///
16/// let mut table = Table::new();
17/// table.set_header(&vec!["one", "two"]);
18///
19/// let mut column = table.column_mut(1).expect("This should be column two");
20///
21/// // Set the max width for all cells of this column to 20 characters.
22/// column.set_constraint(UpperBoundary(Fixed(20)));
23///
24/// // Set the left padding to 5 spaces and the right padding to 1 space
25/// column.set_padding((5, 1));
26///
27/// // Align content in all cells of this column to the center of the cell.
28/// column.set_cell_alignment(CellAlignment::Center);
29/// ```
30#[derive(Debug, Clone)]
31pub struct Column {
32    /// The index of the column
33    pub index: usize,
34    /// Left/right padding for each cell of this column in spaces
35    pub(crate) padding: (u16, u16),
36    /// The delimiter which is used to split the text into consistent pieces.
37    /// Default is ` `.
38    pub(crate) delimiter: Option<char>,
39    /// Define the [CellAlignment] for all cells of this column
40    pub(crate) cell_alignment: Option<CellAlignment>,
41    pub(crate) constraint: Option<ColumnConstraint>,
42}
43
44impl Column {
45    pub fn new(index: usize) -> Self {
46        Self {
47            index,
48            padding: (1, 1),
49            delimiter: None,
50            constraint: None,
51            cell_alignment: None,
52        }
53    }
54
55    /// Set the padding for all cells of this column.
56    ///
57    /// Padding is provided in the form of (left, right).\
58    /// Default is `(1, 1)`.
59    pub fn set_padding(&mut self, padding: (u16, u16)) -> &mut Self {
60        self.padding = padding;
61
62        self
63    }
64
65    /// Convenience helper that returns the total width of the combined padding.
66    pub fn padding_width(&self) -> u16 {
67        self.padding.0.saturating_add(self.padding.1)
68    }
69
70    /// Set the delimiter used to split text for this column's cells.
71    ///
72    /// A custom delimiter on a cell in will overwrite the column's delimiter.
73    /// Normal text uses spaces (` `) as delimiters. This is necessary to help comfy-table
74    /// understand the concept of _words_.
75    pub fn set_delimiter(&mut self, delimiter: char) -> &mut Self {
76        self.delimiter = Some(delimiter);
77
78        self
79    }
80
81    /// Constraints allow to influence the auto-adjustment behavior of columns.\
82    /// This can be useful to counter undesired auto-adjustment of content in tables.
83    pub fn set_constraint(&mut self, constraint: ColumnConstraint) -> &mut Self {
84        self.constraint = Some(constraint);
85
86        self
87    }
88
89    /// Get the constraint that is used for this column.
90    pub fn constraint(&self) -> Option<&ColumnConstraint> {
91        self.constraint.as_ref()
92    }
93
94    /// Remove any constraint on this column
95    pub fn remove_constraint(&mut self) -> &mut Self {
96        self.constraint = None;
97
98        self
99    }
100
101    /// Returns weather the columns is hidden via [ColumnConstraint::Hidden].
102    pub fn is_hidden(&self) -> bool {
103        matches!(self.constraint, Some(ColumnConstraint::Hidden))
104    }
105
106    /// Set the alignment for content inside of cells for this column.\
107    /// **Note:** Alignment on a cell will always overwrite the column's setting.
108    pub fn set_cell_alignment(&mut self, alignment: CellAlignment) {
109        self.cell_alignment = Some(alignment);
110    }
111}
112
113#[cfg(test)]
114mod tests {
115    use super::*;
116
117    #[test]
118    fn test_column() {
119        let mut column = Column::new(0);
120        column.set_padding((0, 0));
121
122        column.set_constraint(ColumnConstraint::ContentWidth);
123        assert_eq!(column.constraint(), Some(&ColumnConstraint::ContentWidth));
124
125        column.remove_constraint();
126        assert_eq!(column.constraint(), None);
127    }
128}