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

MorganKryze / ConsoleAppVisuals / 8194502154

07 Mar 2024 08:37PM UTC coverage: 95.179% (+9.0%) from 86.165%
8194502154

push

github

MorganKryze
🚑 last try

979 of 1112 branches covered (88.04%)

Branch coverage included in aggregate %.

1943 of 1958 relevant lines covered (99.23%)

723.5 hits per line

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

94.94
/src/ConsoleAppVisuals/Window.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;
6

7
/// <summary>
8
/// The Window class manages the elements that are to be displayed on the console.
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 static class Window
18
{
19
    #region Fields: s_elements
20
    private static readonly List<Element> s_elements = new();
3✔
21
    #endregion
22

23
    #region Constants: DefaultVisibility
24
    /// <summary>
25
    /// The default visibility of the elements when they are added to the window.
26
    /// </summary>
27
    /// <remarks>
28
    /// This value should not be changed.
29
    /// Each time the user adds an element to the window, it will try to toggle the visibility of the element.
30
    /// </remarks>
31
    public const bool DEFAULT_VISIBILITY = false;
32
    #endregion
33

34
    #region Properties: NextId, NumberOfElements, Elements
35
    /// <summary>
36
    /// Gives the next id number each time a new element is added to the window.
37
    /// </summary>
38
    public static int NextId => s_elements.Count;
132✔
39

40
    /// <summary>
41
    /// Gives the number of elements in the window.
42
    /// </summary>
43
    public static int CountElements => s_elements.Count;
18✔
44

45
    /// <summary>
46
    /// Gives the list of elements in the window.
47
    /// </summary>
48
    public static List<Element> Elements => s_elements;
42✔
49
    #endregion
50

51
    #region Managing Methods: Get, Add, Insert, Remove, RemoveAll
52

53
    /// <summary>
54
    /// This method returns a range of elements given a start and end ids.
55
    /// </summary>
56
    /// <param name="start">The start id of the range.</param>
57
    /// <param name="end">The end id of the range.</param>
58
    /// <returns>The range of elements from the start to the end id.</returns>
59
    public static List<Element> GetRange(int start, int end)
60
    {
61
        return s_elements.GetRange(start, end);
99✔
62
    }
63

64
    /// <summary>
65
    /// This method returns the first element of the given type.
66
    /// </summary>
67
    /// <typeparam name="T">The type of the element.</typeparam>
68
    /// <returns>The element with the given type if it exists, null otherwise.</returns>
69
    /// <remarks>
70
    /// For more information, refer to the following resources:
71
    /// <list type="bullet">
72
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
73
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
74
    /// </list>
75
    /// </remarks>
76
    public static T? GetElement<T>()
77
        where T : Element
78
    {
79
        return s_elements.Find(element => element.GetType() == typeof(T)) as T;
48✔
80
    }
81

82
    /// <summary>
83
    /// This method returns the first element of the given type and id.
84
    /// </summary>
85
    /// <param name="id">The id of the element.</param>
86
    /// <returns>The element with the given id if it exists, null otherwise.</returns>
87
    /// <exception cref="ArgumentOutOfRangeException">Thrown when the id is out of range.</exception>
88
    /// <remarks>
89
    /// For more information, refer to the following resources:
90
    /// <list type="bullet">
91
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
92
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
93
    /// </list>
94
    /// </remarks>
95
    public static T? GetElement<T>(int id)
96
        where T : Element
97
    {
98
        if (id < 0 || id >= s_elements.Count)
15✔
99
        {
100
            throw new ArgumentOutOfRangeException(nameof(id), "Invalid element ID.");
6✔
101
        }
102
        return (T)s_elements[id];
9✔
103
    }
104

105
    /// <summary>
106
    /// This method returns the first visible element with the given type.
107
    /// </summary>
108
    /// <typeparam name="T">The type of the element.</typeparam>
109
    /// <returns>The visible element with the given type if it exists, null otherwise.</returns>
110
    /// <remarks>
111
    /// For more information, refer to the following resources:
112
    /// <list type="bullet">
113
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
114
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
115
    /// </list>
116
    /// </remarks>
117
    public static T? GetVisibleElement<T>()
118
        where T : Element
119
    {
120
        return s_elements.Find(element => element.GetType() == typeof(T) && element.Visibility)
30✔
121
            as T;
18✔
122
    }
123

124
    /// <summary>
125
    /// This method adds elements to the window.
126
    /// </summary>
127
    /// <param name="elements">The elements to be added.</param>
128
    /// <remarks>
129
    /// For more information, refer to the following resources:
130
    /// <list type="bullet">
131
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
132
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
133
    /// </list>
134
    /// </remarks>
135
    public static void AddElement(params Element[] elements)
136
    {
137
        foreach (var element in elements)
438✔
138
        {
139
            element.Id = NextId;
126✔
140
            s_elements.Add(element);
126✔
141
            if (!element.IsInteractive && AllowVisibilityToggle(element.Id))
126✔
142
            {
143
                element.ToggleVisibility();
45✔
144
            }
145
        }
146
    }
93✔
147

148
    /// <summary>
149
    /// This method inserts an element to the window at the given id.
150
    /// </summary>
151
    /// <param name="element">The element to be inserted.</param>
152
    /// <param name="id">The id of the element.</param>
153
    /// <exception cref="ArgumentOutOfRangeException">Thrown when the id is out of range.</exception>
154
    /// <remarks>
155
    /// For more information, refer to the following resources:
156
    /// <list type="bullet">
157
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
158
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
159
    /// </list>
160
    /// </remarks>
161
    public static void InsertElement(Element element, int id)
162
    {
163
        if (id < 0 || id > s_elements.Count)
9✔
164
        {
165
            throw new ArgumentOutOfRangeException(nameof(id), "Invalid element ID.");
6✔
166
        }
167
        s_elements.Insert(id, element);
3✔
168
    }
3✔
169

170
    /// <summary>
171
    /// This method removes the first element with the given type.
172
    /// </summary>
173
    /// <typeparam name="T">The type of the element.</typeparam>
174
    /// <exception cref="ElementNotFoundException">Thrown when the element is invalid.</exception>
175
    /// <returns>True if the element is successfully removed, false otherwise.</returns>
176
    /// <remarks>
177
    /// For more information, refer to the following resources:
178
    /// <list type="bullet">
179
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
180
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
181
    /// </list>
182
    /// </remarks>
183
    public static bool RemoveElement<T>()
184
        where T : Element
185
    {
186
        var element =
12✔
187
            GetElement<T>()
12✔
188
            ?? throw new ElementNotFoundException("Invalid element. Not found in the window.");
12✔
189
        var state = s_elements.Remove(element);
9✔
190
        UpdateIDs();
9✔
191
        return state;
9✔
192
    }
193

194
    /// <summary>
195
    /// This method removes the element with the given id.
196
    /// </summary>
197
    /// <param name="id">The id of the element.</param>
198
    /// <exception cref="ArgumentOutOfRangeException">Thrown when the id is out of range.</exception>
199
    /// <remarks>
200
    /// For more information, refer to the following resources:
201
    /// <list type="bullet">
202
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
203
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
204
    /// </list>
205
    /// </remarks>
206
    public static void RemoveElement(int id)
207
    {
208
        if (id < 0 || id >= s_elements.Count)
9✔
209
        {
210
            throw new ArgumentOutOfRangeException(nameof(id), "Invalid element ID.");
6✔
211
        }
212
        s_elements.RemoveAt(id);
3✔
213
        UpdateIDs();
3✔
214
    }
3✔
215

216
    /// <summary>
217
    /// This method removes the given element.
218
    /// </summary>
219
    /// <param name="elements">The elements to be removed.</param>
220
    /// <exception cref="ElementNotFoundException">Thrown when the element is invalid.</exception>
221
    /// <returns>True if the element is successfully removed, false otherwise.</returns>
222
    /// <remarks>
223
    /// For more information, refer to the following resources:
224
    /// <list type="bullet">
225
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
226
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
227
    /// </list>
228
    /// </remarks>
229
    public static bool RemoveElement(params Element[] elements)
230
    {
231
        bool state = true;
12✔
232

233
        foreach (var element in elements)
45✔
234
        {
235
            if (!s_elements.Contains(element))
12✔
236
            {
237
                throw new ElementNotFoundException("Invalid element. Not found in the window.");
3✔
238
            }
239
            state &= s_elements.Remove(element);
9✔
240
        }
241

242
        UpdateIDs();
9✔
243
        return state;
9✔
244
    }
245

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

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

293
    /// <summary>
294
    /// This method attempts to activate the visibility of the given element.
295
    /// </summary>
296
    /// <param name="element">The element to be activated.</param>
297
    /// <param name="render">If true, the element will be rendered.</param>
298
    /// <exception cref="ElementNotFoundException">Thrown when the element is invalid.</exception>
299
    [Visual]
300
    public static void ActivateElement(Element element, bool render = true)
301
    {
302
        if (!s_elements.Contains(element))
303
        {
304
            throw new ElementNotFoundException("Invalid element. Not found in the window.");
305
        }
306
        if (!element.Visibility)
307
        {
308
            element.ToggleVisibility();
309
        }
310
        if (render)
311
        {
312
            Render(element);
313
        }
314
    }
315

316
    /// <summary>
317
    /// This method attempts to activate the visibility of the first element of the given type.
318
    /// </summary>
319
    /// <param name="render">If true, the element will be rendered.</param>
320
    /// <typeparam name="T">The type of the element.</typeparam>
321
    /// <exception cref="ElementNotFoundException">Thrown when the element is invalid.</exception>
322
    /// <remarks>
323
    /// For more information, refer to the following resources:
324
    /// <list type="bullet">
325
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
326
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
327
    /// </list>
328
    /// </remarks>
329
    [Visual]
330
    public static void ActivateElement<T>(bool render = true)
331
        where T : Element
332
    {
333
        var element =
334
            GetElement<T>()
335
            ?? throw new ElementNotFoundException("Invalid element. Not found in the window.");
336
        if (!element.Visibility)
337
        {
338
            element.ToggleVisibility();
339
        }
340
        if (render)
341
        {
342
            Render(element);
343
        }
344
    }
345

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

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

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

416
        if (element.Visibility)
3✔
417
        {
418
            element.ToggleVisibility();
3✔
419
            if (clear)
3!
420
            {
421
                element.Clear();
×
422
            }
423
        }
424
    }
3✔
425

426
    /// <summary>
427
    /// This method deactivate the visibility of the first element with the given type.
428
    /// </summary>
429
    /// <param name="clear">If true, the element will be cleared.</param>
430
    /// <typeparam name="T">The type of the element.</typeparam>
431
    /// <exception cref="ElementNotFoundException">Thrown when the element is invalid.</exception>
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 static void DeactivateElement<T>(bool clear = true)
440
        where T : Element
441
    {
442
        var element =
9✔
443
            GetVisibleElement<T>()
9✔
444
            ?? throw new ElementNotFoundException("Invalid element. Not found in the window.");
9✔
445
        if (element.Visibility)
6✔
446
        {
447
            element.ToggleVisibility();
6✔
448
            if (clear)
6!
449
            {
450
                element.Clear();
×
451
            }
452
        }
453
    }
6✔
454

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

477
    #region Utility Methods: AllowVisibilityToggle, GetLineAvailable, Clear, StopExecution, Refresh
478
    private static void UpdateIDs()
479
    {
480
        for (int i = 0; i < s_elements.Count; i++)
48✔
481
        {
482
            s_elements[i].Id = i;
3✔
483
        }
484
    }
21✔
485

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

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

18✔
554
            Placement.TopRight
18✔
555
                => s_elements
3✔
556
                    .Where(e => e.Placement == Placement.TopCenterFullWidth && e.Visibility)
18✔
557
                    .Sum(e => e.Height)
6✔
558
                    + s_elements
3✔
559
                        .Where(e => e.Placement == Placement.TopRight && e.Visibility)
18✔
560
                        .Sum(e => e.Height),
3✔
561
            Placement.BottomCenterFullWidth
18✔
562
                => (Console.WindowHeight == 0 ? 0 : Console.WindowHeight - 1)
6!
563
                    - s_elements
6✔
564
                        .Where(e => e.Placement == Placement.BottomCenterFullWidth && e.Visibility)
18✔
565
                        .Sum(e => e.Height),
9✔
566
            _ => throw new ArgumentOutOfRangeException(nameof(placement), "Invalid placement.")
×
567
        };
18✔
568
    }
569

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

601
    /// <summary>
602
    /// This method stops the execution of the program until a key is pressed.
603
    /// </summary>
604
    /// <param name="key">The key to be pressed to continue the execution.</param>
605
    /// <remarks>
606
    /// For more information, refer to the following resources:
607
    /// <list type="bullet">
608
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
609
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
610
    /// </list>
611
    /// </remarks>
612
    [Visual]
613
    public static void Freeze(ConsoleKey key = ConsoleKey.Enter)
614
    {
615
        while (Console.ReadKey(intercept: true).Key != key)
616
        {
617
            Thread.Sleep(10);
618
        }
619
    }
620

621
    /// <summary>
622
    /// This method draws all visible elements of the window on the console.
623
    /// </summary>
624
    /// <remarks>
625
    /// For more information, refer to the following resources:
626
    /// <list type="bullet">
627
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
628
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
629
    /// </list>
630
    /// </remarks>
631
    [Visual]
632
    public static void Render()
633
    {
634
        Clear();
635
        Core.IsScreenUpdated();
636
        foreach (var element in s_elements)
637
        {
638
            element.RenderElement();
639
        }
640
    }
641

642
    /// <summary>
643
    /// This method draws all given visible elements of the window on the console.
644
    /// </summary>
645
    /// <param name="elements">The elements to be drawn.</param>
646
    /// <exception cref="ElementNotFoundException">Thrown when the element is not found in the window.</exception>
647
    /// <remarks>
648
    /// For more information, refer to the following resources:
649
    /// <list type="bullet">
650
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
651
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
652
    /// </list>
653
    /// </remarks>
654
    [Visual]
655
    public static void Render(params Element[] elements)
656
    {
657
        if (Core.IsScreenUpdated())
658
        {
659
            Render();
660
            return;
661
        }
662

663
        foreach (var element in elements)
664
        {
665
            if (!s_elements.Contains(element))
666
            {
667
                throw new ElementNotFoundException($"Element {element} is not in the Window.");
668
            }
669
            element.RenderElement();
670
        }
671
    }
672

673
    /// <summary>
674
    /// This method draws all the space of the visible elements of the window on the console.
675
    /// </summary>
676
    /// <returns>True if the space of the elements is successfully drawn, false otherwise.</returns>
677
    /// <remarks>
678
    /// For more information, refer to the following resources:
679
    /// <list type="bullet">
680
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
681
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
682
    /// </list>
683
    /// </remarks>
684
    [Visual]
685
    public static bool RenderElementsSpace()
686
    {
687
        foreach (var element in s_elements)
688
        {
689
            element.RenderElementSpace();
690
        }
691
        return true;
692
    }
693

694
    /// <summary>
695
    /// This method clears the console.
696
    /// </summary>
697
    /// <param name="continuous">If true, the window will be cleared continuously.</param>
698
    /// <param name="startLine">The start line of the window to be cleared.</param>
699
    /// <param name="length">The number of lines to be cleared.</param>
700
    /// <param name="step">The step of the window to be cleared.</param>
701
    /// <returns>True if the window is successfully cleared, false otherwise.</returns>
702
    /// <remarks>
703
    /// For more information, refer to the following resources:
704
    /// <list type="bullet">
705
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
706
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
707
    /// </list>
708
    /// </remarks>
709
    [Visual]
710
    public static bool Clear(
711
        bool continuous = false,
712
        int? startLine = null,
713
        int? length = null,
714
        int step = 1
715
    )
716
    {
717
        int stepMax = Console.WindowHeight == 0 ? 1 : Console.WindowHeight;
718
        startLine = CheckLine(startLine) ?? 0;
719
        length = CheckLine(startLine + length) ?? Console.WindowHeight;
720
        if (step < 1 || step > stepMax)
721
        {
722
            throw new ArgumentOutOfRangeException(
723
                nameof(step),
724
                "Invalid step, less than 0 or greater than the window height."
725
            );
726
        }
727

728
        for (int i = (int)startLine; i < (int)length; i += step)
729
        {
730
            if (continuous)
731
            {
732
                Core.WriteContinuousString("".PadRight(Console.WindowWidth), i, false, 100, 10);
733
            }
734
            else
735
            {
736
                Core.WritePositionedString(
737
                    "".PadRight(Console.WindowWidth),
738
                    TextAlignment.Center,
739
                    false,
740
                    i
741
                );
742
            }
743
        }
744
        return true;
745
    }
746

747
    /// <summary>
748
    /// This method "opens" the window (clears the console and hides the cursor).
749
    /// </summary>
750
    [Visual]
751
    public static void Open()
752
    {
753
        Console.Clear();
754
        Console.CursorVisible = false;
755
    }
756

757
    /// <summary>
758
    /// This method clears the window and exit the program.
759
    /// </summary>
760
    /// <remarks>
761
    /// For more information, refer to the following resources:
762
    /// <list type="bullet">
763
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
764
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
765
    /// </list>
766
    /// </remarks>
767
    [Visual]
768
    public static void Close()
769
    {
770
        Core.LoadTerminalColorPanel();
771
        Clear(true);
772
        Console.CursorVisible = true;
773
        Environment.Exit(0);
774
    }
775
    #endregion
776
}
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