• 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

99.12
/src/ConsoleAppVisuals/Core.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 <see cref="Core"/> class contains all the interactions between the application and the console.
9
/// </summary>
10
public static class Core
11
{
12
    #region Constants
13
    /// <summary>
14
    /// This constant is used to define the negative anchor to put inside a string to be recognized as negative.
15
    /// </summary>
16
    public const string NEGATIVE_ANCHOR = "/neg";
17
    #endregion
18

19
    #region Fields
20
    private static (char, char) s_selector = ('â–¶', 'â—€');
3✔
21

22
    [Visual]
23
    private static int s_previousWindowWidth = Console.WindowWidth;
3✔
24

25
    [Visual]
26
    private static int s_previousWindowHeight = Console.WindowHeight;
3✔
27
    private static (ConsoleColor, ConsoleColor) s_colorPanel = (
3✔
28
        ConsoleColor.White,
3✔
29
        ConsoleColor.Black
3✔
30
    );
3✔
31
    private static (ConsoleColor, ConsoleColor) s_initialColorPanel = (
3✔
32
        s_colorPanel.Item1,
3✔
33
        s_colorPanel.Item2
3✔
34
    );
3✔
35

36
    [Visual]
37
    private static (ConsoleColor, ConsoleColor) s_terminalColorPanel = (
3✔
38
        Console.ForegroundColor,
3✔
39
        Console.BackgroundColor
3✔
40
    );
3✔
41
    private static (ConsoleColor, ConsoleColor) s_savedColorPanel;
42

43
    private static readonly Random s_rnd = new();
3✔
44
    #endregion
45

46
    #region Properties
47
    /// <summary>
48
    /// This property is used to get the selector of the console menus.
49
    /// </summary>
50
    public static (char, char) GetSelector => s_selector;
15✔
51

52
    /// <summary>
53
    /// This property is used to get the colors of the console.
54
    /// </summary>
55
    public static (ConsoleColor, ConsoleColor) GetColorPanel => s_colorPanel;
9✔
56

57
    /// <summary>
58
    /// This property is used to get the initial colors of the console.
59
    /// </summary>
60
    public static (ConsoleColor, ConsoleColor) GetInitialColorPanel => s_initialColorPanel;
3✔
61
    #endregion
62

63
    #region Low abstraction level methods
64
    /// <summary>
65
    /// This method is used to check if the screen has been updated.
66
    /// </summary>
67
    /// <returns>True if the screen has been updated, false otherwise.</returns>
68
    /// <remarks>
69
    /// For more information, refer to the following resources:
70
    /// <list type="bullet">
71
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
72
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
73
    /// </list>
74
    /// </remarks>
75
    [Visual]
76
    public static bool IsScreenUpdated()
77
    {
78
        var isUpdated =
79
            Console.WindowWidth != s_previousWindowWidth
80
            || Console.WindowHeight != s_previousWindowHeight
81
            || s_colorPanel != s_initialColorPanel;
82
        if (isUpdated)
83
        {
84
            SetConsoleDimensions();
85
        }
86
        return isUpdated;
87
    }
88

89
    /// <summary>
90
    /// This method is used to set the selector of the console menus.
91
    /// </summary>
92
    /// <param name="onward">The new selector facing forward.</param>
93
    /// <param name="backward">The new selector facing backward.</param>
94
    /// <remarks>
95
    /// For more information, refer to the following resources:
96
    /// <list type="bullet">
97
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
98
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
99
    /// </list>
100
    /// </remarks>
101
    public static void SetSelector(char onward, char backward)
102
    {
103
        s_selector = (onward, backward);
3✔
104
    }
3✔
105

106
    /// <summary>
107
    /// This method changes the font color of the console.
108
    /// </summary>
109
    /// <param name="color">The new font color.</param>
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
    [Visual]
118
    public static void SetForegroundColor(ConsoleColor color)
119
    {
120
        s_colorPanel.Item1 = color;
121
        Console.ForegroundColor = color;
122
    }
123

124
    /// <summary>
125
    /// This method changes the background color of the console.
126
    /// </summary>
127
    /// <param name="color">The new background color.</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
    [Visual]
136
    public static void SetBackgroundColor(ConsoleColor color)
137
    {
138
        s_colorPanel.Item2 = color;
139
        Console.BackgroundColor = color;
140
    }
141

142
    /// <summary>
143
    /// This method is used to save the current color panel.
144
    /// </summary>
145
    /// <remarks>
146
    /// For more information, refer to the following resources:
147
    /// <list type="bullet">
148
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
149
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
150
    /// </list>
151
    /// </remarks>
152
    [Visual]
153
    public static void SaveColorPanel() => s_savedColorPanel = s_colorPanel;
154

155
    /// <summary>
156
    /// This method is used to load the saved color panel.
157
    /// </summary>
158
    /// <remarks>
159
    /// For more information, refer to the following resources:
160
    /// <list type="bullet">
161
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
162
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
163
    /// </list>
164
    /// </remarks>
165
    [Visual]
166
    public static void LoadSavedColorPanel() => s_colorPanel = s_savedColorPanel;
167

168
    /// <summary>
169
    /// This method is used to load the terminal color panel.
170
    /// </summary>
171
    /// <remarks>
172
    /// For more information, refer to the following resources:
173
    /// <list type="bullet">
174
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
175
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
176
    /// </list>
177
    /// </remarks>
178
    [Visual]
179
    public static void LoadTerminalColorPanel() => s_colorPanel = s_terminalColorPanel;
180

181
    /// <summary>
182
    /// This method is used to set the dimensions of the console to the Core variables associated. This does not change the actual dimensions of the console.
183
    /// </summary>
184
    /// <remarks>
185
    /// For more information, refer to the following resources:
186
    /// <list type="bullet">
187
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
188
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
189
    /// </list>
190
    /// </remarks>
191
    [Visual]
192
    public static void SetConsoleDimensions()
193
    {
194
        s_previousWindowWidth = Console.WindowWidth;
195
        s_previousWindowHeight = Console.WindowHeight;
196
    }
197

198
    /// <summary>
199
    /// This methods is used to get a random color from a selection.
200
    /// </summary>
201
    /// <returns>A random color.</returns>
202
    /// <remarks>
203
    /// For more information, refer to the following resources:
204
    /// <list type="bullet">
205
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
206
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
207
    /// </list>
208
    /// </remarks>
209
    public static ConsoleColor GetRandomColor()
210
    {
211
        var colors = new List<ConsoleColor>
3✔
212
        {
3✔
213
            ConsoleColor.Red,
3✔
214
            ConsoleColor.Green,
3✔
215
            ConsoleColor.Blue,
3✔
216
            ConsoleColor.Yellow,
3✔
217
            ConsoleColor.Magenta,
3✔
218
            ConsoleColor.Cyan
3✔
219
        };
3✔
220
        return colors[s_rnd.Next(colors.Count)];
3✔
221
    }
222

223
    /// <summary>
224
    /// This method is used to restore the default colors of the console.
225
    /// </summary>
226
    /// <remarks>
227
    /// For more information, refer to the following resources:
228
    /// <list type="bullet">
229
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
230
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
231
    /// </list>
232
    /// </remarks>
233
    [Visual]
234
    public static void RestoreColorPanel()
235
    {
236
        Console.ForegroundColor = s_initialColorPanel.Item1;
237
        Console.BackgroundColor = s_initialColorPanel.Item2;
238
        s_colorPanel = s_initialColorPanel;
239
    }
240

241
    /// <summary>
242
    /// This method changes the font and background colors of the console in order to apply
243
    /// a negative to highlight the text or not.
244
    /// </summary>
245
    /// <param name="negative">If true, the text is highlighted.</param>
246
    /// <remarks>
247
    /// For more information, refer to the following resources:
248
    /// <list type="bullet">
249
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
250
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
251
    /// </list>
252
    /// </remarks>
253
    [Visual]
254
    public static void ApplyNegative(bool negative = false)
255
    {
256
        Console.ForegroundColor = negative ? s_colorPanel.Item2 : s_colorPanel.Item1;
257
        Console.BackgroundColor = negative ? s_colorPanel.Item1 : s_colorPanel.Item2;
258
    }
259

260
    #endregion
261

262
    #region Middle abstraction level methods
263
    /// <summary>
264
    /// This method is used to write a string positioned in the console.
265
    /// </summary>
266
    /// <param name="str">The string to write.</param>
267
    /// <param name="align">The position of the string in the console.</param>
268
    /// <param name="negative">If true, the text is highlighted.</param>
269
    /// <param name="line">The line where the string is written in the console. If null, will be written where the cursor is.</param>
270
    /// <param name="writeLine">If true, the string is written with a line break.</param>
271
    /// <remarks>
272
    /// For more information, refer to the following resources:
273
    /// <list type="bullet">
274
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
275
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
276
    /// </list>
277
    /// </remarks>
278
    [Visual]
279
    public static void WritePositionedString(
280
        string str,
281
        TextAlignment align = TextAlignment.Center,
282
        bool negative = false,
283
        int? line = null,
284
        bool writeLine = false
285
    )
286
    {
287
        ApplyNegative(negative);
288
        var negativeRng = str.GetRangeAndRemoveNegativeAnchors();
289
        str = negativeRng.Item1;
290
        line ??= Console.CursorTop;
291
        if (str.Length < Console.WindowWidth)
292
            switch (align)
293
            {
294
                case TextAlignment.Left:
295
                    Console.SetCursorPosition(0, (int)line);
296
                    break;
297
                case TextAlignment.Center:
298
                    Console.SetCursorPosition((Console.WindowWidth - str.Length) / 2, (int)line);
299
                    break;
300
                case TextAlignment.Right:
301
                    Console.SetCursorPosition(Console.WindowWidth - str.Length, (int)line);
302
                    break;
303
            }
304
        else
305
            Console.SetCursorPosition(0, (int)line);
306
        if (writeLine)
307
        {
308
            if (negativeRng.Item2 is not null)
309
            {
310
                Console.Write(str[..negativeRng.Item2.Value.Item1]);
311
                ApplyNegative(true);
312
                Console.Write(str[negativeRng.Item2.Value.Item2..]);
313
                ApplyNegative(default);
314
                Console.WriteLine();
315
            }
316
            else
317
            {
318
                Console.WriteLine(str);
319
            }
320
        }
321
        else
322
        {
323
            if (negativeRng.Item2 is not null)
324
            {
325
                Console.Write(negativeRng.Item1[..negativeRng.Item2.Value.Item1]);
326
                ApplyNegative(true);
327
                Console.Write(
328
                    negativeRng.Item1[negativeRng.Item2.Value.Item1..negativeRng.Item2.Value.Item2]
329
                );
330
                ApplyNegative(default);
331
                Console.Write(negativeRng.Item1[negativeRng.Item2.Value.Item2..]);
332
            }
333
            else
334
            {
335
                Console.Write(str);
336
            }
337
        }
338
        ApplyNegative(default);
339
    }
340

341
    /// <summary>
342
    /// This method is used to write a string continuously in the console.
343
    /// The string is written letter by letter on the console.
344
    /// </summary>
345
    /// <param name="str">The string to write.</param>
346
    /// <param name="line">The line where the string is written in the console. If null, will be written where the cursor is.</param>
347
    /// <param name="negative">If true, the text is highlighted.</param>
348
    /// <param name="printTime">The total time to write the string in ms.</param>
349
    /// <param name="additionalTime">The additional time to wait after the string is written in ms.</param>
350
    /// <param name="length">The length of the string. If null, the length is the window width.</param>
351
    /// <param name="align">The alignment of the string.</param>
352
    /// <param name="writeLine">If true, the string is written with a line break.</param>
353
    /// <remarks>
354
    /// For more information, refer to the following resources:
355
    /// <list type="bullet">
356
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
357
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
358
    /// </list>
359
    /// </remarks>
360
    [Visual]
361
    public static void WriteContinuousString(
362
        string str,
363
        int? line,
364
        bool negative = false,
365
        int printTime = 2000,
366
        int additionalTime = 1000,
367
        int length = -1,
368
        TextAlignment align = TextAlignment.Center,
369
        bool writeLine = false
370
    )
371
    {
372
        line ??= Console.CursorTop;
373
        length = length == -1 ? Console.WindowWidth : length;
374
        int timeInterval = printTime / str.Length;
375
        for (int i = 0; i <= str.Length; i++)
376
        {
377
            StringBuilder continuous = new StringBuilder();
378
            for (int j = 0; j < i; j++)
379
                continuous.Append(str[j]);
380
            string continuousStr = continuous.ToString().PadRight(str.Length);
381
            WritePositionedString(
382
                continuousStr.ResizeString(length, align, default),
383
                align,
384
                negative,
385
                line,
386
                writeLine
387
            );
388
            Thread.Sleep(timeInterval);
389

390
            if (Console.KeyAvailable)
391
            {
392
                ConsoleKeyInfo keyPressed = Console.ReadKey(true);
393
                if (keyPressed.Key == ConsoleKey.Enter || keyPressed.Key == ConsoleKey.Escape)
394
                {
395
                    break;
396
                }
397
            }
398
        }
399
        WritePositionedString(str.ResizeString(length, align, default), align, negative, line);
400
        Thread.Sleep(additionalTime);
401
    }
402

403
    /// <summary>
404
    /// This method is used to write a styled string in the console.
405
    /// </summary>
406
    /// <param name="text">The styled string to write.</param>
407
    /// <param name="line">The line where the string is written in the console. If null, will be written from the ContentHeight.</param>
408
    /// <param name="width">The width of the string. If null, the width is the window width.</param>
409
    /// <param name="margin">The upper and lower margin.</param>
410
    /// <param name="align">The alignment of the string.</param>
411
    /// <param name="negative">If true, the text is highlighted.</param>
412
    /// <remarks>
413
    /// For more information, refer to the following resources:
414
    /// <list type="bullet">
415
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
416
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
417
    /// </list>
418
    /// </remarks>
419
    [Visual]
420
    public static void WritePositionedStyledText(
421
        string[]? text = null,
422
        int? line = null,
423
        int? width = null,
424
        int? margin = null,
425
        TextAlignment align = TextAlignment.Center,
426
        bool negative = false
427
    )
428
    {
429
        line ??= Console.CursorTop;
430
        margin ??= 0;
431
        if (text is not null)
432
        {
433
            Console.SetCursorPosition(0, line ?? Window.GetLineAvailable(align.ToPlacement()));
434

435
            for (int i = 0; i < margin; i++)
436
                WritePositionedString(
437
                    "".ResizeString(width ?? Console.WindowWidth, align),
438
                    align,
439
                    negative,
440
                    (line ?? Window.GetLineAvailable(align.ToPlacement())) + i,
441
                    true
442
                );
443
            for (int i = 0; i < text.Length; i++)
444
                WritePositionedString(
445
                    text[i].ResizeString(width ?? Console.WindowWidth, align),
446
                    align,
447
                    negative,
448
                    (line ?? Window.GetLineAvailable(align.ToPlacement())) + margin + i,
449
                    true
450
                );
451
            for (int i = 0; i < margin; i++)
452
                WritePositionedString(
453
                    "".ResizeString(width ?? Console.WindowWidth, align),
454
                    align,
455
                    negative,
456
                    (line ?? Window.GetLineAvailable(align.ToPlacement()))
457
                        + margin
458
                        + text.Length
459
                        + i,
460
                    true
461
                );
462
        }
463
    }
464

465
    /// <summary>
466
    /// This method prints a paragraph in the console.
467
    /// </summary>
468
    /// <param name="equalizeLengths">Whether or not the lines of the paragraph should be equalized to the same length.</param>
469
    /// <param name="align">The alignment of the paragraph.</param>
470
    /// <param name="negative">If true, the paragraph is printed in the negative colors.</param>
471
    /// <param name="line">The height of the paragraph.</param>
472
    /// <param name="text">The lines of the paragraph.</param>
473
    /// <remarks>
474
    /// For more information, refer to the following resources:
475
    /// <list type="bullet">
476
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
477
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
478
    /// </list>
479
    /// </remarks>
480
    [Visual]
481
    public static void WriteMultiplePositionedLines(
482
        bool equalizeLengths = true,
483
        TextAlignment align = TextAlignment.Center,
484
        bool negative = false,
485
        int? line = null,
486
        params string[] text
487
    )
488
    {
489
        line ??= Console.CursorTop;
490
        if (equalizeLengths)
491
        {
492
            int maxLength = text.Length > 0 ? text.Max(s => s.Length) : 0;
×
493
            foreach (string str in text)
494
            {
495
                WritePositionedString(str.ResizeString(maxLength, align), align, negative, line++);
496
                if (line >= Console.WindowHeight - 1)
497
                    break;
498
            }
499
        }
500
        else
501
        {
502
            foreach (string str in text)
503
            {
504
                WritePositionedString(str, align, negative, line++);
505
                if (line >= Console.WindowHeight - 1)
506
                    break;
507
            }
508
        }
509
    }
510

511
    #endregion
512

513
    #region Extensions
514
    /// <summary>
515
    /// This method builds a string with a specific size and a specific placement.
516
    /// </summary>
517
    /// <param name="str">The string to build.</param>
518
    /// <param name="size">The size of the string.</param>
519
    /// <param name="align">The alignment of the string.</param>
520
    /// <param name="truncate">If true, the string is truncated if it is too long.</param>
521
    /// <returns>The built string.</returns>
522
    /// <remarks>
523
    /// For more information, refer to the following resources:
524
    /// <list type="bullet">
525
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
526
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
527
    /// </list>
528
    /// </remarks>
529
    public static string ResizeString(
530
        this string str,
531
        int size,
532
        TextAlignment align = TextAlignment.Center,
533
        bool truncate = true
534
    )
535
    {
536
        int padding = size - str.Length;
297✔
537
        if (truncate && padding < 0)
297✔
538
            switch (align)
539
            {
540
                case TextAlignment.Left:
541
                    return str.Substring(0, size);
3✔
542
                case TextAlignment.Center:
543
                    return str.Substring((-padding) / 2, size);
3✔
544
                case TextAlignment.Right:
545
                    return str.Substring(-padding, size);
3✔
546
            }
547
        else if (padding > 0)
288✔
548
            switch (align)
549
            {
550
                case TextAlignment.Left:
551
                    return str.PadRight(size);
3✔
552
                case TextAlignment.Center:
553
                    return str.PadLeft(padding / 2 + padding % 2 + str.Length)
273✔
554
                        .PadRight(padding + str.Length);
273✔
555
                case TextAlignment.Right:
556
                    return str.PadLeft(size);
3✔
557
            }
558
        return str;
9✔
559
    }
560

561
    /// <summary>
562
    /// Insert a specified string into another string, at a specified position.
563
    /// </summary>
564
    /// <param name="inserted">The string that receives the other.</param>
565
    /// <param name="toInsert">The string to insert.</param>
566
    /// <param name="position">The placement of the string to insert.</param>
567
    /// <returns>The final string after computing.</returns>
568
    /// <remarks>
569
    /// For more information, refer to the following resources:
570
    /// <list type="bullet">
571
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
572
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
573
    /// </list>
574
    /// </remarks>
575
    public static string InsertString(
576
        this string inserted,
577
        string toInsert,
578
        Placement position = Placement.TopCenter
579
    )
580
    {
581
        if (inserted.Length < toInsert.Length)
27✔
582
        {
583
            throw new ArgumentException(
6✔
584
                "The string to insert is longer than the string to insert into"
6✔
585
            );
6✔
586
        }
587
        switch (position)
588
        {
589
            case Placement.TopCenter:
590
            case Placement.BottomCenterFullWidth:
591
            case Placement.TopCenterFullWidth:
592
                int center = inserted.Length / 2;
6✔
593
                int start = center - (toInsert.Length / 2);
6✔
594
                return inserted.Remove(start, toInsert.Length).Insert(start, toInsert);
6✔
595
            case Placement.TopLeft:
596
                return inserted.Remove(0, toInsert.Length).Insert(0, toInsert);
6✔
597
            case Placement.TopRight:
598
                return inserted
6✔
599
                    .Remove(inserted.Length - toInsert.Length, toInsert.Length)
6✔
600
                    .Insert(inserted.Length - toInsert.Length, toInsert);
6✔
601
            default:
602
                throw new ArgumentException("The placement is not valid");
3✔
603
        }
604
    }
605

606
    /// <summary>
607
    /// This method is used to convert the banner tuple into a string.
608
    /// </summary>
609
    /// <param name="banner">The banner tuple.</param>
610
    /// <returns>Converts the banner to a string.</returns>
611
    /// <remarks>
612
    /// For more information, refer to the following resources:
613
    /// <list type="bullet">
614
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
615
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
616
    /// </list>
617
    /// </remarks>
618
    [Visual]
619
    public static string BannerToString(this (string, string, string) banner)
620
    {
621
        string centeredText = banner.Item2.ResizeString(
622
            Console.WindowWidth - 2,
623
            TextAlignment.Center,
624
            true
625
        );
626

627
        string leftAndCenter = banner.Item1 + centeredText.Substring(banner.Item1.Length);
628

629
        string fullBanner =
630
            leftAndCenter.Substring(0, leftAndCenter.Length - banner.Item3.Length) + banner.Item3;
631

632
        return " " + fullBanner + " ";
633
    }
634

635
    /// <summary>
636
    /// This method is used to get the range of a negative sequence in a string and remove the negative anchors.
637
    /// </summary>
638
    /// <param name="str">The string to check.</param>
639
    /// <returns>The string without the negative anchors and the range of the negative sequence.</returns>
640
    /// <remarks>
641
    /// For more information, refer to the following resources:
642
    /// <list type="bullet">
643
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
644
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
645
    /// </list>
646
    /// </remarks>
647
    public static (string, (int, int)?) GetRangeAndRemoveNegativeAnchors(this string str)
648
    {
649
        int negStart = str.IndexOf(NEGATIVE_ANCHOR);
9✔
650
        int negEnd = str.IndexOf(NEGATIVE_ANCHOR, negStart + 1);
9✔
651
        if (negStart == -1 || negEnd == -1)
9✔
652
            return (str, null);
3✔
653

654
        negEnd -= NEGATIVE_ANCHOR.Length;
6✔
655
        string newStr = str.Replace(NEGATIVE_ANCHOR, "");
6✔
656
        return (newStr, (negStart, negEnd));
6✔
657
    }
658

659
    /// <summary>
660
    /// This method is used to convert a Placement into a TextAlignment.
661
    /// </summary>
662
    /// <param name="placement">The placement to convert.</param>
663
    /// <returns>The converted placement.</returns>
664
    /// <exception cref="ArgumentException">Thrown when the placement is not valid.</exception>
665
    /// <remarks>
666
    /// For more information, refer to the following resources:
667
    /// <list type="bullet">
668
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
669
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
670
    /// </list>
671
    /// </remarks>
672
    public static TextAlignment ToTextAlignment(this Placement placement)
673
    {
674
        return placement switch
18✔
675
        {
18✔
676
            Placement.TopCenter => TextAlignment.Center,
3✔
677
            Placement.TopLeft => TextAlignment.Left,
3✔
678
            Placement.TopRight => TextAlignment.Right,
3✔
679
            Placement.BottomCenterFullWidth => TextAlignment.Center,
3✔
680
            Placement.TopCenterFullWidth => TextAlignment.Center,
3✔
681
            _ => throw new ArgumentException("The placement is not valid"),
3✔
682
        };
18✔
683
    }
684

685
    /// <summary>
686
    /// This method is used to convert a TextAlignment into a Placement.
687
    /// </summary>
688
    /// <param name="align">The alignment to convert.</param>
689
    /// <returns>The converted alignment.</returns>
690
    /// <exception cref="ArgumentException">Thrown when the alignment is not valid.</exception>
691
    /// <remarks>
692
    /// For more information, refer to the following resources:
693
    /// <list type="bullet">
694
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
695
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
696
    /// </list>
697
    /// </remarks>
698
    public static Placement ToPlacement(this TextAlignment align)
699
    {
700
        return align switch
12✔
701
        {
12✔
702
            TextAlignment.Center => Placement.TopCenter,
3✔
703
            TextAlignment.Left => Placement.TopLeft,
3✔
704
            TextAlignment.Right => Placement.TopRight,
3✔
705
            _ => throw new ArgumentException("The alignment is not valid"),
3✔
706
        };
12✔
707
    }
708
    #endregion
709
}
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

© 2026 Coveralls, Inc