| 1 | use super::DisplayInfos; |
| 2 | use crate::{ |
| 3 | Cell, Column, Table, |
| 4 | utils::formatting::borders::{ |
| 5 | should_draw_left_border, should_draw_right_border, should_draw_vertical_lines, |
| 6 | }, |
| 7 | }; |
| 8 | |
| 9 | /// The ColumnDisplayInfo works with a fixed value for content width. |
| 10 | /// However, if a column is supposed to get a absolute width, we have to make sure that |
| 11 | /// the padding on top of the content width doesn't get larger than the specified absolute width. |
| 12 | /// |
| 13 | /// For this reason, we take the targeted width, subtract the column's padding and make sure that |
| 14 | /// the content width is always a minimum of 1 |
| 15 | pub fn absolute_width_with_padding(column: &Column, width: u16) -> u16 { |
| 16 | let mut content_width = width |
| 17 | .saturating_sub(column.padding.0) |
| 18 | .saturating_sub(column.padding.1); |
| 19 | if content_width == 0 { |
| 20 | content_width = 1; |
| 21 | } |
| 22 | |
| 23 | content_width |
| 24 | } |
| 25 | |
| 26 | /// Return the amount of visible columns |
| 27 | pub fn count_visible_columns(columns: &[Column]) -> usize { |
| 28 | columns.iter().filter(|column| !column.is_hidden()).count() |
| 29 | } |
| 30 | |
| 31 | /// Return the amount of visible columns that haven't been checked yet. |
| 32 | /// |
| 33 | /// - `column_count` is the total amount of columns that are visible, calculated with |
| 34 | /// [count_visible_columns]. |
| 35 | /// - `infos` are all columns that have already been fixed in size or are hidden. |
| 36 | pub fn count_remaining_columns(column_count: usize, infos: &DisplayInfos) -> usize { |
| 37 | column_count - infos.iter().filter(|(_, info)| !info.is_hidden).count() |
| 38 | } |
| 39 | |
| 40 | /// Return the amount of border columns, that will be visible in the final table output. |
| 41 | pub fn count_border_columns(table: &Table, visible_columns: usize) -> usize { |
| 42 | let mut lines = 0; |
| 43 | // Remove space occupied by borders from remaining_width |
| 44 | if should_draw_left_border(table) { |
| 45 | lines += 1; |
| 46 | } |
| 47 | if should_draw_right_border(table) { |
| 48 | lines += 1; |
| 49 | } |
| 50 | if should_draw_vertical_lines(table) { |
| 51 | lines += visible_columns.saturating_sub(1); |
| 52 | } |
| 53 | |
| 54 | lines |
| 55 | } |
| 56 | |
| 57 | /// Get the delimiter for a Cell. |
| 58 | /// Priority is in decreasing order: Cell -> Column -> Table. |
| 59 | pub fn delimiter(table: &Table, column: &Column, cell: &Cell) -> char { |
| 60 | // Determine, which delimiter should be used |
| 61 | if let Some(delimiter) = cell.delimiter { |
| 62 | delimiter |
| 63 | } else if let Some(delimiter) = column.delimiter { |
| 64 | delimiter |
| 65 | } else if let Some(delimiter) = table.delimiter { |
| 66 | delimiter |
| 67 | } else { |
| 68 | ' ' |
| 69 | } |
| 70 | } |