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

MorganKryze / ConsoleAppVisuals / 7465772104

09 Jan 2024 07:11PM UTC coverage: 12.896% (+0.8%) from 12.086%
7465772104

push

github

MorganKryze
✅ (visual) add attribute to ignore

216 of 1544 branches covered (0.0%)

Branch coverage included in aggregate %.

4 of 15 new or added lines in 5 files covered. (26.67%)

4 existing lines in 2 files now uncovered.

321 of 2620 relevant lines covered (12.25%)

22.11 hits per line

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

0.0
/src/ConsoleAppVisuals/Window.cs
1
/*
2
    MIT License 2023 MorganKryze
3
    For full license information, please visit: https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/LICENSE
4
*/
5
namespace ConsoleAppVisuals;
6

7
/// <summary>
8
/// The major class of the library. The window is used to collect the elements of the console and draw them.
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/Program.cs">Example Project</a></description></item>
15
/// </list>
16
/// </remarks>
17
public static class Window
18
{
19
    #region Setup
20
    /// <summary>
21
    /// This method sets up the window without the need to call it.
22
    /// </summary>
23
    static Window()
24
    {
25
        Console.Clear();
×
26
        Console.CursorVisible = false;
×
27
    }
×
28
    #endregion
29

30
    #region Fields: s_elements
31
    private static readonly List<Element> s_elements = new();
×
32
    #endregion
33

34
    #region Constants: DefaultVisibility
35
    /// <summary>
36
    /// The default visibility of the elements when they are added to the window.
37
    /// </summary>
38
    public const bool DEFAULT_VISIBILITY = false;
39
    #endregion
40

41
    #region Properties: NextId, NumberOfElements
42
    /// <summary>
43
    /// Gives the next id number each time a new element is added to the window.
44
    /// </summary>
45
    public static int NextId => s_elements.Count;
×
46

47
    /// <summary>
48
    /// Gives the number of elements in the window.
49
    /// </summary>
50
    public static int CountElements => s_elements.Count;
×
51
    #endregion
52

53
    #region Basic Methods: Get, Add, Insert, Remove, RemoveAll
54
    /// <summary>
55
    /// This method returns the first element of the given type.
56
    /// </summary>
57
    /// <typeparam name="T">The type of the element.</typeparam>
58
    /// <returns>The element with the given type if it exists, null otherwise.</returns>
59
    /// <remarks>
60
    /// For more information, refer to the following resources:
61
    /// <list type="bullet">
62
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
63
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
64
    /// </list>
65
    /// </remarks>
66
    public static T? GetElement<T>()
67
        where T : Element
68
    {
69
        return s_elements.Find(element => element.GetType() == typeof(T)) as T;
×
70
    }
71

72
    /// <summary>
73
    /// This method returns the element with the given id.
74
    /// </summary>
75
    /// <param name="id">The id of the element.</param>
76
    /// <returns>The element with the given id if it exists, null otherwise.</returns>
77
    /// <exception cref="ArgumentOutOfRangeException">Thrown when the id is out of range.</exception>
78
    /// <remarks>
79
    /// For more information, refer to the following resources:
80
    /// <list type="bullet">
81
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
82
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
83
    /// </list>
84
    /// </remarks>
85
    public static T? GetElement<T>(int id)
86
        where T : Element
87
    {
88
        if (id < 0 || id >= s_elements.Count)
×
89
        {
90
            throw new ArgumentOutOfRangeException(nameof(id), "Invalid element ID.");
×
91
        }
92
        return (T)s_elements[id];
×
93
    }
94

95
    /// <summary>
96
    /// This method returns the first visible element with the given type.
97
    /// </summary>
98
    /// <typeparam name="T">The type of the element.</typeparam>
99
    /// <returns>The visible element with the given type if it exists, null otherwise.</returns>
100
    /// <remarks>
101
    /// For more information, refer to the following resources:
102
    /// <list type="bullet">
103
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
104
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
105
    /// </list>
106
    /// </remarks>
107
    public static T? GetVisibleElement<T>()
108
        where T : Element
109
    {
110
        return s_elements.Find(element => element.GetType() == typeof(T) && element.Visibility)
×
111
            as T;
×
112
    }
113

114
    /// <summary>
115
    /// This method adds an element to the window.
116
    /// </summary>
117
    /// <param name="element">The element to be added.</param>
118
    /// <remarks>
119
    /// For more information, refer to the following resources:
120
    /// <list type="bullet">
121
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
122
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
123
    /// </list>
124
    /// </remarks>
125
    public static void AddElement(Element element)
126
    {
127
        s_elements.Add(element);
×
128
        if (!element.IsInteractive && AllowVisibilityToggle(element.Id))
×
129
        {
130
            element.ToggleVisibility();
×
131
        }
132
    }
×
133

134
    /// <summary>
135
    /// This method inserts an element to the window at the given id.
136
    /// </summary>
137
    /// <param name="element">The element to be inserted.</param>
138
    /// <param name="id">The id of the element.</param>
139
    /// <exception cref="ArgumentOutOfRangeException">Thrown when the id is out of range.</exception>
140
    /// <remarks>
141
    /// For more information, refer to the following resources:
142
    /// <list type="bullet">
143
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
144
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
145
    /// </list>
146
    /// </remarks>
147
    public static void InsertElement(Element element, int id)
148
    {
149
        if (id < 0 || id > s_elements.Count)
×
150
        {
151
            throw new ArgumentOutOfRangeException(nameof(id), "Invalid element ID.");
×
152
        }
153
        s_elements.Insert(id, element);
×
154
    }
×
155

156
    /// <summary>
157
    /// This method removes the element with the given id.
158
    /// </summary>
159
    /// <param name="id">The id of the element.</param>
160
    /// <exception cref="ArgumentOutOfRangeException">Thrown when the id is out of range.</exception>
161
    /// <remarks>
162
    /// For more information, refer to the following resources:
163
    /// <list type="bullet">
164
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
165
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
166
    /// </list>
167
    /// </remarks>
168
    public static void RemoveElement(int id)
169
    {
170
        if (id < 0 || id >= s_elements.Count)
×
171
        {
172
            throw new ArgumentOutOfRangeException(nameof(id), "Invalid element ID.");
×
173
        }
174
        s_elements.RemoveAt(id);
×
175
        UpdateIDs();
×
176
    }
×
177

178
    /// <summary>
179
    /// This method removes the given element.
180
    /// </summary>
181
    /// <param name="element">The element to be removed.</param>
182
    /// <remarks>
183
    /// For more information, refer to the following resources:
184
    /// <list type="bullet">
185
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
186
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
187
    /// </list>
188
    /// </remarks>
189
    public static void RemoveElement(Element element)
190
    {
191
        if (element is null || s_elements.Contains(element))
×
192
        {
193
            throw new ArgumentOutOfRangeException(
×
194
                nameof(element),
×
195
                "Invalid element. Not found in the window."
×
196
            );
×
197
        }
198
        s_elements.Remove(element);
×
199
        UpdateIDs();
×
200
    }
×
201

202
    /// <summary>
203
    /// This method removes the first element with the given type.
204
    /// </summary>
205
    /// <typeparam name="T">The type of the element.</typeparam>
206
    /// <exception cref="ElementNotFoundException">Thrown when the element is invalid.</exception>
207
    /// <remarks>
208
    /// For more information, refer to the following resources:
209
    /// <list type="bullet">
210
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
211
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
212
    /// </list>
213
    /// </remarks>
214
    public static void RemoveElement<T>()
215
        where T : Element
216
    {
217
        var element =
×
218
            GetElement<T>()
×
219
            ?? throw new ElementNotFoundException("Invalid element. Not found in the window.");
×
220
        s_elements.Remove(element);
×
221
        UpdateIDs();
×
222
    }
×
223

224
    /// <summary>
225
    /// This method removes the first element with the given type created by the library itself.
226
    /// </summary>
227
    /// <typeparam name="T">The type of the element.</typeparam>
228
    /// <exception cref="ElementNotFoundException">Thrown when the element is invalid.</exception>
229
    /// <remarks>
230
    /// For more information, refer to the following resources:
231
    /// <list type="bullet">
232
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
233
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
234
    /// </list>
235
    /// </remarks>
236
    public static void RemoveLibraryElement<T>()
237
        where T : Element
238
    {
239
        var element =
×
240
            GetElement<T>()
×
241
            ?? throw new ElementNotFoundException("Invalid element. Not found in the window.");
×
242
        if (element.ElementSource == Source.Library)
×
243
        {
244
            s_elements.Remove(element);
×
245
            UpdateIDs();
×
246
        }
247
    }
×
248

249
    /// <summary>
250
    /// This method removes all elements from the window.
251
    /// </summary>
252
    /// <remarks>
253
    /// For more information, refer to the following resources:
254
    /// <list type="bullet">
255
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
256
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
257
    /// </list>
258
    /// </remarks>
259
    public static void RemoveAllElements()
260
    {
261
        s_elements.Clear();
×
262
    }
×
263
    #endregion
264

265
    #region Manipulation Methods: ActivateElement, ActivateAllElements, DeactivateElement, DeactivateAllElements
266
    /// <summary>
267
    /// This method attempts to activate the visibility of the element with the given id.
268
    /// </summary>
269
    /// <param name="id">The id of the element.</param>
270
    /// <param name="render">If true, the element will be rendered.</param>
271
    /// <exception cref="ArgumentOutOfRangeException">Thrown when the id is out of range.</exception>
272
    /// <remarks>
273
    /// For more information, refer to the following resources:
274
    /// <list type="bullet">
275
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
276
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
277
    /// </list>
278
    /// </remarks>
279
    public static void ActivateElement(int id, bool render = true)
280
    {
281
        if (id < 0 || id >= s_elements.Count)
×
282
        {
283
            throw new ArgumentOutOfRangeException(nameof(id), "Invalid element ID.");
×
284
        }
285
        if (!s_elements[id].Visibility)
×
286
        {
287
            s_elements[id].ToggleVisibility();
×
288
        }
289
        if (render)
×
290
        {
291
            s_elements[id].RenderElement();
×
292
        }
293
    }
×
294

295
    /// <summary>
296
    /// This method attempts to activate the visibility of the first element of the given type.
297
    /// </summary>
298
    /// <param name="render">If true, the element will be rendered.</param>
299
    /// <typeparam name="T">The type of the element.</typeparam>
300
    /// <exception cref="ElementNotFoundException">Thrown when the element is invalid.</exception>
301
    /// <remarks>
302
    /// For more information, refer to the following resources:
303
    /// <list type="bullet">
304
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
305
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
306
    /// </list>
307
    /// </remarks>
308
    public static void ActivateElement<T>(bool render = true)
309
        where T : Element
310
    {
311
        var element =
×
312
            GetElement<T>()
×
313
            ?? throw new ElementNotFoundException("Invalid element. Not found in the window.");
×
314
        if (!element.Visibility)
×
315
        {
316
            element.ToggleVisibility();
×
317
        }
318
        if (render)
×
319
        {
320
            element.RenderElement();
×
321
        }
322
    }
×
323

324
    /// <summary>
325
    /// After activating the visibility of an interactive element, this method will return the response for the user.
326
    /// </summary>
327
    /// <param name="clear">If true, the element will be cleared.</param>
328
    /// <typeparam name="T">The type of interactive element.</typeparam>
329
    /// <typeparam name="TResponse">The type of the response (int, string, float...).</typeparam>
330
    /// <returns>The response of the user.</returns>
331
    /// <exception cref="ElementNotFoundException">Thrown when the element is invalid.</exception>
332
    /// <remarks>
333
    /// For more information, refer to the following resources:
334
    /// <list type="bullet">
335
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
336
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
337
    /// </list>
338
    /// </remarks>
339
    public static InteractionEventArgs<TResponse>? GetResponse<T, TResponse>(bool clear = true)
340
        where T : InteractiveElement<TResponse>
341
    {
342
        var element =
×
343
            GetVisibleElement<T>()
×
344
            ?? throw new ElementNotFoundException("Invalid element. Not found in the window.");
×
345
        DeactivateElement<T>(clear);
×
346
        return element.GetInteractionResponse;
×
347
    }
348

349
    /// <summary>
350
    /// This method attempts to activate the visibility of all elements.
351
    /// </summary>
352
    /// <remarks>
353
    /// For more information, refer to the following resources:
354
    /// <list type="bullet">
355
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
356
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
357
    /// </list>
358
    /// </remarks>
359
    public static void ActivateAllElements()
360
    {
361
        foreach (var element in s_elements)
×
362
        {
363
            if (!element.Visibility)
×
364
            {
365
                element.ToggleVisibility();
×
366
            }
367
        }
368
    }
×
369

370
    /// <summary>
371
    /// This method to deactivate the visibility of the element with the given id.
372
    /// </summary>
373
    /// <param name="id">The id of the element.</param>
374
    /// <param name="clear">If true, the element will be cleared.</param>
375
    /// <exception cref="ArgumentOutOfRangeException">Thrown when the id is out of range.</exception>
376
    /// <remarks>
377
    /// For more information, refer to the following resources:
378
    /// <list type="bullet">
379
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
380
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
381
    /// </list>
382
    /// </remarks>
383
    public static void DeactivateElement(int id, bool clear = true)
384
    {
385
        if (id < 0 || id >= s_elements.Count)
×
386
        {
387
            throw new ArgumentOutOfRangeException(nameof(id), "Invalid element ID.");
×
388
        }
389
        if (s_elements[id].Visibility)
×
390
        {
391
            s_elements[id].ToggleVisibility();
×
392
            if (clear)
×
393
            {
394
                s_elements[id].Clear();
×
395
            }
396
        }
397
        Refresh();
×
398
    }
×
399

400
    /// <summary>
401
    /// This method deactivate the visibility of the element with the given type.
402
    /// </summary>
403
    /// <param name="element">The element to be deactivated.</param>
404
    /// <param name="clear">If true, the element will be cleared.</param>
405
    /// <exception cref="ElementNotFoundException">Thrown when the element is invalid.</exception>
406
    /// <remarks>
407
    /// For more information, refer to the following resources:
408
    /// <list type="bullet">
409
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
410
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
411
    /// </list>
412
    /// </remarks>
413
    public static void DeactivateElement(Element element, bool clear = true)
414
    {
415
        if (element is null || !s_elements.Contains(element))
×
416
        {
417
            throw new ElementNotFoundException("Invalid element. Not found in the window.");
×
418
        }
419

420
        if (element.Visibility)
×
421
        {
422
            element.ToggleVisibility();
×
423
            if (clear)
×
424
            {
425
                element.Clear();
×
426
            }
427
        }
428
    }
×
429

430
    /// <summary>
431
    /// This method deactivate the visibility of the first element with the given type.
432
    /// </summary>
433
    /// <param name="clear">If true, the element will be cleared.</param>
434
    /// <typeparam name="T">The type of the element.</typeparam>
435
    /// <exception cref="ElementNotFoundException">Thrown when the element is invalid.</exception>
436
    /// <remarks>
437
    /// For more information, refer to the following resources:
438
    /// <list type="bullet">
439
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
440
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
441
    /// </list>
442
    /// </remarks>
443
    public static void DeactivateElement<T>(bool clear = true)
444
        where T : Element
445
    {
446
        var element =
×
447
            GetVisibleElement<T>()
×
448
            ?? throw new ElementNotFoundException("Invalid element. Not found in the window.");
×
449
        if (element.Visibility)
×
450
        {
451
            element.ToggleVisibility();
×
452
            if (clear)
×
453
            {
454
                element.Clear();
×
455
            }
456
        }
457
    }
×
458

459
    /// <summary>
460
    /// This method deactivate the visibility of all elements.
461
    /// </summary>
462
    /// <remarks>
463
    /// For more information, refer to the following resources:
464
    /// <list type="bullet">
465
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
466
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
467
    /// </list>
468
    /// </remarks>
469
    public static void DeactivateAllElements()
470
    {
471
        foreach (var element in s_elements)
×
472
        {
473
            if (element.Visibility)
×
474
            {
475
                element.ToggleVisibility();
×
476
            }
477
        }
478
        Refresh();
×
479
    }
×
480
    #endregion
481

482
    #region Utility Methods: AllowVisibilityToggle, GetLineAvailable, Clear, StopExecution, RenderOne, Refresh
483
    private static void UpdateIDs()
484
    {
485
        for (int i = 0; i < s_elements.Count; i++)
×
486
        {
487
            s_elements[i].Id = i;
×
488
        }
489
    }
×
490

491
    /// <summary>
492
    /// This method checks if the element can be toggled to visible.
493
    /// </summary>
494
    /// <param name="id">The id of the element.</param>
495
    /// <returns>True if the element can be toggled to visible, false otherwise.</returns>
496
    public static bool AllowVisibilityToggle(int id)
497
    {
498
        if (s_elements[id].IsInteractive)
×
499
        {
500
            int numberOfVisibleInteractiveElements = s_elements.Count(
×
501
                element => element.IsInteractive && element.Visibility
×
502
            );
×
503
            return numberOfVisibleInteractiveElements == 0;
×
504
        }
505
        else
506
        {
507
            int numberOfVisibleElements = s_elements.Count(
×
508
                element => element.GetType() == s_elements[id].GetType() && element.Visibility
×
509
            );
×
510
            return numberOfVisibleElements < s_elements[id].MaxNumberOfThisElement;
×
511
        }
512
    }
513

514
    /// <summary>
515
    /// Gives the last line available to draw an element on the console from a placement.
516
    /// </summary>
517
    /// <param name="placement">The placement of the element.</param>
518
    /// <returns>The last line available to draw an element on the console from a placement.</returns>
519
    /// <exception cref="ArgumentOutOfRangeException">Thrown when the placement is invalid.</exception>
520
    /// <remarks>
521
    /// For more information, refer to the following resources:
522
    /// <list type="bullet">
523
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
524
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
525
    /// </list>
526
    /// </remarks>
527
    public static int GetLineAvailable(Placement placement)
528
    {
529
        return placement switch
×
530
        {
×
531
            Placement.TopCenterFullWidth
×
532
                => s_elements
×
533
                    .Where(e => e.Placement == Placement.TopCenterFullWidth && e.Visibility)
×
534
                    .Sum(e => e.Height)
×
535
                    + s_elements
×
536
                        .Where(e => e.Placement == Placement.TopCenter && e.Visibility)
×
537
                        .Sum(e => e.Height)
×
538
                    + s_elements
×
539
                        .Where(e => e.Placement == Placement.TopLeft && e.Visibility)
×
540
                        .Sum(e => e.Height)
×
541
                    + s_elements
×
542
                        .Where(e => e.Placement == Placement.TopRight && e.Visibility)
×
543
                        .Sum(e => e.Height),
×
544
            Placement.TopCenter
×
545
                => s_elements
×
546
                    .Where(e => e.Placement == Placement.TopCenterFullWidth && e.Visibility)
×
547
                    .Sum(e => e.Height)
×
548
                    + s_elements
×
549
                        .Where(e => e.Placement == Placement.TopCenter && e.Visibility)
×
550
                        .Sum(e => e.Height),
×
551
            Placement.TopLeft
×
552
                => s_elements
×
553
                    .Where(e => e.Placement == Placement.TopCenterFullWidth && e.Visibility)
×
554
                    .Sum(e => e.Height)
×
555
                    + s_elements
×
556
                        .Where(e => e.Placement == Placement.TopLeft && e.Visibility)
×
557
                        .Sum(e => e.Height),
×
558

×
559
            Placement.TopRight
×
560
                => s_elements
×
561
                    .Where(e => e.Placement == Placement.TopCenterFullWidth && e.Visibility)
×
562
                    .Sum(e => e.Height)
×
563
                    + s_elements
×
564
                        .Where(e => e.Placement == Placement.TopRight && e.Visibility)
×
565
                        .Sum(e => e.Height),
×
566
            Placement.BottomCenterFullWidth
×
567
                => Console.WindowHeight
×
568
                    - s_elements
×
569
                        .Where(e => e.Placement == Placement.BottomCenterFullWidth && e.Visibility)
×
570
                        .Sum(e => e.Height),
×
571
            _ => throw new ArgumentOutOfRangeException(nameof(placement), "Invalid placement.")
×
572
        };
×
573
    }
574

575
    /// <summary>
576
    /// This method checks if the line is valid.
577
    /// </summary>
578
    /// <param name="line">The line to be checked.</param>
579
    /// <returns>The line if it is valid.</returns>
580
    /// <exception cref="ArgumentOutOfRangeException">Thrown when the line is out of range.</exception>
581
    /// <remarks>
582
    /// For more information, refer to the following resources:
583
    /// <list type="bullet">
584
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
585
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
586
    /// </list>
587
    /// </remarks>
588
    public static int? CheckLine(int? line)
589
    {
590
        if (line is null)
×
591
        {
592
            return line;
×
593
        }
594
        if (line < 0 || line >= Console.WindowHeight)
×
595
        {
596
            throw new ArgumentOutOfRangeException(
×
597
                nameof(line),
×
598
                $"Invalid line. The line must be between 0 and {Console.WindowHeight - 1}."
×
599
            );
×
600
        }
601
        return line;
×
602
    }
603

604
    /// <summary>
605
    /// This method clears the window.
606
    /// </summary>
607
    /// <param name="continuous">If true, the window will be cleared continuously.</param>
608
    /// <remarks>
609
    /// For more information, refer to the following resources:
610
    /// <list type="bullet">
611
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
612
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
613
    /// </list>
614
    /// </remarks>
615
    public static void Clear(bool continuous = false)
616
    {
617
        if (continuous)
×
618
        {
619
            for (int i = 0; i < Console.WindowHeight; i++)
×
620
            {
621
                Core.WriteContinuousString("".PadRight(Console.WindowWidth), i, false, 100, 10);
×
622
            }
623
        }
624
        else
625
        {
626
            for (int i = 0; i < Console.WindowHeight; i++)
×
627
            {
628
                Core.WritePositionedString(
×
629
                    "".PadRight(Console.WindowWidth),
×
630
                    TextAlignment.Center,
×
631
                    false,
×
632
                    i
×
633
                );
×
634
            }
635
        }
636
    }
×
637

638
    /// <summary>
639
    /// This method stops the execution of the program until a key is pressed.
640
    /// </summary>
641
    /// <remarks>
642
    /// For more information, refer to the following resources:
643
    /// <list type="bullet">
644
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
645
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
646
    /// </list>
647
    /// </remarks>
648
    public static void StopExecution(ConsoleKey key = ConsoleKey.Enter)
649
    {
650
        // wait until the user presses a key
651
        while (Console.ReadKey(intercept: true).Key != key)
×
652
        {
653
            Thread.Sleep(10);
×
654
        }
655
    }
×
656

657
    /// <summary>
658
    /// This method draws the element with the given id on the console.
659
    /// </summary>
660
    /// <param name="id">The id of the element.</param>
661
    /// <exception cref="ElementNotFoundException">Thrown when the id is out of range.</exception>
662
    /// <remarks>
663
    /// For more information, refer to the following resources:
664
    /// <list type="bullet">
665
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
666
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
667
    /// </list>
668
    /// </remarks>
669
    public static void RenderOneElement(int id)
670
    {
671
        if (id < 0 || id >= s_elements.Count)
×
672
        {
673
            throw new ElementNotFoundException("Invalid element ID.");
×
674
        }
675
        s_elements[id].RenderElement();
×
676
    }
×
677

678
    /// <summary>
679
    /// This method draws the given element on the console.
680
    /// </summary>
681
    /// <param name="element">The element to be drawn.</param>
682
    /// <exception cref="ElementNotFoundException">Thrown when the element is invalid.</exception>
683
    /// <remarks>
684
    /// For more information, refer to the following resources:
685
    /// <list type="bullet">
686
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
687
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
688
    /// </list>
689
    /// </remarks>
690
    public static void RenderOneElement(Element element)
691
    {
692
        if (element == null || !s_elements.Contains(element))
×
693
        {
694
            throw new ElementNotFoundException("Invalid element. Not found in the window.");
×
695
        }
696
        
697
        element.RenderElement();
×
698
    }
×
699

700
    /// <summary>
701
    /// This method draws all the non interactive elements of the window on the console.
702
    /// </summary>
703
    /// <remarks>
704
    /// For more information, refer to the following resources:
705
    /// <list type="bullet">
706
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
707
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
708
    /// </list>
709
    /// </remarks>
710
    public static void Refresh()
711
    {
712
        Clear();
×
713
        foreach (var element in s_elements)
×
714
        {
715
            element.RenderElement();
×
716
        }
717
    }
×
718

719
    /// <summary>
720
    /// This method is called to refresh the window when the size of the console is changed.
721
    /// </summary>
722
    public static void OnResize()
723
    {
724
        if (Core.IsScreenUpdated)
×
725
        {
726
            Core.SetConsoleDimensions();
×
727
            Refresh();
×
728
        }
729
    }
×
730

731

732
    /// <summary>
733
    /// This method draws all the space of the elements of the window on the console.
734
    /// </summary>
735
    /// <remarks>
736
    /// For more information, refer to the following resources:
737
    /// <list type="bullet">
738
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
739
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
740
    /// </list>
741
    /// </remarks>
742
    public static void RenderAllElementsSpace()
743
    {
744
        foreach (var element in s_elements)
×
745
        {
746
            element.RenderElementSpace();
×
747
        }
748
    }
×
749

750
    /// <summary>
751
    /// This method closes the window and exit the program.
752
    /// </summary>
753
    /// <remarks>
754
    /// For more information, refer to the following resources:
755
    /// <list type="bullet">
756
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
757
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
758
    /// </list>
759
    /// </remarks>
760
    public static void Close()
761
    {
NEW
762
        Core.LoadTerminalColorPanel();
×
763
        Clear(true);
×
764
        Console.CursorVisible = true;
×
765
        Environment.Exit(0);
×
766
    }
×
767
    #endregion
768

769
    #region Info Methods: ListWindowElements, ListClassesInheritingElement, ListClassesInheritingInteractiveElement
770
    /// <summary>
771
    /// This method displays a list of all elements in the window and adds a table to the window.
772
    /// </summary>
773
    /// <param name="placement">The placement of the element.</param>
774
    /// <remarks>
775
    /// For more information, refer to the following resources:
776
    /// <list type="bullet">
777
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
778
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
779
    /// </list>
780
    /// </remarks>
781
    public static void AddListWindowElements(Placement placement = Placement.TopCenter)
782
    {
783
        TableView<string> table =
×
784
            new(
×
785
                "Window Elements",
×
786
                new List<string>
×
787
                {
×
788
                    "Id",
×
789
                    "Type",
×
790
                    "Visibility",
×
791
                    "Height",
×
792
                    "Width",
×
793
                    "Line",
×
794
                    "Placement",
×
795
                    "IsInteractive",
×
796
                    "Source"
×
797
                },
×
798
                null,
×
799
                placement
×
800
            )
×
801
            {
×
802
                ElementSource = Source.Library
×
803
            };
×
804
        AddElement(table);
×
805
        foreach (var element in s_elements)
×
806
        {
807
            table.AddLine(
×
808
                new List<string>
×
809
                {
×
810
                    element.Id.ToString(),
×
811
                    element.GetType().Name,
×
812
                    element.Visibility.ToString(),
×
813
                    element.Height.ToString(),
×
814
                    element.Width.ToString(),
×
815
                    element.Line.ToString(),
×
816
                    element.Placement.ToString(),
×
817
                    element.IsInteractive.ToString(),
×
818
                    element.ElementSource.ToString()
×
819
                }
×
820
            );
×
821
        }
822
    }
×
823

824
    /// <summary>
825
    /// This method is used to get a list of all the types of the elements in the window.
826
    /// </summary>
827
    /// <returns>A list of all the types of the elements in the window.</returns>
828
    /// <remarks>
829
    /// For more information, refer to the following resources:
830
    /// <list type="bullet">
831
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
832
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
833
    /// </list>
834
    /// </remarks>
835
    public static List<string>? GetListWindowElements()
836
    {
837
        TableView<string> table =
×
838
            new(
×
839
                "Window Elements",
×
840
                new List<string>
×
841
                {
×
842
                    "Id",
×
843
                    "Type",
×
844
                    "Visibility",
×
845
                    "Height",
×
846
                    "Width",
×
847
                    "Line",
×
848
                    "Placement",
×
849
                    "IsInteractive",
×
850
                    "Source"
×
851
                }
×
852
            )
×
853
            {
×
854
                ElementSource = Source.Library
×
855
            };
×
856
        foreach (var element in s_elements)
×
857
        {
858
            table.AddLine(
×
859
                new List<string>
×
860
                {
×
861
                    element.Id.ToString(),
×
862
                    element.GetType().Name,
×
863
                    element.Visibility.ToString(),
×
864
                    element.Height.ToString(),
×
865
                    element.Width.ToString(),
×
866
                    element.Line.ToString(),
×
867
                    element.Placement.ToString(),
×
868
                    element.IsInteractive.ToString(),
×
869
                    element.ElementSource.ToString()
×
870
                }
×
871
            );
×
872
        }
873
        return table.GetColumnData("Type");
×
874
    }
875

876
    /// <summary>
877
    /// This method gives a list of all classes inheriting from the Element (and so InteractiveElement as well) class and adds a table to the window.
878
    /// </summary>
879
    /// <param name="placement">The placement of the element.</param>
880
    /// <returns>The list of all classes inheriting from the Element class.</returns>
881
    /// <remarks>
882
    /// For more information, refer to the following resources:
883
    /// <list type="bullet">
884
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
885
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
886
    /// </list>
887
    /// </remarks>
888
    public static void AddListClassesInheritingElement(Placement placement = Placement.TopCenter)
889
    {
890
        var types = new List<Type>();
×
891
        foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
×
892
        {
893
            if (
×
894
                assembly.FullName != null
×
895
                && !assembly.FullName.StartsWith("mscorlib")
×
896
                && !assembly.FullName.StartsWith("System")
×
897
                && !assembly.FullName.StartsWith("Microsoft")
×
898
            )
×
899
            {
900
                types.AddRange(assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(Element))));
×
901
            }
902
        }
903
        TableView<string> table =
×
904
            new("Element Classes", new List<string> { "Id", "Type", "Project" }, null, placement);
×
905
        var id = 0;
×
906
        foreach (var type in types)
×
907
        {
908
            if (type.IsAbstract)
×
909
            {
910
                continue;
911
            }
912
            table.AddLine(
×
913
                new List<string> { $"{id}", type.Name, type.Assembly.GetName().Name ?? "Unknown" }
×
914
            );
×
915
            id += 1;
×
916
        }
917
        table.ElementSource = Source.Library;
×
918
        AddElement(table);
×
919
    }
×
920

921
    /// <summary>
922
    /// This method is used to get a list of all the types of the classes inheriting from the Element class.
923
    /// </summary>
924
    /// <returns>A list of all the types of the classes inheriting from the Element class.</returns>
925
    /// <remarks>
926
    /// For more information, refer to the following resources:
927
    /// <list type="bullet">
928
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
929
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
930
    /// </list>
931
    /// </remarks>
932
    public static List<string>? GetListClassesInheritingElement()
933
    {
934
        var types = new List<Type>();
×
935
        foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
×
936
        {
937
            if (
×
938
                assembly.FullName != null
×
939
                && !assembly.FullName.StartsWith("mscorlib")
×
940
                && !assembly.FullName.StartsWith("System")
×
941
                && !assembly.FullName.StartsWith("Microsoft")
×
942
            )
×
943
            {
944
                types.AddRange(assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(Element)) && t != typeof(InteractiveElement<>)));
×
945
            }
946
        }
947
        TableView<string> table =
×
948
            new("Element Classes", new List<string> { "Id", "Type", "Project" });
×
949
        var id = 0;
×
950
        foreach (var type in types)
×
951
        {
952
            if (type.IsAbstract)
×
953
            {
954
                continue;
955
            }
956
            table.AddLine(
×
957
                new List<string> { $"{id}", type.Name, type.Assembly.GetName().Name ?? "Unknown" }
×
958
            );
×
959
            id += 1;
×
960
        }
961
        return table.GetColumnData("Type");
×
962
    }
963

964
    /// <summary>
965
    /// This method gives a list of all classes inheriting from the InteractiveElement class and adds a table to the window.
966
    /// </summary>
967
    /// <param name="placement">The placement of the element.</param>
968
    /// <returns>The list of all classes inheriting from the InteractiveElement class.</returns>
969
    public static void AddListClassesInheritingInteractiveElement(
970
        Placement placement = Placement.TopCenter
971
    )
972
    {
973
        var types = new List<Type>();
×
974
        foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
×
975
        {
976
            if (
×
977
                assembly.FullName != null
×
978
                && !assembly.FullName.StartsWith("mscorlib")
×
979
                && !assembly.FullName.StartsWith("System")
×
980
                && !assembly.FullName.StartsWith("Microsoft")
×
981
            )
×
982
            {
983
                types.AddRange(
×
984
                    assembly
×
985
                        .GetTypes()
×
986
                        .Where(
×
987
                            t =>
×
988
                                t.BaseType != null
×
989
                                && t.BaseType.IsGenericType
×
990
                                && t.BaseType.GetGenericTypeDefinition()
×
991
                                    == typeof(InteractiveElement<>)
×
992
                        )
×
993
                );
×
994
            }
995
        }
996
        TableView<string> table =
×
997
            new(
×
998
                "Interactive Element Classes",
×
999
                new List<string> { "Id", "Type", "Project" },
×
1000
                null,
×
1001
                placement
×
1002
            );
×
1003
        var id = 0;
×
1004
        foreach (var type in types)
×
1005
        {
1006
            table.AddLine(
×
1007
                new List<string> { $"{id}", type.Name, type.Assembly.GetName().Name ?? "Unknown" }
×
1008
            );
×
1009
            id += 1;
×
1010
        }
1011
        table.ElementSource = Source.Library;
×
1012
        AddElement(table);
×
1013
    }
×
1014

1015
    /// <summary>
1016
    /// This method is used to get a list of all the types of the classes inheriting from the InteractiveElement class.
1017
    /// </summary>
1018
    /// <returns>A list of all the types of the classes inheriting from the InteractiveElement class.</returns>
1019
    /// <remarks>
1020
    /// For more information, refer to the following resources:
1021
    /// <list type="bullet">
1022
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
1023
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
1024
    /// </list>
1025
    /// </remarks>
1026
    public static List<string>? GetListClassesInheritingInteractiveElement()
1027
    {
1028
        var types = new List<Type>();
×
1029
        foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
×
1030
        {
1031
            if (
×
1032
                assembly.FullName != null
×
1033
                && !assembly.FullName.StartsWith("mscorlib")
×
1034
                && !assembly.FullName.StartsWith("System")
×
1035
                && !assembly.FullName.StartsWith("Microsoft")
×
1036
            )
×
1037
            {
1038
                types.AddRange(
×
1039
                    assembly
×
1040
                        .GetTypes()
×
1041
                        .Where(
×
1042
                            t =>
×
1043
                                t.BaseType != null
×
1044
                                && t.BaseType.IsGenericType
×
1045
                                && t.BaseType.GetGenericTypeDefinition()
×
1046
                                    == typeof(InteractiveElement<>)
×
1047
                        )
×
1048
                );
×
1049
            }
1050
        }
1051
        TableView<string> table =
×
1052
            new("Interactive Element Classes", new List<string> { "Id", "Type", "Project" });
×
1053
        var id = 0;
×
1054
        foreach (var type in types)
×
1055
        {
1056
            table.AddLine(
×
1057
                new List<string> { $"{id}", type.Name, type.Assembly.GetName().Name ?? "Unknown" }
×
1058
            );
×
1059
            id += 1;
×
1060
        }
1061
        return table.GetColumnData("Type");
×
1062
    }
1063

1064
    /// <summary>
1065
    /// This method displays a list of all elements in the window, a list of all classes inheriting from the Element class and a list of all classes inheriting from the InteractiveElement class.
1066
    /// </summary>
1067
    /// <remarks>
1068
    /// For more information, refer to the following resources:
1069
    /// <list type="bullet">
1070
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
1071
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
1072
    /// </list>
1073
    /// </remarks>
1074
    public static void AddDashboard()
1075
    {
1076
        AddListClassesInheritingElement(Placement.TopLeft);
×
1077
        AddListClassesInheritingInteractiveElement(Placement.TopRight);
×
1078
        AddListWindowElements(Placement.TopCenter);
×
1079
    }
×
1080

1081
    /// <summary>
1082
    /// This method removes the dashboard TableView from the window.
1083
    /// </summary>
1084
    /// <remarks>
1085
    /// For more information, refer to the following resources:
1086
    /// <list type="bullet">
1087
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
1088
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
1089
    /// </list>
1090
    /// </remarks>
1091
    public static void RemoveDashboard()
1092
    {
1093
        RemoveLibraryElement<TableView<string>>();
×
1094
        RemoveLibraryElement<TableView<string>>();
×
1095
        RemoveLibraryElement<TableView<string>>();
×
1096
    }
×
1097
    #endregion
1098
}
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