Debian Codegraph @

src/column.rs

raw symbols SCIP
1 use craterust-analyzer cargo comfy-table 7.2.2 crate/::stylerust-analyzer cargo comfy-table 7.2.2 style/::{CellAlignmentrust-analyzer cargo comfy-table 7.2.2 style/cell/CellAlignment#, ColumnConstraintrust-analyzer cargo comfy-table 7.2.2 style/column/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::{CellAlignment, ColumnConstraint::*, Table, Width::*};
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 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)]
31 pub struct Columnrust-analyzer cargo comfy-table 7.2.2 column/Column# {
32 /// The index of the column
33 pub indexrust-analyzer cargo comfy-table 7.2.2 column/Column#index.: usize,
34 /// Left/right padding for each cell of this column in spaces
35 pub(craterust-analyzer cargo comfy-table 7.2.2 crate/) paddingrust-analyzer cargo comfy-table 7.2.2 column/Column#padding.: (u16, u16),
36 /// The delimiter which is used to split the text into consistent pieces.
37 /// Default is ` `.
38 pub(craterust-analyzer cargo comfy-table 7.2.2 crate/) delimiterrust-analyzer cargo comfy-table 7.2.2 column/Column#delimiter.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<char>,
39 /// Define the [CellAlignment] for all cells of this column
40 pub(craterust-analyzer cargo comfy-table 7.2.2 crate/) cell_alignmentrust-analyzer cargo comfy-table 7.2.2 column/Column#cell_alignment.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<CellAlignmentrust-analyzer cargo comfy-table 7.2.2 style/cell/CellAlignment#>,
41 pub(craterust-analyzer cargo comfy-table 7.2.2 crate/) constraintrust-analyzer cargo comfy-table 7.2.2 column/Column#constraint.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<ColumnConstraintrust-analyzer cargo comfy-table 7.2.2 style/column/ColumnConstraint#>,
42 }
43
44 impl Columnrust-analyzer cargo comfy-table 7.2.2 column/Column# {
45 pub fn newrust-analyzer cargo comfy-table 7.2.2 column/impl#[Column]new().(index: usize) -> Selfrust-analyzer cargo comfy-table 7.2.2 column/impl#[Column] {
46 Selfrust-analyzer cargo comfy-table 7.2.2 column/impl#[Column] {
47 indexrust-analyzer cargo comfy-table 7.2.2 column/Column#index.,
48 paddingrust-analyzer cargo comfy-table 7.2.2 column/Column#padding.: (1, 1),
49 delimiterrust-analyzer cargo comfy-table 7.2.2 column/Column#delimiter.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
50 constraintrust-analyzer cargo comfy-table 7.2.2 column/Column#constraint.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
51 cell_alignmentrust-analyzer cargo comfy-table 7.2.2 column/Column#cell_alignment.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#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_paddingrust-analyzer cargo comfy-table 7.2.2 column/impl#[Column]set_padding().(&mut self, padding: (u16, u16)) -> &mut Selfrust-analyzer cargo comfy-table 7.2.2 column/impl#[Column] {
60 self.paddingrust-analyzer cargo comfy-table 7.2.2 column/Column#padding. = padding;
61
62 self
63 }
64
65 /// Convenience helper that returns the total width of the combined padding.
66 pub fn padding_widthrust-analyzer cargo comfy-table 7.2.2 column/impl#[Column]padding_width().(&self) -> u16 {
67 self.paddingrust-analyzer cargo comfy-table 7.2.2 column/Column#padding..0.saturating_addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u16]saturating_add().(self.paddingrust-analyzer cargo comfy-table 7.2.2 column/Column#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_delimiterrust-analyzer cargo comfy-table 7.2.2 column/impl#[Column]set_delimiter().(&mut self, delimiter: char) -> &mut Selfrust-analyzer cargo comfy-table 7.2.2 column/impl#[Column] {
76 self.delimiterrust-analyzer cargo comfy-table 7.2.2 column/Column#delimiter. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#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_constraintrust-analyzer cargo comfy-table 7.2.2 column/impl#[Column]set_constraint().(&mut self, constraint: ColumnConstraintrust-analyzer cargo comfy-table 7.2.2 style/column/ColumnConstraint#) -> &mut Selfrust-analyzer cargo comfy-table 7.2.2 column/impl#[Column] {
84 self.constraintrust-analyzer cargo comfy-table 7.2.2 column/Column#constraint. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(constraint);
85
86 self
87 }
88
89 /// Get the constraint that is used for this column.
90 pub fn constraintrust-analyzer cargo comfy-table 7.2.2 column/impl#[Column]constraint().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&ColumnConstraintrust-analyzer cargo comfy-table 7.2.2 style/column/ColumnConstraint#> {
91 self.constraintrust-analyzer cargo comfy-table 7.2.2 column/Column#constraint..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().()
92 }
93
94 /// Remove any constraint on this column
95 pub fn remove_constraintrust-analyzer cargo comfy-table 7.2.2 column/impl#[Column]remove_constraint().(&mut self) -> &mut Selfrust-analyzer cargo comfy-table 7.2.2 column/impl#[Column] {
96 self.constraintrust-analyzer cargo comfy-table 7.2.2 column/Column#constraint. = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
97
98 self
99 }
100
101 /// Returns weather the columns is hidden via [ColumnConstraint::Hidden].
102 pub fn is_hiddenrust-analyzer cargo comfy-table 7.2.2 column/impl#[Column]is_hidden().(&self) -> bool {
103 matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(self.constraintrust-analyzer cargo comfy-table 7.2.2 column/Column#constraint., Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ColumnConstraintrust-analyzer cargo comfy-table 7.2.2 style/column/ColumnConstraint#::Hiddenrust-analyzer cargo comfy-table 7.2.2 style/column/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_alignmentrust-analyzer cargo comfy-table 7.2.2 column/impl#[Column]set_cell_alignment().(&mut self, alignment: CellAlignmentrust-analyzer cargo comfy-table 7.2.2 style/cell/CellAlignment#) {
109 self.cell_alignmentrust-analyzer cargo comfy-table 7.2.2 column/Column#cell_alignment. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(alignment);
110 }
111 }
112
113 #[cfg(test)]
114 mod testsrust-analyzer cargo comfy-table 7.2.2 column/tests/ {
115 use superrust-analyzer cargo comfy-table 7.2.2 column/::*;
116
117 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
118 fn test_columnrust-analyzer cargo comfy-table 7.2.2 column/tests/test_column().() {
119 let mut column = Columnrust-analyzer cargo comfy-table 7.2.2 column/Column#::newrust-analyzer cargo comfy-table 7.2.2 column/impl#[Column]new().(0);
120 column.set_paddingrust-analyzer cargo comfy-table 7.2.2 column/impl#[Column]set_padding().((0, 0));
121
122 column.set_constraintrust-analyzer cargo comfy-table 7.2.2 column/impl#[Column]set_constraint().(ColumnConstraintrust-analyzer cargo comfy-table 7.2.2 style/column/ColumnConstraint#::ContentWidthrust-analyzer cargo comfy-table 7.2.2 style/column/ColumnConstraint#ContentWidth#);
123 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(column.constraintrust-analyzer cargo comfy-table 7.2.2 column/impl#[Column]constraint().(), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&ColumnConstraintrust-analyzer cargo comfy-table 7.2.2 style/column/ColumnConstraint#::ContentWidthrust-analyzer cargo comfy-table 7.2.2 style/column/ColumnConstraint#ContentWidth#));
124
125 column.remove_constraintrust-analyzer cargo comfy-table 7.2.2 column/impl#[Column]remove_constraint().();
126 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(column.constraintrust-analyzer cargo comfy-table 7.2.2 column/impl#[Column]constraint().(), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
127 }
128 }