comfy-table-inline 8.0.2

A fork of comfy-table by Arne Beer adding inline table support.
Documentation
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
use std::iter::repeat_n;

use crate::{
    table::Table,
    utils::{
        ColumnDisplayInfo, arrangement::arrange_content, formatting::content_format::BuildTableItem,
    },
};

pub(crate) fn draw_borders(
    table: &Table,
    rows: &[BuildTableItem],
    display_info: &[ColumnDisplayInfo],
    last_inline_table_of_higherst_level: bool,
    is_inline_table: bool,
    very_last_border: &mut Option<String>,
) -> 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 table.style.has_top_border() {
        lines.push(draw_top_border(table, display_info));
    }

    draw_rows(
        &mut lines,
        rows,
        table,
        display_info,
        last_inline_table_of_higherst_level,
        is_inline_table,
        very_last_border,
    );

    if !is_inline_table
        && let Some(border) = very_last_border
        && table.style.has_bottom_border()
    {
        lines.push(border.to_string());
    } else if table.style.has_bottom_border() {
        lines.push(draw_bottom_border(table, display_info));
    }

    lines
}

fn draw_top_border(table: &Table, display_info: &[ColumnDisplayInfo]) -> String {
    let left_corner = table.style.top_border.left.unwrap_or(' ');
    let top_border = table.style.top_border.fill.unwrap_or(' ');
    let intersection = table.style.top_border.junction.unwrap_or(' ');
    let right_corner = table.style.top_border.right.unwrap_or(' ');

    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.push(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.push(intersection);
            }
            line.extend(repeat_n(top_border, 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.push(right_corner);
    }

    line
}

fn draw_top_inline_border(
    table: &Table,
    display_info: &[ColumnDisplayInfo],
    connect_above_idx: &Vec<usize>,
) -> String {
    let left_corner = table.style.top_inline_border.left.unwrap_or(' ');
    let top_border = table.style.top_inline_border.fill.unwrap_or(' ');
    let intersection = table.style.top_inline_border.junction.unwrap_or(' ');
    let right_corner = table.style.top_inline_border.right.unwrap_or(' ');

    let connector = table.style.bottom_inline_border.junction.unwrap_or(' ');

    let intersection_connector = table.style.header_separator.junction.unwrap_or(' ');

    let mut line: Vec<char> = Vec::new();
    // We only need the top left corner, if we need to draw a left border
    if should_draw_left_border(table) {
        line.push(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.push(intersection);
            }
            line.extend(repeat_n(top_border, 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.push(right_corner);
    }

    for connection in connect_above_idx {
        if connection < &line.len() {
            if line[*connection] == intersection {
                line[*connection] = intersection_connector;
            } else {
                line[*connection] = connector;
            }
        }
    }

    line.into_iter().collect()
}

fn draw_bottom_inline_border(
    table: &Table,
    display_info: &[ColumnDisplayInfo],
    connect_below_idx: &Vec<usize>,
) -> String {
    let left_corner = table.style.bottom_inline_border.left.unwrap_or(' ');
    let bottom_border = table.style.bottom_inline_border.fill.unwrap_or(' ');
    let intersection = table.style.bottom_inline_border.junction.unwrap_or(' ');
    let right_corner = table.style.bottom_inline_border.right.unwrap_or(' ');

    let connector = table.style.top_inline_border.junction.unwrap_or(' ');

    let intersection_connector = table.style.header_separator.junction.unwrap_or(' ');

    let mut line = Vec::new();
    // We only need the bottom left corner, if we need to draw a left border
    if should_draw_left_border(table) {
        line.push(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.push(intersection);
            }
            line.extend(repeat_n(bottom_border, 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.push(right_corner);
    }

    for connection in connect_below_idx {
        if connection < &line.len() {
            if line[*connection] == intersection {
                line[*connection] = intersection_connector;
            } else {
                line[*connection] = connector;
            }
        }
    }

    line.into_iter().collect()
}

fn draw_rows(
    lines: &mut Vec<String>,
    rows: &[BuildTableItem],
    table: &Table,
    display_info: &[ColumnDisplayInfo],
    last_inline_table_of_higherst_level: bool,
    is_inline_table: bool,
    very_last_border: &mut Option<String>,
) {
    let draw_left_border = should_draw_left_border(table);
    let draw_right_border = should_draw_right_border(table);
    let draw_vertical_lines = should_draw_vertical_lines(table);

    // Iterate over all rows
    let mut row_iter = rows.iter().enumerate().peekable();
    while let Some((row_index, item)) = row_iter.next() {
        match item {
            BuildTableItem::Inline(inline) => {
                if row_index == 0 && table.header.is_none() && table.style.has_top_border() {
                    lines.pop();
                }
                // Apply arrangement, style and width from the parent table to the inner table.
                let style = table.style();
                let width = table.width();
                let arrangement = table.content_arrangement();
                let mut inline_table = inline.table.table.clone();
                inline_table.load_style(style);
                inline_table.set_content_arrangement(arrangement);
                if let Some(w) = width {
                    inline_table.set_width(w);
                }

                // Crate the display information
                let arranged_content = arrange_content(&inline_table);

                // Identify as last item of highest level and parent is highest level
                let is_last_item = row_iter.peek().is_none();
                let last_item_and_inline_table_of_higherst_level_check =
                    (!is_inline_table || last_inline_table_of_higherst_level) && is_last_item;
                    
                if table.style.has_bottom_border()
                    && last_item_and_inline_table_of_higherst_level_check
                {
                    *very_last_border = Some(draw_bottom_border(&inline_table, &arranged_content))
                }

                // Retreive inner table as a string vector.
                let mut draw_inner_table = draw_borders(
                    &inline_table,
                    &inline.items,
                    &arranged_content,
                    last_item_and_inline_table_of_higherst_level_check,
                    true,
                    very_last_border,
                );

                // Start manipulate borders so the inner table fitts to the parent

                // Manipulate the top border of the inline table if the line before is a row
                let mut connect_above_idx: Vec<usize> = Vec::new();
                if let Some(junction) = table.style.top_inline_border.junction {
                    // Find out where the junctions of the line above placed.
                    let line = draw_top_inline_border(table, display_info, &Vec::new());
                    let line_len = line.chars().count();
                    connect_above_idx = line
                        .chars()
                        .enumerate()
                        .filter(|(idx, c)| {
                            table.style.has_vertical_lines()
                                && *idx != 0
                                && *idx != line_len - 1
                                && *c == junction
                        })
                        .map(|(idx, _)| idx)
                        .collect();
                };

                // Redraw top and bottom border of inner table

                // Only redraw the top inline table border if it have one.
                if inline_table.style.has_top_inline_border() && row_index != 0 {
                    let inline_header_top_seperator = draw_top_inline_border(
                        &inline_table,
                        &arranged_content,
                        &connect_above_idx,
                    );
                    if table.style.has_top_border() {
                        draw_inner_table[0] = inline_header_top_seperator;
                    } else {
                        draw_inner_table.insert(0, inline_header_top_seperator);
                    }
                }

                if inline_table.style.has_bottom_inline_border() {
                    let inline_bootom_seperator = draw_bottom_inline_border(
                        &inline_table,
                        &arranged_content,
                        &connect_above_idx,
                    );
                    if table.style.has_bottom_border() {
                        draw_inner_table.pop();
                    }

                    // Only redraw the bottom inline table border if it have one.
                    if !last_item_and_inline_table_of_higherst_level_check {
                        draw_inner_table.push(inline_bootom_seperator);
                        // To complete the connections, we draw a row seperator if the parent have one and we are not in the last row
                        if table.style.has_row_separator()
                            && table.style.has_bottom_border()
                            && row_iter.peek().is_some()
                        {
                            draw_inner_table.push(draw_horizontal_lines(
                                table,
                                display_info,
                                false,
                            ));
                        }
                    }
                }

                // Append the inner table to the draw vector.
                lines.append(&mut draw_inner_table);
            }
            BuildTableItem::Row(row) => {
                // Styling depends on whether we're currently in the header or not.
                let style = if row_index == 0 && table.header.is_some() {
                    table.style.header_lines
                } else {
                    table.style.content_lines
                };
                let left_border = style.left.unwrap_or(' ');
                let vertical_lines = style.junction.unwrap_or(' ');
                let right_border = style.right.unwrap_or(' ');

                // Concatenate the line parts and insert the vertical borders if needed
                for line_parts in row.iter() {
                    let mut line = String::new();
                    if draw_left_border {
                        line.push(left_border);
                    }

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

                    lines.push(line);
                }

                // Draw the horizontal header line if desired, otherwise continue to the next iteration
                if row_index == 0 && table.header.is_some() {
                    if table.style.has_header_separator() {
                        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() && table.style.has_row_separator() {
                    lines.push(draw_horizontal_lines(table, display_info, false));
                }
            }
        }
    }
}

// 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 separator = if header {
        table.style.header_separator
    } else {
        table.style.row_separator
    };
    let left_intersection = separator.left.unwrap_or(' ');
    let horizontal_lines = separator.fill.unwrap_or(' ');
    let middle_intersection = separator.junction.unwrap_or(' ');
    let right_intersection = separator.right.unwrap_or(' ');

    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.push(left_intersection);
    }

    let draw_vertical_lines = should_draw_vertical_lines(table);

    // 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 && draw_vertical_lines {
                line.push(middle_intersection);
            }
            line.extend(repeat_n(horizontal_lines, 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.push(right_intersection);
    }

    line
}

fn draw_bottom_border(table: &Table, display_info: &[ColumnDisplayInfo]) -> String {
    let left_corner = table.style.bottom_border.left.unwrap_or(' ');
    let bottom_border = table.style.bottom_border.fill.unwrap_or(' ');
    let middle_intersection = table.style.bottom_border.junction.unwrap_or(' ');
    let right_corner = table.style.bottom_border.right.unwrap_or(' ');

    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.push(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.push(middle_intersection);
            }
            line.extend(repeat_n(bottom_border, 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.push(right_corner);
    }

    line
}

pub fn should_draw_left_border(table: &Table) -> bool {
    table.style.has_left_border()
}

pub fn should_draw_right_border(table: &Table) -> bool {
    table.style.has_right_border()
}

pub fn should_draw_vertical_lines(table: &Table) -> bool {
    table.style.has_vertical_lines()
}