super_table/column.rs
1use crate::style::{CellAlignment, ColumnConstraint, VerticalAlignment};
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 super_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 /// Define the [VerticalAlignment] for all cells of this column
42 pub(crate) vertical_alignment: Option<VerticalAlignment>,
43 pub(crate) constraint: Option<ColumnConstraint>,
44}
45
46impl Column {
47 pub fn new(index: usize) -> Self {
48 Self {
49 index,
50 padding: (1, 1),
51 delimiter: None,
52 constraint: None,
53 cell_alignment: None,
54 vertical_alignment: None,
55 }
56 }
57
58 /// Set the padding for all cells of this column.
59 ///
60 /// Padding is provided in the form of (left, right).\
61 /// Default is `(1, 1)`.
62 pub fn set_padding(&mut self, padding: (u16, u16)) -> &mut Self {
63 self.padding = padding;
64
65 self
66 }
67
68 /// Convenience helper that returns the total width of the combined padding.
69 pub fn padding_width(&self) -> u16 {
70 self.padding.0.saturating_add(self.padding.1)
71 }
72
73 /// Set the delimiter used to split text for this column's cells.
74 ///
75 /// A custom delimiter on a cell in will overwrite the column's delimiter.
76 /// Normal text uses spaces (` `) as delimiters. This is necessary to help super-table
77 /// understand the concept of _words_.
78 pub fn set_delimiter(&mut self, delimiter: char) -> &mut Self {
79 self.delimiter = Some(delimiter);
80
81 self
82 }
83
84 /// Constraints allow to influence the auto-adjustment behavior of columns.\
85 /// This can be useful to counter undesired auto-adjustment of content in tables.
86 pub fn set_constraint(&mut self, constraint: ColumnConstraint) -> &mut Self {
87 self.constraint = Some(constraint);
88
89 self
90 }
91
92 /// Get the constraint that is used for this column.
93 pub fn constraint(&self) -> Option<&ColumnConstraint> {
94 self.constraint.as_ref()
95 }
96
97 /// Remove any constraint on this column
98 pub fn remove_constraint(&mut self) -> &mut Self {
99 self.constraint = None;
100
101 self
102 }
103
104 /// Returns weather the columns is hidden via [ColumnConstraint::Hidden].
105 pub fn is_hidden(&self) -> bool {
106 matches!(self.constraint, Some(ColumnConstraint::Hidden))
107 }
108
109 /// Set the horizontal alignment for content inside of cells for this column.\
110 /// **Note:** Alignment on a cell will always overwrite the column's setting.
111 pub fn set_cell_alignment(&mut self, alignment: CellAlignment) {
112 self.cell_alignment = Some(alignment);
113 }
114
115 /// Set the vertical alignment for content inside of cells for this column.\
116 /// **Note:** Vertical alignment on a cell will always overwrite the column's setting.
117 pub fn set_vertical_alignment(&mut self, alignment: VerticalAlignment) {
118 self.vertical_alignment = Some(alignment);
119 }
120}
121
122#[cfg(test)]
123mod tests {
124 use super::*;
125
126 #[test]
127 fn test_column() {
128 let mut column = Column::new(0);
129 column.set_padding((0, 0));
130
131 column.set_constraint(ColumnConstraint::ContentWidth);
132 assert_eq!(column.constraint(), Some(&ColumnConstraint::ContentWidth));
133
134 column.remove_constraint();
135 assert_eq!(column.constraint(), None);
136 }
137}