Skip to content

Commit f38b13f

Browse files
committed
Fix error in table without title and with multiline cells
1 parent 73da33b commit f38b13f

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

cli-table/src/table.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,14 @@ impl TableStruct {
6363

6464
fn required_dimension(&self) -> Dimension {
6565
let mut heights = Vec::with_capacity(self.rows.len() + 1);
66+
let mut widths = Vec::new();
6667

67-
let row_dimension = match self.title {
68-
Some(ref title) => title.required_dimension(),
69-
None => {
70-
if self.rows.is_empty() {
71-
Default::default()
72-
} else {
73-
self.rows[0].required_dimension()
74-
}
75-
}
76-
};
68+
let title_dimension = self.title.as_ref().map(RowStruct::required_dimension);
7769

78-
let mut widths = row_dimension.widths;
79-
heights.push(row_dimension.height);
70+
if let Some(title_dimension) = title_dimension {
71+
widths = title_dimension.widths;
72+
heights.push(title_dimension.height);
73+
}
8074

8175
for row in self.rows.iter() {
8276
let row_dimension = row.required_dimension();
@@ -85,11 +79,17 @@ impl TableStruct {
8579

8680
let new_widths = row_dimension.widths;
8781

88-
for (width, new_width) in widths.iter_mut().zip(new_widths.into_iter()) {
89-
*width = std::cmp::max(new_width, *width);
82+
if widths.is_empty() {
83+
widths = new_widths;
84+
} else {
85+
for (width, new_width) in widths.iter_mut().zip(new_widths.into_iter()) {
86+
*width = std::cmp::max(new_width, *width);
87+
}
9088
}
9189
}
9290

91+
println!("heights: {:?}, widths: {:?}", heights, widths);
92+
9393
Dimension { widths, heights }
9494
}
9595

0 commit comments

Comments
 (0)