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
use crate::styling::cell::CellAlignment;
use crate::styling::column::Constraint;

/// The Column struct mainly exists for styling purposes.
/// It's used to determine how much horizontal space each column should get and
/// allows users to manipulate this option.
/// On top of this, the column determines how much padding each cell should get.
pub struct Column {
    /// The index of the column
    pub index: usize,
    /// Left/right padding for each cell of this column in spaces
    pub(crate) padding: (u16, u16),
    /// Define the cell alligment for all cells of this column
    pub(crate) cell_alignment: Option<CellAlignment>,
    pub(crate) max_content_width: u16,
    pub(crate) constraint: Option<Constraint>,
}

impl Column {
    pub fn new(index: usize) -> Self {
        Column {
            index: index,
            padding: (1, 1),
            constraint: None,
            max_content_width: 0,
            cell_alignment: None,
        }
    }

    /// Set the padding for all cells of this column
    /// Padding is provided in the form of (left, right).
    /// Default is (1, 1)
    pub fn set_padding(&mut self, padding: (u16, u16)) -> &mut Self {
        self.padding = padding;

        self
    }

    /// Get the width in characters of the widest line in this column.
    pub fn get_max_content_width(&self) -> u16 {
        self.max_content_width
    }

    /// Set the constraint for this column.
    /// Adding a constraint allows to define some additional styling parameters for columns
    /// This can be useful to counter undesired auto-formatting of content in tables.
    pub fn set_constraint(&mut self, constraint: Constraint) -> &mut Self {
        self.constraint = Some(constraint);

        self
    }

    /// Get the constraint that is used for this column.
    pub fn get_constraint(&mut self) -> Option<&Constraint> {
        self.constraint.as_ref()
    }

    /// Set the alignment for content inside of cells for this column
    /// If the alignment attribute is set on a cell in this column as well,
    /// The cell's alighment value will overwrite the column's setting.
    pub fn set_cell_alignment(&mut self, alignment: Option<CellAlignment>) {
        self.cell_alignment = alignment;
    }
}