comfy_table/row.rs
1use std::slice::Iter;
2
3use crate::{
4 cell::{Cell, Cells},
5 utils::formatting::content_split::measure_text_width,
6};
7
8/// Each row contains [Cells](crate::Cell) and can be added to a [Table](crate::Table).
9#[derive(Clone, Debug, Default)]
10pub struct Row {
11 /// Index of the row.
12 /// This will be set as soon as the row is added to the table.
13 pub(crate) index: Option<usize>,
14 pub(crate) cells: Vec<Cell>,
15 pub(crate) max_height: Option<usize>,
16}
17
18impl Row {
19 pub fn new() -> Self {
20 Self::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::{Row, Cell};
33 ///
34 /// let mut row = Row::new();
35 /// row.add_cell(Cell::new("One"));
36 /// ```
37 pub fn add_cell(&mut self, cell: Cell) -> &mut Self {
38 self.cells.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::{Row, Cell};
47 ///
48 /// let mut row = Row::new();
49 /// row.max_height(5);
50 /// ```
51 pub fn max_height(&mut self, lines: usize) -> &mut Self {
52 self.max_height = Some(lines);
53
54 self
55 }
56
57 /// Get the longest content width for all cells of this row
58 pub(crate) fn max_content_widths(&self) -> Vec<usize> {
59 // Iterate over all cells
60 self.cells
61 .iter()
62 .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.content
66 .iter()
67 .map(|string| measure_text_width(string))
68 .max()
69 .unwrap_or(0)
70 })
71 .collect()
72 }
73
74 /// Get the amount of cells on this row.
75 pub fn cell_count(&self) -> usize {
76 self.cells.len()
77 }
78
79 /// Returns an iterator over all cells of this row
80 pub fn cell_iter(&self) -> Iter<Cell> {
81 self.cells.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::{Row, Cell};
92///
93/// let row = Row::from(vec!["One", "Two", "Three",]);
94/// let row = Row::from(vec![
95/// Cell::new("One"),
96/// Cell::new("Two"),
97/// Cell::new("Three"),
98/// ]);
99/// let row = Row::from(vec![1, 2, 3, 4]);
100/// ```
101impl<T: Into<Cells>> From<T> for Row {
102 fn from(cells: T) -> Self {
103 Self {
104 index: None,
105 cells: cells.into().0,
106 max_height: None,
107 }
108 }
109}
110
111#[cfg(test)]
112mod tests {
113 use super::*;
114
115 #[test]
116 fn test_correct_max_content_width() {
117 let row = Row::from(vec![
118 "",
119 "four",
120 "fivef",
121 "sixsix",
122 "11 but with\na newline",
123 ]);
124
125 let max_content_widths = row.max_content_widths();
126
127 assert_eq!(max_content_widths, vec![0, 4, 5, 6, 11]);
128 }
129
130 #[test]
131 fn test_some_functions() {
132 let cells = ["one", "two", "three"];
133 let mut row = Row::new();
134 for cell in cells.iter() {
135 row.add_cell(Cell::new(cell));
136 }
137 assert_eq!(row.cell_count(), cells.len());
138
139 let mut cell_content_iter = cells.iter();
140 for cell in row.cell_iter() {
141 assert_eq!(
142 cell.content(),
143 cell_content_iter.next().unwrap().to_string()
144 );
145 }
146 }
147}