| 1 | pub mod arrangement; |
| 2 | pub mod formatting; |
| 3 | |
| 4 | use arrangement::arrange_content; |
| 5 | use formatting::{borders::draw_borders, content_format::format_content}; |
| 6 | |
| 7 | use crate::{ |
| 8 | Column, Table, |
| 9 | style::{CellAlignment, ColumnConstraint}, |
| 10 | }; |
| 11 | |
| 12 | /// This struct is ONLY used when table.to_string() is called. |
| 13 | /// It's purpose is to store intermediate results, information on how to |
| 14 | /// arrange the table and other convenience variables. |
| 15 | /// |
| 16 | /// The idea is to have a place for all this intermediate stuff, without |
| 17 | /// actually touching the Column struct. |
| 18 | #[derive(Debug)] |
| 19 | pub struct ColumnDisplayInfo { |
| 20 | pub padding: (u16, u16), |
| 21 | pub delimiter: Option<char>, |
| 22 | /// The actual allowed content width after arrangement |
| 23 | pub content_width: u16, |
| 24 | /// The content alignment of cells in this column |
| 25 | pub cell_alignment: Option<CellAlignment>, |
| 26 | is_hidden: bool, |
| 27 | } |
| 28 | |
| 29 | impl ColumnDisplayInfo { |
| 30 | pub fn new(column: &Column, mut content_width: u16) -> Self { |
| 31 | // The min contend width may only be 1 |
| 32 | if content_width == 0 { |
| 33 | content_width = 1; |
| 34 | } |
| 35 | Self { |
| 36 | padding: column.padding, |
| 37 | delimiter: column.delimiter, |
| 38 | content_width, |
| 39 | cell_alignment: column.cell_alignment, |
| 40 | is_hidden: matches!(column.constraint, Some(ColumnConstraint::Hidden)), |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | pub fn width(&self) -> u16 { |
| 45 | self.content_width |
| 46 | .saturating_add(self.padding.0) |
| 47 | .saturating_add(self.padding.1) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | pub fn build_table(table: &Table) -> impl Iterator<Item = String> { |
| 52 | let display_info = arrange_content(table); |
| 53 | let content = format_content(table, &display_info); |
| 54 | draw_borders(table, &content, &display_info).into_iter() |
| 55 | } |