Debian Codegraph @

src/row.rs

raw symbols SCIP
1 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/::Iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/Iter#;
2
3 use craterust-analyzer cargo comfy-table 7.2.2 crate/::{
4 cellrust-analyzer cargo comfy-table 7.2.2 cell/::{Cellrust-analyzer cargo comfy-table 7.2.2 cell/Cell#, Cellsrust-analyzer cargo comfy-table 7.2.2 cell/Cells#},
5 utilsrust-analyzer cargo comfy-table 7.2.2 utils/::formattingrust-analyzer cargo comfy-table 7.2.2 utils/formatting/::content_splitrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/::measure_text_widthrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/custom_styling/measure_text_width().,
6 };
7
8 /// Each row contains [Cells](crate::Cell) and can be added to a [Table](crate::Table).
9 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)]
10 pub struct Rowrust-analyzer cargo comfy-table 7.2.2 row/Row# {
11 /// Index of the row.
12 /// This will be set as soon as the row is added to the table.
13 pub(craterust-analyzer cargo comfy-table 7.2.2 crate/) indexrust-analyzer cargo comfy-table 7.2.2 row/Row#index.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>,
14 pub(craterust-analyzer cargo comfy-table 7.2.2 crate/) cellsrust-analyzer cargo comfy-table 7.2.2 row/Row#cells.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Cellrust-analyzer cargo comfy-table 7.2.2 cell/Cell#>,
15 pub(craterust-analyzer cargo comfy-table 7.2.2 crate/) max_heightrust-analyzer cargo comfy-table 7.2.2 row/Row#max_height.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>,
16 }
17
18 impl Rowrust-analyzer cargo comfy-table 7.2.2 row/Row# {
19 pub fn newrust-analyzer cargo comfy-table 7.2.2 row/impl#[Row]new().() -> Selfrust-analyzer cargo comfy-table 7.2.2 row/impl#[Row] {
20 Selfrust-analyzer cargo comfy-table 7.2.2 row/impl#[Row]::defaultrust-analyzer cargo comfy-table 7.2.2 row/impl#[Row][Default]default().()
21 }
22
23 /// Add a cell to the row.
24 ///
25 /// **Attention:**
26 /// If a row has already been added to a table and you add more cells to it
27 /// than there're columns currently know to the [Table](crate::Table) struct,
28 /// these columns won't be known to the table unless you call
29 /// [crate::Table::discover_columns].
30 ///
31 /// ```rust
32 /// use comfy_table::{Cell, Row};
33 ///
34 /// let mut row = Row::new();
35 /// row.add_cell(Cell::new("One"));
36 /// ```
37 pub fn add_cellrust-analyzer cargo comfy-table 7.2.2 row/impl#[Row]add_cell().(&mut self, cell: Cellrust-analyzer cargo comfy-table 7.2.2 cell/Cell#) -> &mut Selfrust-analyzer cargo comfy-table 7.2.2 row/impl#[Row] {
38 self.cellsrust-analyzer cargo comfy-table 7.2.2 row/Row#cells..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(cell);
39
40 self
41 }
42
43 /// Truncate content of cells which occupies more than X lines of space.
44 ///
45 /// ```
46 /// use comfy_table::{Cell, Row};
47 ///
48 /// let mut row = Row::new();
49 /// row.max_height(5);
50 /// ```
51 pub fn max_heightrust-analyzer cargo comfy-table 7.2.2 row/impl#[Row]max_height().(&mut self, lines: usize) -> &mut Selfrust-analyzer cargo comfy-table 7.2.2 row/impl#[Row] {
52 self.max_heightrust-analyzer cargo comfy-table 7.2.2 row/Row#max_height. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(lines);
53
54 self
55 }
56
57 /// Get the longest content width for all cells of this row
58 pub(craterust-analyzer cargo comfy-table 7.2.2 crate/) fn max_content_widthsrust-analyzer cargo comfy-table 7.2.2 row/impl#[Row]max_content_widths().(&self) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<usize> {
59 // Iterate over all cells
60 self.cellsrust-analyzer cargo comfy-table 7.2.2 row/Row#cells.
61 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
62 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|cell| {
63 // Iterate over all content strings and return a vector of string widths.
64 // Each entry represents the longest string width for a cell.
65 cell.contentrust-analyzer cargo comfy-table 7.2.2 cell/Cell#content.
66 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
67 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|string| measure_text_widthrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/custom_styling/measure_text_width().(string))
68 .maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#max().()
69 .unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(0)
70 })
71 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().()
72 }
73
74 /// Get the amount of cells on this row.
75 pub fn cell_countrust-analyzer cargo comfy-table 7.2.2 row/impl#[Row]cell_count().(&self) -> usize {
76 self.cellsrust-analyzer cargo comfy-table 7.2.2 row/Row#cells..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()
77 }
78
79 /// Returns an iterator over all cells of this row
80 pub fn cell_iterrust-analyzer cargo comfy-table 7.2.2 row/impl#[Row]cell_iter().(&self) -> Iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/Iter#<'_, Cellrust-analyzer cargo comfy-table 7.2.2 cell/Cell#> {
81 self.cellsrust-analyzer cargo comfy-table 7.2.2 row/Row#cells..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
82 }
83 }
84
85 /// Create a Row from any `Into<Cells>`. \
86 /// [Cells] is a simple wrapper around a `Vec<Cell>`.
87 ///
88 /// Check the [From] implementations on [Cell] for more information.
89 ///
90 /// ```rust
91 /// use comfy_table::{Cell, Row};
92 ///
93 /// let row = Row::from(vec!["One", "Two", "Three"]);
94 /// let row = Row::from(vec![Cell::new("One"), Cell::new("Two"), Cell::new("Three")]);
95 /// let row = Row::from(vec![1, 2, 3, 4]);
96 /// ```
97 impl<T: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Cellsrust-analyzer cargo comfy-table 7.2.2 cell/Cells#>> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<T> for Rowrust-analyzer cargo comfy-table 7.2.2 row/Row# {
98 fn fromrust-analyzer cargo comfy-table 7.2.2 row/impl#[Row][`From<T>`]from().(cells: T) -> Selfrust-analyzer cargo comfy-table 7.2.2 row/impl#[Row][`From<T>`] {
99 Selfrust-analyzer cargo comfy-table 7.2.2 row/impl#[Row][`From<T>`] {
100 indexrust-analyzer cargo comfy-table 7.2.2 row/Row#index.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
101 cellsrust-analyzer cargo comfy-table 7.2.2 row/Row#cells.: cells.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().().0rust-analyzer cargo comfy-table 7.2.2 cell/Cells#0.,
102 max_heightrust-analyzer cargo comfy-table 7.2.2 row/Row#max_height.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
103 }
104 }
105 }
106
107 #[cfg(test)]
108 mod testsrust-analyzer cargo comfy-table 7.2.2 row/tests/ {
109 use superrust-analyzer cargo comfy-table 7.2.2 row/::*;
110
111 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
112 fn test_correct_max_content_widthrust-analyzer cargo comfy-table 7.2.2 row/tests/test_correct_max_content_width().() {
113 let row = Rowrust-analyzer cargo comfy-table 7.2.2 row/Row#::fromrust-analyzer cargo comfy-table 7.2.2 row/impl#[Row][`From<T>`]from().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![
114 "",
115 "four",
116 "fivef",
117 "sixsix",
118 "11 but with\na newline",
119 ]);
120
121 let max_content_widths = row.max_content_widthsrust-analyzer cargo comfy-table 7.2.2 row/impl#[Row]max_content_widths().();
122
123 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(max_content_widths, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0, 4, 5, 6, 11]);
124 }
125
126 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
127 fn test_some_functionsrust-analyzer cargo comfy-table 7.2.2 row/tests/test_some_functions().() {
128 let cells = ["one", "two", "three"];
129 let mut row = Rowrust-analyzer cargo comfy-table 7.2.2 row/Row#::newrust-analyzer cargo comfy-table 7.2.2 row/impl#[Row]new().();
130 for cell in cells.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() {
131 row.add_cellrust-analyzer cargo comfy-table 7.2.2 row/impl#[Row]add_cell().(Cellrust-analyzer cargo comfy-table 7.2.2 cell/Cell#::newrust-analyzer cargo comfy-table 7.2.2 cell/impl#[Cell]new().(cell));
132 }
133 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(row.cell_countrust-analyzer cargo comfy-table 7.2.2 row/impl#[Row]cell_count().(), cells.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().());
134
135 let mut cell_content_iter = cells.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().();
136 for cell in row.cell_iterrust-analyzer cargo comfy-table 7.2.2 row/impl#[Row]cell_iter().() {
137 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
138 cell.contentrust-analyzer cargo comfy-table 7.2.2 cell/impl#[Cell]content().(),
139 cell_content_iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]next().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()
140 );
141 }
142 }
143 }