| 1 | use std::collections::BTreeMap; |
| 2 | |
| 3 | use super::ColumnDisplayInfo; |
| 4 | use crate::{style::ContentArrangement, table::Table}; |
| 5 | |
| 6 | pub mod constraint; |
| 7 | mod disabled; |
| 8 | mod dynamic; |
| 9 | pub mod helper; |
| 10 | |
| 11 | type DisplayInfos = BTreeMap<usize, ColumnDisplayInfo>; |
| 12 | |
| 13 | /// Determine the width of each column depending on the content of the given table. |
| 14 | /// The results uses Option<usize>, since users can choose to hide columns. |
| 15 | pub fn arrange_content(table: &Table) -> Vec<ColumnDisplayInfo> { |
| 16 | let table_width = table.width().map(usize::from); |
| 17 | let mut infos = BTreeMap::new(); |
| 18 | |
| 19 | let max_content_widths = table.column_max_content_widths(); |
| 20 | |
| 21 | // Check if we can already resolve some constraints. |
| 22 | // This step also populates the ColumnDisplayInfo structs. |
| 23 | let visible_columns = helper::count_visible_columns(&table.columns); |
| 24 | for column in table.columns.iter() { |
| 25 | if column.constraint.is_some() { |
| 26 | constraint::evaluate( |
| 27 | table, |
| 28 | visible_columns, |
| 29 | &mut infos, |
| 30 | column, |
| 31 | max_content_widths[column.index], |
| 32 | ); |
| 33 | } |
| 34 | } |
| 35 | #[cfg(feature = "_debug")] |
| 36 | println!("After initial constraints: {infos:#?}"); |
| 37 | |
| 38 | // Fallback to `ContentArrangement::Disabled`, if we don't have any information |
| 39 | // on how wide the table should be. |
| 40 | let table_width = if let Some(table_width) = table_width { |
| 41 | table_width |
| 42 | } else { |
| 43 | disabled::arrange(table, &mut infos, visible_columns, &max_content_widths); |
| 44 | return infos.into_values().collect(); |
| 45 | }; |
| 46 | |
| 47 | match &table.arrangement { |
| 48 | ContentArrangement::Disabled => { |
| 49 | disabled::arrange(table, &mut infos, visible_columns, &max_content_widths) |
| 50 | } |
| 51 | ContentArrangement::Dynamic | ContentArrangement::DynamicFullWidth => { |
| 52 | dynamic::arrange(table, &mut infos, table_width, &max_content_widths); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | infos.into_values().collect() |
| 57 | } |
| 58 | |
| 59 | #[cfg(test)] |
| 60 | mod tests { |
| 61 | use super::*; |
| 62 | |
| 63 | #[test] |
| 64 | fn test_disabled_arrangement() { |
| 65 | let mut table = Table::new(); |
| 66 | table.set_header(vec!["head", "head", "head"]); |
| 67 | table.add_row(vec!["__", "fivef", "sixsix"]); |
| 68 | |
| 69 | let display_infos = arrange_content(&table); |
| 70 | |
| 71 | // The width should be the width of the rows + padding |
| 72 | let widths: Vec<u16> = display_infos.iter().map(ColumnDisplayInfo::width).collect(); |
| 73 | assert_eq!(widths, vec![6, 7, 8]); |
| 74 | } |
| 75 | |
| 76 | #[test] |
| 77 | fn test_discover_columns() { |
| 78 | let mut table = Table::new(); |
| 79 | table.add_row(vec!["one", "two"]); |
| 80 | |
| 81 | // Get the first row and add a new cell, which would create a new column. |
| 82 | let row = table.row_mut(0).unwrap(); |
| 83 | row.add_cell("three".into()); |
| 84 | |
| 85 | // The table cannot know about the new cell yet, which is why we expect two columns. |
| 86 | assert_eq!(table.columns.len(), 2); |
| 87 | |
| 88 | // After scanning for new columns however, it should show up. |
| 89 | table.discover_columns(); |
| 90 | assert_eq!(table.columns.len(), 3); |
| 91 | } |
| 92 | } |