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

MorganKryze / ConsoleAppVisuals / 8138371021

04 Mar 2024 09:56AM UTC coverage: 85.231% (-0.02%) from 85.254%
8138371021

push

github

MorganKryze
📖 update line property metadata

868 of 1082 branches covered (80.22%)

Branch coverage included in aggregate %.

1700 of 1931 relevant lines covered (88.04%)

272.6 hits per line

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

92.12
/src/ConsoleAppVisuals/elements/static_elements/TableView.cs
1
/*
2
    GNU GPL License 2024 MorganKryze(Yann Vidamment)
3
    For full license information, please visit: https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/LICENSE
4
*/
5
namespace ConsoleAppVisuals.Elements;
6

7
/// <summary>
8
/// The <see cref="TableView{T}"/> class that contains the methods to create a table and display it.
9
/// </summary>
10
/// <remarks>
11
/// For more information, refer to the following resources:
12
/// <list type="bullet">
13
/// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
14
/// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
15
/// </list>
16
/// </remarks>
17
public class TableView<T> : Element
18
{
19
    #region Fields: title, headers, lines, display array, rounded corners
20
    private string? _title;
21
    private List<string>? _rawHeaders;
22
    private List<List<T>>? _rawLines;
23
    private string[]? _displayArray;
24
    private bool _roundedCorners;
25
    private readonly Placement _placement;
26
    #endregion
27

28
    #region Properties: get headers, get lines
29
    /// <summary>
30
    /// This property returns the headers of the table.
31
    /// </summary>
32
    public List<string>? GetRawHeaders => _rawHeaders;
18✔
33

34
    /// <summary>
35
    /// This property returns the lines of the table.
36
    /// </summary>
37
    public List<List<T>>? GetRawLines => _rawLines;
15✔
38

39
    /// <summary>
40
    /// This property returns the title of the table.
41
    /// </summary>
42
    public override Placement Placement => _placement;
24✔
43

44
    /// <summary>
45
    /// This property returns the height of the table.
46
    /// </summary>
47
    public override int Height => _displayArray?.Length ?? 0;
27✔
48

49
    /// <summary>
50
    /// This property returns the width of the table.
51
    /// </summary>
52
    public override int Width => _displayArray?.Max(x => x.Length) ?? 0;
3!
53

54
    /// <summary>
55
    /// This property returns the maximum number of this element that can be displayed on the console.
56
    /// </summary>
57
    public override int MaxNumberOfThisElement { get; } = 9;
174✔
58

59
    #endregion
60

61
    #region Constructor
62
    /// <summary>
63
    /// The <see cref="TableView{T}"/> natural constructor.
64
    /// </summary>
65
    /// <param name="title">The title of the table.</param>
66
    /// <param name="headers">The headers of the table.</param>
67
    /// <param name="lines">The lines of the table.</param>
68
    /// <param name="roundedCorners">The rounded corners of the table.</param>
69
    /// <param name="placement">The placement of the table.</param>
70
    /// <exception cref="ArgumentException">Is thrown when the number of columns in the table is not consistent with itself or with the headers.</exception>
71
    /// <exception cref="NullReferenceException">Is thrown when no body lines were provided.</exception>
72
    /// <remarks>
73
    /// For more information, refer to the following resources:
74
    /// <list type="bullet">
75
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
76
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
77
    /// </list>
78
    /// </remarks>
79
    public TableView(
168✔
80
        string? title = null,
168✔
81
        List<string>? headers = null,
168✔
82
        List<List<T>>? lines = null,
168✔
83
        bool roundedCorners = false,
168✔
84
        Placement placement = Placement.TopCenter
168✔
85
    )
168✔
86
    {
87
        _title = title;
168✔
88
        _rawHeaders = headers;
168✔
89
        _rawLines = lines;
168✔
90
        _roundedCorners = roundedCorners;
168✔
91
        _placement = placement;
168✔
92
        if (CompatibilityCheck())
168✔
93
        {
94
            BuildTable();
144✔
95
        }
96
    }
162✔
97
    #endregion
98

99
    #region Check Methods
100
    private bool CompatibilityCheck()
101
    {
102
        if (_rawHeaders is null)
177✔
103
        {
104
            return CheckRawLines();
21✔
105
        }
106
        else if (_rawLines is null)
156✔
107
        {
108
            return true;
3✔
109
        }
110
        else
111
        {
112
            return CheckRawHeadersAndLines();
153✔
113
        }
114
    }
115

116
    private bool CheckRawLines()
117
    {
118
        if (_rawLines is null || _rawLines.Count == 0)
21✔
119
        {
120
            return false;
6✔
121
        }
122

123
        for (int i = 0; i < _rawLines.Count; i++)
84✔
124
        {
125
            if (_rawLines[i].Count != _rawLines[0].Count)
30✔
126
            {
127
                throw new ArgumentException(
3✔
128
                    "The number of columns in the table is not consistent."
3✔
129
                );
3✔
130
            }
131
        }
132
        return true;
12✔
133
    }
134

135
    private bool CheckRawHeadersAndLines()
136
    {
137
        if (_rawLines is null || _rawLines.Count == 0)
153✔
138
        {
139
            return false;
12✔
140
        }
141

142
        if (_rawLines.Count > 0)
141✔
143
        {
144
            for (int i = 0; i < _rawLines.Count; i++)
798✔
145
            {
146
                if (_rawLines[i].Count != _rawHeaders?.Count)
264!
147
                {
148
                    throw new ArgumentException(
6✔
149
                        "The number of columns in the table is not consistent(Headers or Lines)."
6✔
150
                    );
6✔
151
                }
152
            }
153
        }
154
        return true;
135✔
155
    }
156
    #endregion
157

158
    #region Build Methods
159
    private void BuildTable()
160
    {
161
        if (_rawHeaders is null)
204✔
162
        {
163
            if (_rawLines is not null)
30✔
164
            {
165
                BuildLines();
24✔
166
            }
167
        }
168
        else
169
        {
170
            if (_rawLines is null)
174✔
171
            {
172
                BuildHeaders();
6✔
173
            }
174
            else
175
            {
176
                BuildHeadersAndLines();
168✔
177
            }
178
        }
179
    }
174✔
180

181
    private void BuildHeadersAndLines()
182
    {
183
        if (_rawHeaders is not null && _rawLines is not null)
168✔
184
        {
185
            var stringList = new List<string>();
168✔
186
            var localMax = new int[_rawHeaders.Count];
168✔
187
            for (int i = 0; i < _rawHeaders.Count; i++)
1,344✔
188
            {
189
                if (_rawHeaders[i]?.Length > localMax[i])
504!
190
                {
191
                    localMax[i] = _rawHeaders[i]?.Length ?? 0;
504!
192
                }
193
            }
194

195
            for (int i = 0; i < _rawLines.Count; i++)
900✔
196
            {
197
                for (int j = 0; j < _rawLines[i].Count; j++)
2,256✔
198
                {
199
                    if (_rawLines[i][j]?.ToString()?.Length > localMax[j])
846!
200
                    {
201
                        localMax[j] = _rawLines[i][j]?.ToString()?.Length ?? 0;
×
202
                    }
203
                }
204
            }
205

206
            StringBuilder headerBuilder = new("│ ");
168✔
207
            for (int i = 0; i < _rawHeaders.Count; i++)
1,344✔
208
            {
209
                headerBuilder.Append(_rawHeaders[i]?.PadRight(localMax[i]) ?? "");
504!
210
                if (i != _rawHeaders.Count - 1)
504✔
211
                {
212
                    headerBuilder.Append(" │ ");
336✔
213
                }
214
                else
215
                {
216
                    headerBuilder.Append(" │");
168✔
217
                }
218
            }
219
            stringList.Add(headerBuilder.ToString());
168✔
220

221
            StringBuilder upperBorderBuilder = new(GetCorners[0].ToString());
168✔
222
            for (int i = 0; i < _rawHeaders.Count; i++)
1,344✔
223
            {
224
                upperBorderBuilder.Append(new string('─', localMax[i] + 2));
504✔
225
                upperBorderBuilder.Append(
504✔
226
                    (i != _rawHeaders.Count - 1) ? "┬" : GetCorners[1].ToString()
504✔
227
                );
504✔
228
            }
229
            stringList.Insert(0, upperBorderBuilder.ToString());
168✔
230

231
            StringBuilder intermediateBorderBuilder = new("├");
168✔
232
            for (int i = 0; i < _rawHeaders.Count; i++)
1,344✔
233
            {
234
                intermediateBorderBuilder.Append(new string('─', localMax[i] + 2));
504✔
235
                intermediateBorderBuilder.Append((i != _rawHeaders.Count - 1) ? "┼" : "┤");
504✔
236
            }
237
            stringList.Add(intermediateBorderBuilder.ToString());
168✔
238

239
            for (int i = 0; i < _rawLines.Count; i++)
900✔
240
            {
241
                StringBuilder lineBuilder = new("│ ");
282✔
242
                for (int j = 0; j < _rawLines[i].Count; j++)
2,256✔
243
                {
244
                    lineBuilder.Append(_rawLines[i][j]?.ToString()?.PadRight(localMax[j]) ?? "");
846!
245
                    if (j != _rawLines[i].Count - 1)
846✔
246
                    {
247
                        lineBuilder.Append(" │ ");
564✔
248
                    }
249
                    else
250
                    {
251
                        lineBuilder.Append(" │");
282✔
252
                    }
253
                }
254
                stringList.Add(lineBuilder.ToString());
282✔
255
            }
256

257
            StringBuilder lowerBorderBuilder = new(GetCorners[2].ToString());
168✔
258
            for (int i = 0; i < _rawHeaders.Count; i++)
1,344✔
259
            {
260
                lowerBorderBuilder.Append(new string('─', localMax[i] + 2));
504✔
261
                lowerBorderBuilder.Append(
504✔
262
                    (i != _rawHeaders.Count - 1) ? "┴" : GetCorners[3].ToString()
504✔
263
                );
504✔
264
            }
265
            stringList.Add(lowerBorderBuilder.ToString());
168✔
266

267
            _displayArray = stringList.ToArray();
168✔
268
            BuildTitle();
168✔
269
        }
270
    }
168✔
271

272
    private void BuildHeaders()
273
    {
274
        if (_rawHeaders is not null)
6✔
275
        {
276
            var stringList = new List<string>();
6✔
277
            var localMax = new int[_rawHeaders.Count];
6✔
278
            for (int i = 0; i < _rawHeaders.Count; i++)
48✔
279
            {
280
                if (_rawHeaders[i]?.Length > localMax[i])
18!
281
                {
282
                    localMax[i] = _rawHeaders[i]?.Length ?? 0;
18!
283
                }
284
            }
285
            StringBuilder headerBuilder = new("│ ");
6✔
286
            for (int i = 0; i < _rawHeaders.Count; i++)
48✔
287
            {
288
                headerBuilder.Append(_rawHeaders[i]?.PadRight(localMax[i]) ?? "");
18!
289
                if (i != _rawHeaders.Count - 1)
18✔
290
                {
291
                    headerBuilder.Append(" │ ");
12✔
292
                }
293
                else
294
                {
295
                    headerBuilder.Append(" │");
6✔
296
                }
297
            }
298
            stringList.Add(headerBuilder.ToString());
6✔
299
            StringBuilder upperBorderBuilder = new(GetCorners[0].ToString());
6✔
300
            for (int i = 0; i < _rawHeaders.Count; i++)
48✔
301
            {
302
                upperBorderBuilder.Append(new string('─', localMax[i] + 2));
18✔
303
                upperBorderBuilder.Append(
18✔
304
                    (i != _rawHeaders.Count - 1) ? "┬" : GetCorners[1].ToString()
18✔
305
                );
18✔
306
            }
307
            stringList.Insert(0, upperBorderBuilder.ToString());
6✔
308
            StringBuilder lowerBorderBuilder = new(GetCorners[2].ToString());
6✔
309
            for (int i = 0; i < _rawHeaders.Count; i++)
48✔
310
            {
311
                lowerBorderBuilder.Append(new string('─', localMax[i] + 2));
18✔
312
                lowerBorderBuilder.Append(
18✔
313
                    (i != _rawHeaders.Count - 1) ? "┴" : GetCorners[3].ToString()
18✔
314
                );
18✔
315
            }
316
            stringList.Add(lowerBorderBuilder.ToString());
6✔
317
            _displayArray = stringList.ToArray();
6✔
318
            BuildTitle();
6✔
319
        }
320
    }
6✔
321

322
    private void BuildLines()
323
    {
324
        if (_rawLines is not null)
24✔
325
        {
326
            var stringList = new List<string>();
24✔
327
            var localMax = new int[_rawLines[0].Count];
24✔
328
            for (int i = 0; i < _rawLines.Count; i++)
132✔
329
            {
330
                for (int j = 0; j < _rawLines[i].Count; j++)
336✔
331
                {
332
                    if (_rawLines[i][j]?.ToString()?.Length > localMax[j])
126!
333
                    {
334
                        localMax[j] = _rawLines[i][j]?.ToString()?.Length ?? 0;
72!
335
                    }
336
                }
337
            }
338
            for (int i = 0; i < _rawLines.Count; i++)
132✔
339
            {
340
                StringBuilder line = new("│ ");
42✔
341
                for (int j = 0; j < _rawLines[i].Count; j++)
336✔
342
                {
343
                    line.Append(_rawLines[i][j]?.ToString()?.PadRight(localMax[j]) ?? "");
126!
344
                    if (j != _rawLines[i].Count - 1)
126✔
345
                    {
346
                        line.Append(" │ ");
84✔
347
                    }
348
                    else
349
                    {
350
                        line.Append(" │");
42✔
351
                    }
352
                }
353
                stringList.Add(line.ToString());
42✔
354
            }
355
            StringBuilder upperBorderBuilder = new(GetCorners[0].ToString());
24✔
356
            for (int i = 0; i < _rawLines.Count; i++)
132✔
357
            {
358
                upperBorderBuilder.Append(new string('─', localMax[i] + 2));
42✔
359
                upperBorderBuilder.Append(
42✔
360
                    (i != _rawLines.Count - 1) ? "┬" : GetCorners[1].ToString()
42✔
361
                );
42✔
362
            }
363
            stringList.Insert(0, upperBorderBuilder.ToString());
24✔
364
            StringBuilder lowerBorderBuilder = new(GetCorners[2].ToString());
24✔
365
            for (int i = 0; i < _rawLines.Count; i++)
132✔
366
            {
367
                lowerBorderBuilder.Append(new string('─', localMax[i] + 2));
42✔
368
                lowerBorderBuilder.Append(
42✔
369
                    (i != _rawLines.Count - 1) ? "┴" : GetCorners[3].ToString()
42✔
370
                );
42✔
371
            }
372
            stringList.Add(lowerBorderBuilder.ToString());
24✔
373
            _displayArray = stringList.ToArray();
24✔
374
            BuildTitle();
24✔
375
        }
376
    }
24✔
377

378
    private void BuildTitle()
379
    {
380
        if (_title is not null)
198✔
381
        {
382
            var len = _displayArray![0].Length;
30✔
383
            var title = _title.ResizeString(len - 4);
30✔
384
            title = $"│ {title} │";
30✔
385
            var upperBorderBuilder = new StringBuilder(GetCorners[0].ToString());
30✔
386
            upperBorderBuilder.Append(new string('─', len - 2));
30✔
387
            upperBorderBuilder.Append(GetCorners[1].ToString());
30✔
388
            var display = _displayArray.ToList();
30✔
389
            display[0] = display[0]
30✔
390
                .Remove(0, 1)
30✔
391
                .Insert(0, "├")
30✔
392
                .Remove(display[1].Length - 1, 1)
30✔
393
                .Insert(display[1].Length - 1, "┤");
30✔
394
            display.Insert(0, title);
30✔
395
            display.Insert(0, upperBorderBuilder.ToString());
30✔
396
            _displayArray = display.ToArray();
30✔
397
        }
398
    }
198✔
399
    #endregion
400

401
    #region Properties: GetCorners, Count
402
    private string GetCorners => _roundedCorners ? "╭╮╰╯" : "┌┐└┘";
852✔
403

404
    /// <summary>
405
    /// This property returns the number of lines in the table.
406
    /// </summary>
407
    public int Count => _rawLines?.Count ?? 0;
24✔
408

409
    #endregion
410

411
    #region Methods: Get, Add, Update, Remove, Clear
412
    /// <summary>
413
    /// Toggles the rounded corners of the table.
414
    /// </summary>
415
    /// <remarks>
416
    /// For more information, refer to the following resources:
417
    /// <list type="bullet">
418
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
419
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
420
    /// </list>
421
    /// </remarks>
422
    public void SetRoundedCorners(bool rounded = true)
423
    {
424
        _roundedCorners = rounded;
6✔
425
        BuildTable();
6✔
426
    }
6✔
427

428
    /// <summary>
429
    /// This method adds a title to the table.
430
    /// </summary>
431
    /// <param name="title">The title to add.</param>
432
    /// <remarks>
433
    /// For more information, refer to the following resources:
434
    /// <list type="bullet">
435
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
436
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
437
    /// </list>
438
    /// </remarks>
439
    public void AddTitle(string title)
440
    {
441
        _title = title;
6✔
442
        BuildTable();
6✔
443
    }
6✔
444

445
    /// <summary>
446
    /// This method updates the title of the table.
447
    /// </summary>
448
    /// <param name="title">The title to update.</param>
449
    /// <remarks>
450
    /// For more information, refer to the following resources:
451
    /// <list type="bullet">
452
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
453
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
454
    /// </list>
455
    /// </remarks>
456
    public void UpdateTitle(string title)
457
    {
458
        AddTitle(title);
3✔
459
    }
3✔
460

461
    /// <summary>
462
    /// This method clears the title of the table.
463
    /// </summary>
464
    /// <remarks>
465
    /// For more information, refer to the following resources:
466
    /// <list type="bullet">
467
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
468
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
469
    /// </list>
470
    /// </remarks>
471
    public void ClearTitle()
472
    {
473
        _title = null;
9✔
474
        BuildTable();
9✔
475
    }
9✔
476

477
    /// <summary>
478
    /// This method adds headers to the table.
479
    /// </summary>
480
    /// <param name="headers">The headers to add.</param>
481
    /// <remarks>
482
    /// For more information, refer to the following resources:
483
    /// <list type="bullet">
484
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
485
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
486
    /// </list>
487
    /// </remarks>
488
    public void AddHeaders(List<string> headers)
489
    {
490
        _rawHeaders = headers;
9✔
491
        if (CompatibilityCheck())
9✔
492
        {
493
            BuildTable();
6✔
494
        }
495
    }
6✔
496

497
    /// <summary>
498
    /// This method updates the headers of the table.
499
    /// </summary>
500
    /// <param name="headers">The headers to update.</param>
501
    /// <remarks>
502
    /// For more information, refer to the following resources:
503
    /// <list type="bullet">
504
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
505
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
506
    /// </list>
507
    /// </remarks>
508
    public void UpdateHeaders(List<string> headers)
509
    {
510
        AddHeaders(headers);
3✔
511
    }
3✔
512

513
    /// <summary>
514
    /// This method clears the headers of the table.
515
    /// </summary>
516
    /// <remarks>
517
    /// For more information, refer to the following resources:
518
    /// <list type="bullet">
519
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
520
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
521
    /// </list>
522
    /// </remarks>
523
    public void ClearHeaders()
524
    {
525
        _rawHeaders = null;
12✔
526
        BuildTable();
12✔
527
    }
12✔
528

529
    /// <summary>
530
    /// This method adds a line to the table.
531
    /// </summary>
532
    /// <param name="line">The line to add.</param>
533
    /// <exception cref="ArgumentException">Is thrown when the number of columns in the table is not consistent with itself or with the headers.</exception>
534
    /// <remarks>
535
    /// For more information, refer to the following resources:
536
    /// <list type="bullet">
537
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
538
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
539
    /// </list>
540
    /// </remarks>
541
    public void AddLine(List<T> line)
542
    {
543
        if (_rawLines?.Count > 0 && line.Count != _rawLines[0].Count)
12✔
544
        {
545
            throw new ArgumentException(
3✔
546
                "The number of columns in the table is not consistent with other lines."
3✔
547
            );
3✔
548
        }
549
        if (_rawHeaders is not null && line.Count != _rawHeaders.Count)
9✔
550
        {
551
            throw new ArgumentException(
3✔
552
                "The number of columns in the table is not consistent with the headers."
3✔
553
            );
3✔
554
        }
555
        _rawLines ??= new List<List<T>>();
6!
556
        _rawLines.Add(line);
6✔
557
        BuildTable();
6✔
558
    }
6✔
559

560
    /// <summary>
561
    /// This method updates a line in the table.
562
    /// </summary>
563
    /// <param name="index">The index of the line to update.</param>
564
    /// <param name="line">The new line.</param>
565
    /// <exception cref="ArgumentOutOfRangeException">Is thrown when the index is out of range.</exception>
566
    /// <exception cref="ArgumentException">Is thrown when the number of columns in the table is not consistent with itself or with the headers.</exception>
567
    /// <remarks>
568
    /// For more information, refer to the following resources:
569
    /// <list type="bullet">
570
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
571
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
572
    /// </list>
573
    /// </remarks>
574
    public void UpdateLine(int index, List<T> line)
575
    {
576
        if (_rawLines?.Count > 0)
9!
577
        {
578
            if (index < 0 || index >= _rawLines.Count)
9✔
579
            {
580
                throw new ArgumentOutOfRangeException(nameof(index), "The index is out of range.");
3✔
581
            }
582

583
            if (line.Count != _rawHeaders?.Count)
6!
584
            {
585
                throw new ArgumentException(
3✔
586
                    "The number of columns in the table is not consistent."
3✔
587
                );
3✔
588
            }
589
        }
590
        _rawLines![index] = line;
3✔
591
        BuildTable();
3✔
592
    }
3✔
593

594
    /// <summary>
595
    /// This method removes a line from the table.
596
    /// </summary>
597
    /// <param name="index">The index of the line to remove.</param>
598
    /// <exception cref="ArgumentOutOfRangeException">Is thrown when the index is out of range.</exception>
599
    /// <remarks>
600
    /// For more information, refer to the following resources:
601
    /// <list type="bullet">
602
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
603
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
604
    /// </list>
605
    /// </remarks>
606
    public void RemoveLine(int index)
607
    {
608
        if (_rawLines?.Count > 0 && (index < 0 || index >= _rawLines.Count))
6!
609
        {
610
            throw new ArgumentOutOfRangeException(nameof(index), "The index is out of range.");
3✔
611
        }
612

613
        _rawLines?.RemoveAt(index);
3!
614
        BuildTable();
3✔
615
    }
3✔
616

617
    /// <summary>
618
    /// This method clears the lines of the table.
619
    /// </summary>
620
    /// <remarks>
621
    /// For more information, refer to the following resources:
622
    /// <list type="bullet">
623
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
624
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
625
    /// </list>
626
    /// </remarks>
627
    public void ClearLines()
628
    {
629
        _rawLines = null;
9✔
630
        BuildTable();
9✔
631
    }
9✔
632

633
    /// <summary>
634
    /// This property returns the specified line in the table.
635
    /// </summary>
636
    /// <param name="index">The index of the line to return.</param>
637
    /// <returns>The line at the specified index.</returns>
638
    /// <exception cref="ArgumentOutOfRangeException">Is thrown when the index is out of range.</exception>
639
    /// <remarks>
640
    /// For more information, refer to the following resources:
641
    /// <list type="bullet">
642
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
643
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
644
    /// </list>
645
    /// </remarks>
646
    public List<T> GetLine(int index)
647
    {
648
        if (index < 0 || index >= _rawLines?.Count)
9!
649
        {
650
            throw new ArgumentOutOfRangeException(nameof(index), "The index is out of range.");
3✔
651
        }
652
        return _rawLines![index];
6✔
653
    }
654

655
    /// <summary>
656
    /// This method is used to get all the elements from a column given its index.
657
    /// </summary>
658
    /// <param name="index">The index of the column.</param>
659
    /// <returns>The elements of the column.</returns>
660
    /// <exception cref="ArgumentOutOfRangeException">Is thrown when the index is out of range.</exception>
661
    /// <remarks>
662
    /// For more information, refer to the following resources:
663
    /// <list type="bullet">
664
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
665
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
666
    /// </list>
667
    /// </remarks>
668
    public List<T>? GetColumnData(int index)
669
    {
670
        if (_rawLines is null)
12✔
671
        {
672
            return null;
3✔
673
        }
674

675
        if (index < 0 || index >= _rawLines[0].Count)
9✔
676
        {
677
            throw new ArgumentOutOfRangeException(nameof(index), "Invalid column index.");
3✔
678
        }
679

680
        List<T>? list = new();
6✔
681
        for (int i = 0; i < _rawLines.Count; i++)
36✔
682
        {
683
            list.Add(_rawLines[i][index]);
12✔
684
        }
685
        return list;
6✔
686
    }
687

688
    /// <summary>
689
    /// This method is used to get all the elements from a column given its header.
690
    /// </summary>
691
    /// <param name="header">The header of the column.</param>
692
    /// <returns>The elements of the column.</returns>
693
    /// <exception cref="InvalidOperationException">Is thrown when the table is empty.</exception>
694
    /// <exception cref="ArgumentOutOfRangeException">Is thrown when the header is invalid.</exception>
695
    /// <remarks>
696
    /// For more information, refer to the following resources:
697
    /// <list type="bullet">
698
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
699
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
700
    /// </list>
701
    /// </remarks>
702
    public List<T>? GetColumnData(string header)
703
    {
704
        if (_rawHeaders is null)
12✔
705
        {
706
            throw new InvalidOperationException("The headers are null.");
3✔
707
        }
708
        else if (_rawLines is null)
9✔
709
        {
710
            return null;
3✔
711
        }
712
        if (!_rawHeaders.Contains(header))
6✔
713
        {
714
            throw new ArgumentOutOfRangeException(nameof(header), "Invalid column header.");
3✔
715
        }
716

717
        return GetColumnData(_rawHeaders.IndexOf(header));
3✔
718
    }
719

720
    /// <summary>
721
    /// This method clears the table.
722
    /// </summary>
723
    /// <remarks>
724
    /// For more information, refer to the following resources:
725
    /// <list type="bullet">
726
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
727
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
728
    /// </list>
729
    /// </remarks>
730
    public void Reset()
731
    {
732
        _title = null;
3✔
733
        _rawHeaders?.Clear();
3!
734
        _rawLines?.Clear();
3!
735
        _displayArray = null;
3✔
736
    }
3✔
737
    #endregion
738

739
    #region Display Methods
740
    /// <summary>
741
    /// This method displays the table without interaction.
742
    /// </summary>
743
    protected override void RenderElementActions()
744
    {
745
        string[] array = new string[_displayArray!.Length];
3✔
746
        for (int j = 0; j < _displayArray.Length; j++)
54✔
747
        {
748
            array[j] = _displayArray[j];
24✔
749
            Core.WritePositionedString(array[j], _placement.ToTextAlignment(), false, Line + j);
24✔
750
        }
751
    }
3✔
752
    #endregion
753
}
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