• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MorganKryze / ConsoleAppVisuals / 8520738714

02 Apr 2024 09:44AM UTC coverage: 96.257%. Remained the same
8520738714

push

github

MorganKryze
📖 (Elements options) update videos

942 of 1051 branches covered (89.63%)

Branch coverage included in aggregate %.

2118 of 2128 relevant lines covered (99.53%)

251.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.62
/src/ConsoleAppVisuals/elements/passive_elements/TableView.cs
1
/*
2
    Copyright (c) 2024 Yann M. Vidamment (MorganKryze)
3
    Licensed under GNU GPL v2.0. See full license at: https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/LICENSE.md
4
*/
5
namespace ConsoleAppVisuals.PassiveElements;
6

7
/// <summary>
8
/// The <see cref="TableView"/> is a passive element that displays a table on the console.
9
/// </summary>
10
/// <remarks>
11
/// For more information, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
12
/// </remarks>
13
public class TableView : PassiveElement
14
{
15
    #region Constants
16
    const string DEFAULT_TITLE = null;
17
    const Placement DEFAULT_PLACEMENT = Placement.TopCenter;
18
    const BordersType DEFAULT_BORDERS_TYPE = BordersType.SingleStraight;
19
    #endregion
20

21
    #region Fields
22
    private string? _title;
23
    private List<string>? _rawHeaders;
24
    private List<List<string>>? _rawLines;
25
    private string[]? _displayArray;
26
    private Placement _placement;
27
    private readonly Borders _borders;
28
    #endregion
29

30
    #region Default Properties
31
    /// <summary>
32
    /// Gets the height of the table.
33
    /// </summary>
34
    public override int Height => _displayArray?.Length ?? 0;
9✔
35

36
    /// <summary>
37
    /// Gets the width of the table.
38
    /// </summary>
39
    public override int Width => _displayArray?.Max(x => x.Length) ?? 0;
1!
40

41
    /// <summary>
42
    /// Gets the title of the table.
43
    /// </summary>
44
    public override Placement Placement => _placement;
2✔
45
    #endregion
46

47
    #region Properties
48
    /// <summary>
49
    /// Gets the headers of the table.
50
    /// </summary>
51
    public List<string>? GetRawHeaders => _rawHeaders;
4✔
52

53
    /// <summary>
54
    /// Gets the lines of the table.
55
    /// </summary>
56
    public List<List<string>>? GetRawLines => _rawLines;
3✔
57

58
    /// <summary>
59
    /// Gets the number of lines in the table.
60
    /// </summary>
61
    public int Count => _rawLines?.Count ?? 0;
8✔
62

63
    /// <summary>
64
    /// Gets the borders of the table.
65
    /// </summary>
66
    public Borders Borders => _borders;
2,116✔
67

68
    /// <summary>
69
    /// Gets the border type of the selector.
70
    /// </summary>
71
    public BordersType BordersType => _borders.Type;
1✔
72
    #endregion
73

74
    #region Constructor
75
    /// <summary>
76
    /// The <see cref="TableView"/> is a passive element that displays a table on the console.
77
    /// </summary>
78
    /// <param name="title">The title of the table.</param>s
79
    /// <param name="headers">The headers of the table.</param>
80
    /// <param name="lines">The lines of the table.</param>
81
    /// <param name="placement">The placement of the table.</param>
82
    /// <param name="bordersType">The type of borders to use for the table.</param>
83
    /// <exception cref="ArgumentException">Is thrown when the number of columns in the table is not consistent with itself or with the headers.</exception>
84
    /// <exception cref="NullReferenceException">Is thrown when no body lines were provided.</exception>
85
    /// <remarks>
86
    /// For more information, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
87
    /// </remarks>
88
    public TableView(
59✔
89
        string? title = DEFAULT_TITLE,
59✔
90
        List<string>? headers = null,
59✔
91
        List<List<string>>? lines = null,
59✔
92
        Placement placement = DEFAULT_PLACEMENT,
59✔
93
        BordersType bordersType = DEFAULT_BORDERS_TYPE
59✔
94
    )
59✔
95
    {
96
        _title = title;
59✔
97
        _rawHeaders = headers;
59✔
98
        _rawLines = lines;
59✔
99
        _borders = new Borders(bordersType);
59✔
100
        _placement = placement;
59✔
101
        if (CompatibilityCheck())
59✔
102
        {
103
            BuildTable();
51✔
104
        }
105
    }
57✔
106
    #endregion
107

108
    #region Check Methods
109
    private bool CompatibilityCheck()
110
    {
111
        if (_rawHeaders is null)
62✔
112
        {
113
            return CheckRawLines();
7✔
114
        }
115
        else if (_rawLines is null)
55✔
116
        {
117
            return true;
1✔
118
        }
119
        else
120
        {
121
            return CheckRawHeadersAndLines();
54✔
122
        }
123
    }
124

125
    private bool CheckRawLines()
126
    {
127
        if (_rawLines is null || _rawLines.Count == 0)
7✔
128
        {
129
            return false;
2✔
130
        }
131

132
        for (int i = 0; i < _rawLines.Count; i++)
28✔
133
        {
134
            if (_rawLines[i].Count != _rawLines[0].Count)
10✔
135
            {
136
                throw new ArgumentException(
1✔
137
                    "The number of columns in the table is not consistent."
1✔
138
                );
1✔
139
            }
140
        }
141
        return true;
4✔
142
    }
143

144
    private bool CheckRawHeadersAndLines()
145
    {
146
        if (_rawLines is null || _rawLines.Count == 0)
54✔
147
        {
148
            return false;
4✔
149
        }
150

151
        if (_rawLines.Count > 0)
50✔
152
        {
153
            for (int i = 0; i < _rawLines.Count; i++)
284✔
154
            {
155
                if (_rawLines[i].Count != _rawHeaders?.Count)
94!
156
                {
157
                    throw new ArgumentException(
2✔
158
                        "The number of columns in the table is not consistent(Headers or Lines)."
2✔
159
                    );
2✔
160
                }
161
            }
162
        }
163
        return true;
48✔
164
    }
165
    #endregion
166

167
    #region Update Methods
168
    /// <summary>
169
    /// Updates the placement of the table.
170
    /// </summary>
171
    /// <param name="placement">The new placement of the table.</param>
172
    /// <remarks>
173
    /// For more information, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
174
    /// </remarks>
175
    public void UpdatePlacement(Placement placement)
176
    {
177
        _placement = placement;
1✔
178
        BuildTable();
1✔
179
    }
1✔
180

181
    /// <summary>
182
    /// Updates the borders of the table.
183
    /// </summary>
184
    /// <param name="bordersType">The type of border to use for the table.</param>
185
    /// <remarks>
186
    /// For more information, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
187
    /// </remarks>
188
    public void UpdateBordersType(BordersType bordersType)
189
    {
190
        _borders.UpdateBordersType(bordersType);
1✔
191
        BuildTable();
1✔
192
    }
1✔
193

194
    /// <summary>
195
    /// Adds a title to the table.
196
    /// </summary>
197
    /// <param name="title">The title to add.</param>
198
    /// <remarks>
199
    /// For more information, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
200
    /// </remarks>
201
    public void AddTitle(string title)
202
    {
203
        _title = title;
2✔
204
        BuildTable();
2✔
205
    }
2✔
206

207
    /// <summary>
208
    /// Updates the title of the table.
209
    /// </summary>
210
    /// <param name="title">The title to update.</param>
211
    /// <remarks>
212
    /// For more information, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
213
    /// </remarks>
214
    public void UpdateTitle(string title)
215
    {
216
        AddTitle(title);
1✔
217
    }
1✔
218

219
    /// <summary>
220
    /// Clears the title of the table.
221
    /// </summary>
222
    /// <remarks>
223
    /// For more information, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
224
    /// </remarks>
225
    public void ClearTitle()
226
    {
227
        _title = null;
3✔
228
        BuildTable();
3✔
229
    }
3✔
230
    #endregion
231

232
    #region Manipulation Methods
233
    /// <summary>
234
    /// Adds headers to the table.
235
    /// </summary>
236
    /// <param name="headers">The headers to add.</param>
237
    /// <remarks>
238
    /// For more information, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
239
    /// </remarks>
240
    public void AddHeaders(List<string> headers)
241
    {
242
        _rawHeaders = headers;
3✔
243
        if (CompatibilityCheck())
3✔
244
        {
245
            BuildTable();
2✔
246
        }
247
    }
2✔
248

249
    /// <summary>
250
    /// Updates the headers of the table.
251
    /// </summary>
252
    /// <param name="headers">The headers to update.</param>
253
    /// <remarks>
254
    /// For more information, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
255
    /// </remarks>
256
    public void UpdateHeaders(List<string> headers)
257
    {
258
        AddHeaders(headers);
1✔
259
    }
1✔
260

261
    /// <summary>
262
    /// Clears the headers of the table.
263
    /// </summary>
264
    /// <remarks>
265
    /// For more information, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
266
    /// </remarks>
267
    public void ClearHeaders()
268
    {
269
        _rawHeaders = null;
4✔
270
        BuildTable();
4✔
271
    }
4✔
272

273
    /// <summary>
274
    /// Adds a line to the table.
275
    /// </summary>
276
    /// <param name="line">The line to add.</param>
277
    /// <exception cref="ArgumentException">Is thrown when the number of columns in the table is not consistent with itself or with the headers.</exception>
278
    /// <remarks>
279
    /// For more information, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
280
    /// </remarks>
281
    public void AddLine(List<string> line)
282
    {
283
        if (_rawLines?.Count > 0 && line.Count != _rawLines[0].Count)
4✔
284
        {
285
            throw new ArgumentException(
1✔
286
                "The number of columns in the table is not consistent with other lines."
1✔
287
            );
1✔
288
        }
289
        if (_rawHeaders is not null && line.Count != _rawHeaders.Count)
3✔
290
        {
291
            throw new ArgumentException(
1✔
292
                "The number of columns in the table is not consistent with the headers."
1✔
293
            );
1✔
294
        }
295
        _rawLines ??= new List<List<string>>();
2!
296
        _rawLines.Add(line);
2✔
297
        BuildTable();
2✔
298
    }
2✔
299

300
    /// <summary>
301
    /// Updates a line in the table.
302
    /// </summary>
303
    /// <param name="index">The index of the line to update.</param>
304
    /// <param name="line">The new line.</param>
305
    /// <exception cref="ArgumentOutOfRangeException">Is thrown when the index is out of range.</exception>
306
    /// <exception cref="ArgumentException">Is thrown when the number of columns in the table is not consistent with itself or with the headers.</exception>
307
    /// <remarks>
308
    /// For more information, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
309
    /// </remarks>
310
    public void UpdateLine(int index, List<string> line)
311
    {
312
        if (_rawLines?.Count > 0)
3!
313
        {
314
            if (index < 0 || index >= _rawLines.Count)
3✔
315
            {
316
                throw new ArgumentOutOfRangeException(nameof(index), "The index is out of range.");
1✔
317
            }
318

319
            if (line.Count != _rawHeaders?.Count)
2!
320
            {
321
                throw new ArgumentException(
1✔
322
                    "The number of columns in the table is not consistent."
1✔
323
                );
1✔
324
            }
325
        }
326
        _rawLines![index] = line;
1✔
327
        BuildTable();
1✔
328
    }
1✔
329

330
    /// <summary>
331
    /// Removes a line from the table.
332
    /// </summary>
333
    /// <param name="index">The index of the line to remove.</param>
334
    /// <exception cref="ArgumentOutOfRangeException">Is thrown when the index is out of range.</exception>
335
    /// <remarks>
336
    /// For more information, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
337
    /// </remarks>
338
    public void RemoveLine(int index)
339
    {
340
        if (_rawLines?.Count > 0 && (index < 0 || index >= _rawLines.Count))
2!
341
        {
342
            throw new ArgumentOutOfRangeException(nameof(index), "The index is out of range.");
1✔
343
        }
344

345
        _rawLines?.RemoveAt(index);
1!
346
        BuildTable();
1✔
347
    }
1✔
348

349
    /// <summary>
350
    /// Clears the lines of the table.
351
    /// </summary>
352
    /// <remarks>
353
    /// For more information, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
354
    /// </remarks>
355
    public void ClearLines()
356
    {
357
        _rawLines = null;
3✔
358
        BuildTable();
3✔
359
    }
3✔
360

361
    /// <summary>
362
    /// Gets the specified line in the table.
363
    /// </summary>
364
    /// <param name="index">The index of the line to return.</param>
365
    /// <returns>The line at the specified index.</returns>
366
    /// <exception cref="ArgumentOutOfRangeException">Is thrown when the index is out of range.</exception>
367
    /// <remarks>
368
    /// For more information, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
369
    /// </remarks>
370
    public List<string> GetLine(int index)
371
    {
372
        if (index < 0 || index >= _rawLines?.Count)
3!
373
        {
374
            throw new ArgumentOutOfRangeException(nameof(index), "The index is out of range.");
1✔
375
        }
376
        return _rawLines![index];
2✔
377
    }
378

379
    /// <summary>
380
    /// Gets all the elements from a column given its index.
381
    /// </summary>
382
    /// <param name="index">The index of the column.</param>
383
    /// <returns>The elements of the column.</returns>
384
    /// <exception cref="ArgumentOutOfRangeException">Is thrown when the index is out of range.</exception>
385
    /// <remarks>
386
    /// For more information, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
387
    /// </remarks>
388
    public List<string>? GetColumnData(int index)
389
    {
390
        if (_rawLines is null)
4✔
391
        {
392
            return null;
1✔
393
        }
394

395
        if (index < 0 || index >= _rawLines[0].Count)
3✔
396
        {
397
            throw new ArgumentOutOfRangeException(nameof(index), "Invalid column index.");
1✔
398
        }
399

400
        List<string>? list = new();
2✔
401
        for (int i = 0; i < _rawLines.Count; i++)
12✔
402
        {
403
            list.Add(_rawLines[i][index]);
4✔
404
        }
405
        return list;
2✔
406
    }
407

408
    /// <summary>
409
    /// Gets all the elements from a column given its header.
410
    /// </summary>
411
    /// <param name="header">The header of the column.</param>
412
    /// <returns>The elements of the column.</returns>
413
    /// <exception cref="InvalidOperationException">Is thrown when the table is empty.</exception>
414
    /// <exception cref="ArgumentOutOfRangeException">Is thrown when the header is invalid.</exception>
415
    /// <remarks>
416
    /// For more information, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
417
    /// </remarks>
418
    public List<string>? GetColumnData(string header)
419
    {
420
        if (_rawHeaders is null)
4✔
421
        {
422
            throw new InvalidOperationException("The headers are null.");
1✔
423
        }
424
        else if (_rawLines is null)
3✔
425
        {
426
            return null;
1✔
427
        }
428
        if (!_rawHeaders.Contains(header))
2✔
429
        {
430
            throw new ArgumentOutOfRangeException(nameof(header), "Invalid column header.");
1✔
431
        }
432

433
        return GetColumnData(_rawHeaders.IndexOf(header));
1✔
434
    }
435

436
    /// <summary>
437
    /// Clears the table.
438
    /// </summary>
439
    /// <remarks>
440
    /// For more information, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
441
    /// </remarks>
442
    public void Reset()
443
    {
444
        _title = null;
1✔
445
        _rawHeaders?.Clear();
1!
446
        _rawLines?.Clear();
1!
447
        _displayArray = null;
1✔
448
    }
1✔
449
    #endregion
450

451
    #region Rendering
452

453
    #region Build Methods
454
    private void BuildTable()
455
    {
456
        if (_rawHeaders is null)
71✔
457
        {
458
            if (_rawLines is not null)
10✔
459
            {
460
                BuildLines();
8✔
461
            }
462
        }
463
        else
464
        {
465
            if (_rawLines is null)
61✔
466
            {
467
                BuildHeaders();
2✔
468
            }
469
            else
470
            {
471
                BuildHeadersAndLines();
59✔
472
            }
473
        }
474
    }
61✔
475

476
    private void BuildHeadersAndLines()
477
    {
478
        if (_rawHeaders is not null && _rawLines is not null)
59✔
479
        {
480
            var stringList = new List<string>();
59✔
481
            var localMax = new int[_rawHeaders.Count];
59✔
482
            for (int i = 0; i < _rawHeaders.Count; i++)
472✔
483
            {
484
                if (_rawHeaders[i]?.Length > localMax[i])
177!
485
                {
486
                    localMax[i] = _rawHeaders[i]?.Length ?? 0;
177!
487
                }
488
            }
489

490
            for (int i = 0; i < _rawLines.Count; i++)
318✔
491
            {
492
                for (int j = 0; j < _rawLines[i].Count; j++)
800✔
493
                {
494
                    if (_rawLines[i][j]?.ToString()?.Length > localMax[j])
300!
495
                    {
496
                        localMax[j] = _rawLines[i][j]?.ToString()?.Length ?? 0;
×
497
                    }
498
                }
499
            }
500

501
            StringBuilder headerBuilder = new($"{Borders.Vertical} ");
59✔
502
            for (int i = 0; i < _rawHeaders.Count; i++)
472✔
503
            {
504
                headerBuilder.Append(_rawHeaders[i]?.PadRight(localMax[i]) ?? "");
177!
505
                if (i != _rawHeaders.Count - 1)
177✔
506
                {
507
                    headerBuilder.Append($" {Borders.Vertical} ");
118✔
508
                }
509
                else
510
                {
511
                    headerBuilder.Append($" {Borders.Vertical}");
59✔
512
                }
513
            }
514
            stringList.Add(headerBuilder.ToString());
59✔
515

516
            StringBuilder upperBorderBuilder = new(Borders.TopLeft.ToString());
59✔
517
            for (int i = 0; i < _rawHeaders.Count; i++)
472✔
518
            {
519
                upperBorderBuilder.Append(new string(Borders.Horizontal, localMax[i] + 2));
177✔
520
                upperBorderBuilder.Append(
177✔
521
                    (i != _rawHeaders.Count - 1)
177✔
522
                        ? Borders.Top.ToString()
177✔
523
                        : Borders.TopRight.ToString()
177✔
524
                );
177✔
525
            }
526
            stringList.Insert(0, upperBorderBuilder.ToString());
59✔
527

528
            StringBuilder intermediateBorderBuilder = new($"{Borders.Left}");
59✔
529
            for (int i = 0; i < _rawHeaders.Count; i++)
472✔
530
            {
531
                intermediateBorderBuilder.Append(new string(Borders.Horizontal, localMax[i] + 2));
177✔
532
                intermediateBorderBuilder.Append(
177✔
533
                    (i != _rawHeaders.Count - 1) ? Borders.Cross : Borders.Right
177✔
534
                );
177✔
535
            }
536
            stringList.Add(intermediateBorderBuilder.ToString());
59✔
537

538
            for (int i = 0; i < _rawLines.Count; i++)
318✔
539
            {
540
                StringBuilder lineBuilder = new($"{Borders.Vertical} ");
100✔
541
                for (int j = 0; j < _rawLines[i].Count; j++)
800✔
542
                {
543
                    lineBuilder.Append(_rawLines[i][j]?.ToString()?.PadRight(localMax[j]) ?? "");
300!
544
                    if (j != _rawLines[i].Count - 1)
300✔
545
                    {
546
                        lineBuilder.Append($" {Borders.Vertical} ");
200✔
547
                    }
548
                    else
549
                    {
550
                        lineBuilder.Append($" {Borders.Vertical}");
100✔
551
                    }
552
                }
553
                stringList.Add(lineBuilder.ToString());
100✔
554
            }
555

556
            StringBuilder lowerBorderBuilder = new(Borders.BottomLeft.ToString());
59✔
557
            for (int i = 0; i < _rawHeaders.Count; i++)
472✔
558
            {
559
                lowerBorderBuilder.Append(new string(Borders.Horizontal, localMax[i] + 2));
177✔
560
                lowerBorderBuilder.Append(
177✔
561
                    (i != _rawHeaders.Count - 1)
177✔
562
                        ? Borders.Bottom.ToString()
177✔
563
                        : Borders.BottomRight.ToString()
177✔
564
                );
177✔
565
            }
566
            stringList.Add(lowerBorderBuilder.ToString());
59✔
567

568
            _displayArray = stringList.ToArray();
59✔
569
            BuildTitle();
59✔
570
        }
571
    }
59✔
572

573
    private void BuildHeaders()
574
    {
575
        if (_rawHeaders is not null)
2✔
576
        {
577
            var stringList = new List<string>();
2✔
578
            var localMax = new int[_rawHeaders.Count];
2✔
579
            for (int i = 0; i < _rawHeaders.Count; i++)
16✔
580
            {
581
                if (_rawHeaders[i]?.Length > localMax[i])
6!
582
                {
583
                    localMax[i] = _rawHeaders[i]?.Length ?? 0;
6!
584
                }
585
            }
586
            StringBuilder headerBuilder = new($"{Borders.Vertical} ");
2✔
587
            for (int i = 0; i < _rawHeaders.Count; i++)
16✔
588
            {
589
                headerBuilder.Append(_rawHeaders[i]?.PadRight(localMax[i]) ?? "");
6!
590
                if (i != _rawHeaders.Count - 1)
6✔
591
                {
592
                    headerBuilder.Append($" {Borders.Vertical} ");
4✔
593
                }
594
                else
595
                {
596
                    headerBuilder.Append($" {Borders.Vertical}");
2✔
597
                }
598
            }
599
            stringList.Add(headerBuilder.ToString());
2✔
600
            StringBuilder upperBorderBuilder = new(Borders.TopLeft.ToString());
2✔
601
            for (int i = 0; i < _rawHeaders.Count; i++)
16✔
602
            {
603
                upperBorderBuilder.Append(new string(Borders.Horizontal, localMax[i] + 2));
6✔
604
                upperBorderBuilder.Append(
6✔
605
                    (i != _rawHeaders.Count - 1)
6✔
606
                        ? Borders.Top.ToString()
6✔
607
                        : Borders.TopRight.ToString()
6✔
608
                );
6✔
609
            }
610
            stringList.Insert(0, upperBorderBuilder.ToString());
2✔
611
            StringBuilder lowerBorderBuilder = new(Borders.BottomLeft.ToString());
2✔
612
            for (int i = 0; i < _rawHeaders.Count; i++)
16✔
613
            {
614
                lowerBorderBuilder.Append(new string(Borders.Horizontal, localMax[i] + 2));
6✔
615
                lowerBorderBuilder.Append(
6✔
616
                    (i != _rawHeaders.Count - 1)
6✔
617
                        ? Borders.Bottom.ToString()
6✔
618
                        : Borders.BottomRight.ToString()
6✔
619
                );
6✔
620
            }
621
            stringList.Add(lowerBorderBuilder.ToString());
2✔
622
            _displayArray = stringList.ToArray();
2✔
623
            BuildTitle();
2✔
624
        }
625
    }
2✔
626

627
    private void BuildLines()
628
    {
629
        if (_rawLines is not null)
8✔
630
        {
631
            var stringList = new List<string>();
8✔
632
            var localMax = new int[_rawLines[0].Count];
8✔
633
            for (int i = 0; i < _rawLines.Count; i++)
44✔
634
            {
635
                for (int j = 0; j < _rawLines[i].Count; j++)
112✔
636
                {
637
                    if (_rawLines[i][j]?.ToString()?.Length > localMax[j])
42!
638
                    {
639
                        localMax[j] = _rawLines[i][j]?.ToString()?.Length ?? 0;
24!
640
                    }
641
                }
642
            }
643
            for (int i = 0; i < _rawLines.Count; i++)
44✔
644
            {
645
                StringBuilder line = new($"{Borders.Vertical} ");
14✔
646
                for (int j = 0; j < _rawLines[i].Count; j++)
112✔
647
                {
648
                    line.Append(_rawLines[i][j]?.ToString()?.PadRight(localMax[j]) ?? "");
42!
649
                    if (j != _rawLines[i].Count - 1)
42✔
650
                    {
651
                        line.Append($" {Borders.Vertical} ");
28✔
652
                    }
653
                    else
654
                    {
655
                        line.Append($" {Borders.Vertical}");
14✔
656
                    }
657
                }
658
                stringList.Add(line.ToString());
14✔
659
            }
660
            StringBuilder upperBorderBuilder = new(Borders.TopLeft.ToString());
8✔
661
            for (int i = 0; i < _rawLines.Count; i++)
44✔
662
            {
663
                upperBorderBuilder.Append(new string(Borders.Horizontal, localMax[i] + 2));
14✔
664
                upperBorderBuilder.Append(
14✔
665
                    (i != _rawLines.Count - 1)
14✔
666
                        ? Borders.Top.ToString()
14✔
667
                        : Borders.TopRight.ToString()
14✔
668
                );
14✔
669
            }
670
            stringList.Insert(0, upperBorderBuilder.ToString());
8✔
671
            StringBuilder lowerBorderBuilder = new(Borders.BottomLeft.ToString());
8✔
672
            for (int i = 0; i < _rawLines.Count; i++)
44✔
673
            {
674
                lowerBorderBuilder.Append(new string(Borders.Horizontal, localMax[i] + 2));
14✔
675
                lowerBorderBuilder.Append(
14✔
676
                    (i != _rawLines.Count - 1)
14✔
677
                        ? Borders.Bottom.ToString()
14✔
678
                        : Borders.BottomRight.ToString()
14✔
679
                );
14✔
680
            }
681
            stringList.Add(lowerBorderBuilder.ToString());
8✔
682
            _displayArray = stringList.ToArray();
8✔
683
            BuildTitle();
8✔
684
        }
685
    }
8✔
686

687
    private void BuildTitle()
688
    {
689
        if (_title is not null)
69✔
690
        {
691
            var len = _displayArray![0].Length;
11✔
692
            var title = _title.ResizeString(len - 4);
11✔
693
            title = $"{Borders.Vertical} {title} {Borders.Vertical}";
11✔
694
            var upperBorderBuilder = new StringBuilder(Borders.TopLeft.ToString());
11✔
695
            upperBorderBuilder.Append(new string(Borders.Horizontal, len - 2));
11✔
696
            upperBorderBuilder.Append(Borders.TopRight.ToString());
11✔
697
            var display = _displayArray.ToList();
11✔
698
            display[0] = display[0]
11✔
699
                .Remove(0, 1)
11✔
700
                .Insert(0, Borders.Left.ToString())
11✔
701
                .Remove(display[1].Length - 1, 1)
11✔
702
                .Insert(display[1].Length - 1, Borders.Right.ToString());
11✔
703
            display.Insert(0, title);
11✔
704
            display.Insert(0, upperBorderBuilder.ToString());
11✔
705
            _displayArray = display.ToArray();
11✔
706
        }
707
    }
69✔
708
    #endregion
709

710
    /// <summary>
711
    /// Defines the actions to perform when the element is called to be rendered on the console.
712
    /// </summary>
713
    [Visual]
714
    protected override void RenderElementActions()
715
    {
716
        BuildTable();
717
        string[] array = new string[_displayArray!.Length];
718
        for (int j = 0; j < _displayArray.Length; j++)
719
        {
720
            array[j] = _displayArray[j];
721
            Core.WritePositionedString(array[j], _placement, false, Line + j);
722
        }
723
    }
724
    #endregion
725
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc