Calculate the width for each column. Width is determined by the maximum width of all cells in the column, including the header, constrained by min/max width settings.
(&self)
| 389 | /// Width is determined by the maximum width of all cells in the column, |
| 390 | /// including the header, constrained by min/max width settings. |
| 391 | fn calculate_widths(&self) -> Vec<usize> { |
| 392 | let mut widths: Vec<usize> = self |
| 393 | .columns |
| 394 | .iter() |
| 395 | .map(|c| { |
| 396 | let header_width = console::measure_text_width(&c.header); |
| 397 | header_width.max(c.min_width) |
| 398 | }) |
| 399 | .collect(); |
| 400 | |
| 401 | // Consider row content |
| 402 | for row in &self.rows { |
| 403 | for (i, cell) in row.cells.iter().enumerate() { |
| 404 | if i < widths.len() { |
| 405 | let cell_width = console::measure_text_width(cell); |
| 406 | widths[i] = widths[i].max(cell_width); |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | // Apply max width constraints |
| 412 | for (i, col) in self.columns.iter().enumerate() { |
| 413 | if col.max_width > 0 && i < widths.len() { |
| 414 | widths[i] = widths[i].min(col.max_width); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | widths |
| 419 | } |
| 420 | |
| 421 | /// Truncate text to fit within a maximum width. |
| 422 | fn truncate(text: &str, max_width: usize) -> String { |