1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
use crate::style::TableComponent;
use crate::table::Table;
use crate::utils::ColumnDisplayInfo;

pub(crate) fn draw_borders(
    table: &Table,
    rows: &[Vec<Vec<String>>],
    display_info: &[ColumnDisplayInfo],
) -> Vec<String> {
    // We know how many lines there should be. Initialize the vector with the rough correct amount.
    // We might over allocate a bit, but that's better than under allocating.
    let mut lines = if let Some(capacity) = rows.first().map(|lines| lines.len()) {
        // Lines * 2 -> Lines + delimiters
        // + 5 -> header delimiters + header + bottom/top borders
        Vec::with_capacity(capacity * 2 + 5)
    } else {
        Vec::new()
    };

    if should_draw_top_border(table) {
        lines.push(draw_top_border(table, display_info));
    }

    draw_rows(&mut lines, rows, table, display_info);

    if should_draw_bottom_border(table) {
        lines.push(draw_bottom_border(table, display_info));
    }

    lines
}

fn draw_top_border(table: &Table, display_info: &[ColumnDisplayInfo]) -> String {
    let left_corner = table.style_or_default(TableComponent::TopLeftCorner);
    let top_border = table.style_or_default(TableComponent::TopBorder);
    let intersection = table.style_or_default(TableComponent::TopBorderIntersections);
    let right_corner = table.style_or_default(TableComponent::TopRightCorner);

    let mut line = String::new();
    // We only need the top left corner, if we need to draw a left border
    if should_draw_left_border(table) {
        line += &left_corner;
    }

    // Build the top border line depending on the columns' width.
    // Also add the border intersections.
    let mut first = true;
    for info in display_info.iter() {
        // Only add something, if the column isn't hidden
        if !info.is_hidden {
            if !first {
                line += &intersection;
            }
            line += &top_border.repeat(info.width().into());
            first = false;
        }
    }

    // We only need the top right corner, if we need to draw a right border
    if should_draw_right_border(table) {
        line += &right_corner;
    }

    line
}

fn draw_rows(
    lines: &mut Vec<String>,
    rows: &[Vec<Vec<String>>],
    table: &Table,
    display_info: &[ColumnDisplayInfo],
) {
    // Iterate over all rows
    let mut row_iter = rows.iter().enumerate().peekable();
    while let Some((row_index, row)) = row_iter.next() {
        // Concatenate the line parts and insert the vertical borders if needed
        for line_parts in row.iter() {
            lines.push(embed_line(line_parts, table));
        }

        // Draw the horizontal header line if desired, otherwise continue to the next iteration
        if row_index == 0 && table.header.is_some() {
            if should_draw_header(table) {
                lines.push(draw_horizontal_lines(table, display_info, true));
            }
            continue;
        }

        // Draw a horizontal line, if we desired and if we aren't in the last row of the table.
        if row_iter.peek().is_some() && should_draw_horizontal_lines(table) {
            lines.push(draw_horizontal_lines(table, display_info, false));
        }
    }
}

// Takes the parts of a single line, surrounds them with borders and adds vertical lines.
fn embed_line(line_parts: &[String], table: &Table) -> String {
    let vertical_lines = table.style_or_default(TableComponent::VerticalLines);
    let left_border = table.style_or_default(TableComponent::LeftBorder);
    let right_border = table.style_or_default(TableComponent::RightBorder);

    let mut line = String::new();
    if should_draw_left_border(table) {
        line += &left_border;
    }

    let mut part_iter = line_parts.iter().peekable();
    while let Some(part) = part_iter.next() {
        line += part;
        if should_draw_vertical_lines(table) && part_iter.peek().is_some() {
            line += &vertical_lines;
        } else if should_draw_right_border(table) && part_iter.peek().is_none() {
            line += &right_border;
        }
    }

    line
}

// The horizontal line that separates between rows.
fn draw_horizontal_lines(
    table: &Table,
    display_info: &[ColumnDisplayInfo],
    header: bool,
) -> String {
    // Styling depends on whether we're currently on the header line or not.
    let (left_intersection, horizontal_lines, middle_intersection, right_intersection) = if header {
        (
            table.style_or_default(TableComponent::LeftHeaderIntersection),
            table.style_or_default(TableComponent::HeaderLines),
            table.style_or_default(TableComponent::MiddleHeaderIntersections),
            table.style_or_default(TableComponent::RightHeaderIntersection),
        )
    } else {
        (
            table.style_or_default(TableComponent::LeftBorderIntersections),
            table.style_or_default(TableComponent::HorizontalLines),
            table.style_or_default(TableComponent::MiddleIntersections),
            table.style_or_default(TableComponent::RightBorderIntersections),
        )
    };

    let mut line = String::new();
    // We only need the bottom left corner, if we need to draw a left border
    if should_draw_left_border(table) {
        line += &left_intersection;
    }

    // Append the middle lines depending on the columns' widths.
    // Also add the middle intersections.
    let mut first = true;
    for info in display_info.iter() {
        // Only add something, if the column isn't hidden
        if !info.is_hidden {
            if !first {
                line += &middle_intersection;
            }
            line += &horizontal_lines.repeat(info.width().into());
            first = false;
        }
    }

    // We only need the bottom right corner, if we need to draw a right border
    if should_draw_right_border(table) {
        line += &right_intersection;
    }

    line
}

fn draw_bottom_border(table: &Table, display_info: &[ColumnDisplayInfo]) -> String {
    let left_corner = table.style_or_default(TableComponent::BottomLeftCorner);
    let bottom_border = table.style_or_default(TableComponent::BottomBorder);
    let middle_intersection = table.style_or_default(TableComponent::BottomBorderIntersections);
    let right_corner = table.style_or_default(TableComponent::BottomRightCorner);

    let mut line = String::new();
    // We only need the bottom left corner, if we need to draw a left border
    if should_draw_left_border(table) {
        line += &left_corner;
    }

    // Add the bottom border lines depending on column width
    // Also add the border intersections.
    let mut first = true;
    for info in display_info.iter() {
        // Only add something, if the column isn't hidden
        if !info.is_hidden {
            if !first {
                line += &middle_intersection;
            }
            line += &bottom_border.repeat(info.width().into());
            first = false;
        }
    }

    // We only need the bottom right corner, if we need to draw a right border
    if should_draw_right_border(table) {
        line += &right_corner;
    }

    line
}

fn should_draw_top_border(table: &Table) -> bool {
    if table.style_exists(TableComponent::TopLeftCorner)
        || table.style_exists(TableComponent::TopBorder)
        || table.style_exists(TableComponent::TopBorderIntersections)
        || table.style_exists(TableComponent::TopRightCorner)
    {
        return true;
    }

    false
}

fn should_draw_bottom_border(table: &Table) -> bool {
    if table.style_exists(TableComponent::BottomLeftCorner)
        || table.style_exists(TableComponent::BottomBorder)
        || table.style_exists(TableComponent::BottomBorderIntersections)
        || table.style_exists(TableComponent::BottomRightCorner)
    {
        return true;
    }

    false
}

pub fn should_draw_left_border(table: &Table) -> bool {
    if table.style_exists(TableComponent::TopLeftCorner)
        || table.style_exists(TableComponent::LeftBorder)
        || table.style_exists(TableComponent::LeftBorderIntersections)
        || table.style_exists(TableComponent::LeftHeaderIntersection)
        || table.style_exists(TableComponent::BottomLeftCorner)
    {
        return true;
    }

    false
}

pub fn should_draw_right_border(table: &Table) -> bool {
    if table.style_exists(TableComponent::TopRightCorner)
        || table.style_exists(TableComponent::RightBorder)
        || table.style_exists(TableComponent::RightBorderIntersections)
        || table.style_exists(TableComponent::RightHeaderIntersection)
        || table.style_exists(TableComponent::BottomRightCorner)
    {
        return true;
    }

    false
}

fn should_draw_horizontal_lines(table: &Table) -> bool {
    if table.style_exists(TableComponent::LeftBorderIntersections)
        || table.style_exists(TableComponent::HorizontalLines)
        || table.style_exists(TableComponent::MiddleIntersections)
        || table.style_exists(TableComponent::RightBorderIntersections)
    {
        return true;
    }

    false
}

pub fn should_draw_vertical_lines(table: &Table) -> bool {
    if table.style_exists(TableComponent::TopBorderIntersections)
        || table.style_exists(TableComponent::MiddleHeaderIntersections)
        || table.style_exists(TableComponent::VerticalLines)
        || table.style_exists(TableComponent::MiddleIntersections)
        || table.style_exists(TableComponent::BottomBorderIntersections)
    {
        return true;
    }

    false
}

fn should_draw_header(table: &Table) -> bool {
    if table.style_exists(TableComponent::LeftHeaderIntersection)
        || table.style_exists(TableComponent::HeaderLines)
        || table.style_exists(TableComponent::MiddleHeaderIntersections)
        || table.style_exists(TableComponent::RightHeaderIntersection)
    {
        return true;
    }

    false
}