| 1 | use crate::{style::TableComponent, table::Table, utils::ColumnDisplayInfo}; |
| 2 | |
| 3 | pub(crate) fn draw_borders( |
| 4 | table: &Table, |
| 5 | rows: &[Vec<Vec<String>>], |
| 6 | display_info: &[ColumnDisplayInfo], |
| 7 | ) -> Vec<String> { |
| 8 | // We know how many lines there should be. Initialize the vector with the rough correct amount. |
| 9 | // We might over allocate a bit, but that's better than under allocating. |
| 10 | let mut lines = if let Some(capacity) = rows.first().map(|lines| lines.len()) { |
| 11 | // Lines * 2 -> Lines + delimiters |
| 12 | // + 5 -> header delimiters + header + bottom/top borders |
| 13 | Vec::with_capacity(capacity * 2 + 5) |
| 14 | } else { |
| 15 | Vec::new() |
| 16 | }; |
| 17 | |
| 18 | if should_draw_top_border(table) { |
| 19 | lines.push(draw_top_border(table, display_info)); |
| 20 | } |
| 21 | |
| 22 | draw_rows(&mut lines, rows, table, display_info); |
| 23 | |
| 24 | if should_draw_bottom_border(table) { |
| 25 | lines.push(draw_bottom_border(table, display_info)); |
| 26 | } |
| 27 | |
| 28 | lines |
| 29 | } |
| 30 | |
| 31 | fn draw_top_border(table: &Table, display_info: &[ColumnDisplayInfo]) -> String { |
| 32 | let left_corner = table.style_or_default(TableComponent::TopLeftCorner); |
| 33 | let top_border = table.style_or_default(TableComponent::TopBorder); |
| 34 | let intersection = table.style_or_default(TableComponent::TopBorderIntersections); |
| 35 | let right_corner = table.style_or_default(TableComponent::TopRightCorner); |
| 36 | |
| 37 | let mut line = String::new(); |
| 38 | // We only need the top left corner, if we need to draw a left border |
| 39 | if should_draw_left_border(table) { |
| 40 | line += &left_corner; |
| 41 | } |
| 42 | |
| 43 | // Build the top border line depending on the columns' width. |
| 44 | // Also add the border intersections. |
| 45 | let mut first = true; |
| 46 | for info in display_info.iter() { |
| 47 | // Only add something, if the column isn't hidden |
| 48 | if !info.is_hidden { |
| 49 | if !first { |
| 50 | line += &intersection; |
| 51 | } |
| 52 | line += &top_border.repeat(info.width().into()); |
| 53 | first = false; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // We only need the top right corner, if we need to draw a right border |
| 58 | if should_draw_right_border(table) { |
| 59 | line += &right_corner; |
| 60 | } |
| 61 | |
| 62 | line |
| 63 | } |
| 64 | |
| 65 | fn draw_rows( |
| 66 | lines: &mut Vec<String>, |
| 67 | rows: &[Vec<Vec<String>>], |
| 68 | table: &Table, |
| 69 | display_info: &[ColumnDisplayInfo], |
| 70 | ) { |
| 71 | // Iterate over all rows |
| 72 | let mut row_iter = rows.iter().enumerate().peekable(); |
| 73 | while let Some((row_index, row)) = row_iter.next() { |
| 74 | // Concatenate the line parts and insert the vertical borders if needed |
| 75 | for line_parts in row.iter() { |
| 76 | lines.push(embed_line(line_parts, table)); |
| 77 | } |
| 78 | |
| 79 | // Draw the horizontal header line if desired, otherwise continue to the next iteration |
| 80 | if row_index == 0 && table.header.is_some() { |
| 81 | if should_draw_header(table) { |
| 82 | lines.push(draw_horizontal_lines(table, display_info, true)); |
| 83 | } |
| 84 | continue; |
| 85 | } |
| 86 | |
| 87 | // Draw a horizontal line, if we desired and if we aren't in the last row of the table. |
| 88 | if row_iter.peek().is_some() && should_draw_horizontal_lines(table) { |
| 89 | lines.push(draw_horizontal_lines(table, display_info, false)); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // Takes the parts of a single line, surrounds them with borders and adds vertical lines. |
| 95 | fn embed_line(line_parts: &[String], table: &Table) -> String { |
| 96 | let vertical_lines = table.style_or_default(TableComponent::VerticalLines); |
| 97 | let left_border = table.style_or_default(TableComponent::LeftBorder); |
| 98 | let right_border = table.style_or_default(TableComponent::RightBorder); |
| 99 | |
| 100 | let mut line = String::new(); |
| 101 | if should_draw_left_border(table) { |
| 102 | line += &left_border; |
| 103 | } |
| 104 | |
| 105 | let mut part_iter = line_parts.iter().peekable(); |
| 106 | while let Some(part) = part_iter.next() { |
| 107 | line += part; |
| 108 | if part_iter.peek().is_none() && should_draw_right_border(table) { |
| 109 | line += &right_border; |
| 110 | } else if part_iter.peek().is_some() && should_draw_vertical_lines(table) { |
| 111 | line += &vertical_lines; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | line |
| 116 | } |
| 117 | |
| 118 | // The horizontal line that separates between rows. |
| 119 | fn draw_horizontal_lines( |
| 120 | table: &Table, |
| 121 | display_info: &[ColumnDisplayInfo], |
| 122 | header: bool, |
| 123 | ) -> String { |
| 124 | // Styling depends on whether we're currently on the header line or not. |
| 125 | let (left_intersection, horizontal_lines, middle_intersection, right_intersection) = if header { |
| 126 | ( |
| 127 | table.style_or_default(TableComponent::LeftHeaderIntersection), |
| 128 | table.style_or_default(TableComponent::HeaderLines), |
| 129 | table.style_or_default(TableComponent::MiddleHeaderIntersections), |
| 130 | table.style_or_default(TableComponent::RightHeaderIntersection), |
| 131 | ) |
| 132 | } else { |
| 133 | ( |
| 134 | table.style_or_default(TableComponent::LeftBorderIntersections), |
| 135 | table.style_or_default(TableComponent::HorizontalLines), |
| 136 | table.style_or_default(TableComponent::MiddleIntersections), |
| 137 | table.style_or_default(TableComponent::RightBorderIntersections), |
| 138 | ) |
| 139 | }; |
| 140 | |
| 141 | let mut line = String::new(); |
| 142 | // We only need the bottom left corner, if we need to draw a left border |
| 143 | if should_draw_left_border(table) { |
| 144 | line += &left_intersection; |
| 145 | } |
| 146 | |
| 147 | let draw_vertical_lines = should_draw_vertical_lines(table); |
| 148 | |
| 149 | // Append the middle lines depending on the columns' widths. |
| 150 | // Also add the middle intersections. |
| 151 | let mut first = true; |
| 152 | for info in display_info.iter() { |
| 153 | // Only add something, if the column isn't hidden |
| 154 | if !info.is_hidden { |
| 155 | if !first && draw_vertical_lines { |
| 156 | line += &middle_intersection; |
| 157 | } |
| 158 | line += &horizontal_lines.repeat(info.width().into()); |
| 159 | first = false; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // We only need the bottom right corner, if we need to draw a right border |
| 164 | if should_draw_right_border(table) { |
| 165 | line += &right_intersection; |
| 166 | } |
| 167 | |
| 168 | line |
| 169 | } |
| 170 | |
| 171 | fn draw_bottom_border(table: &Table, display_info: &[ColumnDisplayInfo]) -> String { |
| 172 | let left_corner = table.style_or_default(TableComponent::BottomLeftCorner); |
| 173 | let bottom_border = table.style_or_default(TableComponent::BottomBorder); |
| 174 | let middle_intersection = table.style_or_default(TableComponent::BottomBorderIntersections); |
| 175 | let right_corner = table.style_or_default(TableComponent::BottomRightCorner); |
| 176 | |
| 177 | let mut line = String::new(); |
| 178 | // We only need the bottom left corner, if we need to draw a left border |
| 179 | if should_draw_left_border(table) { |
| 180 | line += &left_corner; |
| 181 | } |
| 182 | |
| 183 | // Add the bottom border lines depending on column width |
| 184 | // Also add the border intersections. |
| 185 | let mut first = true; |
| 186 | for info in display_info.iter() { |
| 187 | // Only add something, if the column isn't hidden |
| 188 | if !info.is_hidden { |
| 189 | if !first { |
| 190 | line += &middle_intersection; |
| 191 | } |
| 192 | line += &bottom_border.repeat(info.width().into()); |
| 193 | first = false; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | // We only need the bottom right corner, if we need to draw a right border |
| 198 | if should_draw_right_border(table) { |
| 199 | line += &right_corner; |
| 200 | } |
| 201 | |
| 202 | line |
| 203 | } |
| 204 | |
| 205 | fn should_draw_top_border(table: &Table) -> bool { |
| 206 | if table.style_exists(TableComponent::TopLeftCorner) |
| 207 | || table.style_exists(TableComponent::TopBorder) |
| 208 | || table.style_exists(TableComponent::TopBorderIntersections) |
| 209 | || table.style_exists(TableComponent::TopRightCorner) |
| 210 | { |
| 211 | return true; |
| 212 | } |
| 213 | |
| 214 | false |
| 215 | } |
| 216 | |
| 217 | fn should_draw_bottom_border(table: &Table) -> bool { |
| 218 | if table.style_exists(TableComponent::BottomLeftCorner) |
| 219 | || table.style_exists(TableComponent::BottomBorder) |
| 220 | || table.style_exists(TableComponent::BottomBorderIntersections) |
| 221 | || table.style_exists(TableComponent::BottomRightCorner) |
| 222 | { |
| 223 | return true; |
| 224 | } |
| 225 | |
| 226 | false |
| 227 | } |
| 228 | |
| 229 | pub fn should_draw_left_border(table: &Table) -> bool { |
| 230 | if table.style_exists(TableComponent::TopLeftCorner) |
| 231 | || table.style_exists(TableComponent::LeftBorder) |
| 232 | || table.style_exists(TableComponent::LeftBorderIntersections) |
| 233 | || table.style_exists(TableComponent::LeftHeaderIntersection) |
| 234 | || table.style_exists(TableComponent::BottomLeftCorner) |
| 235 | { |
| 236 | return true; |
| 237 | } |
| 238 | |
| 239 | false |
| 240 | } |
| 241 | |
| 242 | pub fn should_draw_right_border(table: &Table) -> bool { |
| 243 | if table.style_exists(TableComponent::TopRightCorner) |
| 244 | || table.style_exists(TableComponent::RightBorder) |
| 245 | || table.style_exists(TableComponent::RightBorderIntersections) |
| 246 | || table.style_exists(TableComponent::RightHeaderIntersection) |
| 247 | || table.style_exists(TableComponent::BottomRightCorner) |
| 248 | { |
| 249 | return true; |
| 250 | } |
| 251 | |
| 252 | false |
| 253 | } |
| 254 | |
| 255 | fn should_draw_horizontal_lines(table: &Table) -> bool { |
| 256 | if table.style_exists(TableComponent::LeftBorderIntersections) |
| 257 | || table.style_exists(TableComponent::HorizontalLines) |
| 258 | || table.style_exists(TableComponent::MiddleIntersections) |
| 259 | || table.style_exists(TableComponent::RightBorderIntersections) |
| 260 | { |
| 261 | return true; |
| 262 | } |
| 263 | |
| 264 | false |
| 265 | } |
| 266 | |
| 267 | pub fn should_draw_vertical_lines(table: &Table) -> bool { |
| 268 | if table.style_exists(TableComponent::TopBorderIntersections) |
| 269 | || table.style_exists(TableComponent::MiddleHeaderIntersections) |
| 270 | || table.style_exists(TableComponent::VerticalLines) |
| 271 | || table.style_exists(TableComponent::MiddleIntersections) |
| 272 | || table.style_exists(TableComponent::BottomBorderIntersections) |
| 273 | { |
| 274 | return true; |
| 275 | } |
| 276 | |
| 277 | false |
| 278 | } |
| 279 | |
| 280 | fn should_draw_header(table: &Table) -> bool { |
| 281 | if table.style_exists(TableComponent::LeftHeaderIntersection) |
| 282 | || table.style_exists(TableComponent::HeaderLines) |
| 283 | || table.style_exists(TableComponent::MiddleHeaderIntersections) |
| 284 | || table.style_exists(TableComponent::RightHeaderIntersection) |
| 285 | { |
| 286 | return true; |
| 287 | } |
| 288 | |
| 289 | false |
| 290 | } |