Render the table to lines with wrapped cells.
(&self, max_total_width: usize)
| 647 | |
| 648 | /// Render the table to lines with wrapped cells. |
| 649 | fn render_table(&self, max_total_width: usize) -> Vec<Line<'static>> { |
| 650 | let mut lines = Vec::new(); |
| 651 | |
| 652 | if self.rows.is_empty() { |
| 653 | return lines; |
| 654 | } |
| 655 | |
| 656 | let widths = self.calculate_column_widths(max_total_width); |
| 657 | let num_cols = widths.len(); |
| 658 | if num_cols == 0 { |
| 659 | return lines; |
| 660 | } |
| 661 | |
| 662 | let border_style = Style::default().fg(ratatui::style::Color::DarkGray); |
| 663 | let build_border = |left: char, mid: char, right: char| -> Line<'static> { |
| 664 | let mut border = String::new(); |
| 665 | border.push(left); |
| 666 | for (idx, width) in widths.iter().enumerate() { |
| 667 | border.push_str(&"\u{2500}".repeat(width.saturating_add(2))); |
| 668 | if idx + 1 < num_cols { |
| 669 | border.push(mid); |
| 670 | } |
| 671 | } |
| 672 | border.push(right); |
| 673 | Line::from(Span::styled(border, border_style)) |
| 674 | }; |
| 675 | |
| 676 | lines.push(build_border('\u{250C}', '\u{252C}', '\u{2510}')); |
| 677 | |
| 678 | for (row_idx, row) in self.rows.iter().enumerate() { |
| 679 | let mut wrapped_cells: Vec<Vec<Vec<Span<'static>>>> = Vec::with_capacity(num_cols); |
| 680 | let mut row_height = 1usize; |
| 681 | |
| 682 | for (col_idx, col_width) in widths.iter().copied().enumerate().take(num_cols) { |
| 683 | let cell = row.get(col_idx).cloned().unwrap_or_default(); |
| 684 | let mut wrapped_lines: Vec<Line<'static>> = Vec::new(); |
| 685 | if cell.is_empty() { |
| 686 | wrapped_lines.push(Line::from(Vec::<Span<'static>>::new())); |
| 687 | } else { |
| 688 | wrap_spans_to_lines(cell, col_width.max(1), &mut wrapped_lines); |
| 689 | if wrapped_lines.is_empty() { |
| 690 | wrapped_lines.push(Line::from(Vec::<Span<'static>>::new())); |
| 691 | } |
| 692 | } |
| 693 | let wrapped_spans: Vec<Vec<Span<'static>>> = |
| 694 | wrapped_lines.into_iter().map(|line| line.spans).collect(); |
| 695 | row_height = row_height.max(wrapped_spans.len()); |
| 696 | wrapped_cells.push(wrapped_spans); |
| 697 | } |
| 698 | |
| 699 | for visual_row in 0..row_height { |
| 700 | let mut line_spans = Vec::new(); |
| 701 | line_spans.push(Span::styled("\u{2502}".to_string(), border_style)); |
| 702 | |
| 703 | for col_idx in 0..num_cols { |
| 704 | let col_width = widths[col_idx]; |
| 705 | let alignment = self |
| 706 | .alignments |