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

MorganKryze / ConsoleAppVisuals / 8158191062

05 Mar 2024 02:47PM UTC coverage: 86.165% (+0.9%) from 85.231%
8158191062

push

github

MorganKryze
📖 (readme) update roadmap

931 of 1144 branches covered (81.38%)

Branch coverage included in aggregate %.

1803 of 2029 relevant lines covered (88.86%)

412.64 hits per line

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

100.0
/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 = ('â–¶', 'â—€');
2✔
21

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

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

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

43
    private static readonly Random s_rnd = new();
2✔
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;
10✔
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;
16✔
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;
2✔
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);
2✔
104
    }
2✔
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
    public static void SetForegroundColor(ConsoleColor color)
118
    {
119
        s_colorPanel.Item1 = color;
8✔
120
        Console.ForegroundColor = color;
8✔
121
    }
8✔
122

123
    /// <summary>
124
    /// This method changes the background color of the console.
125
    /// </summary>
126
    /// <param name="color">The new background color.</param>
127
    /// <remarks>
128
    /// For more information, refer to the following resources:
129
    /// <list type="bullet">
130
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
131
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
132
    /// </list>
133
    /// </remarks>
134
    public static void SetBackgroundColor(ConsoleColor color)
135
    {
136
        s_colorPanel.Item2 = color;
6✔
137
        Console.BackgroundColor = color;
6✔
138
    }
6✔
139

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

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

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

176
    /// <summary>
177
    /// 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.
178
    /// </summary>
179
    /// <remarks>
180
    /// For more information, refer to the following resources:
181
    /// <list type="bullet">
182
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
183
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
184
    /// </list>
185
    /// </remarks>
186
    [Visual]
187
    public static void SetConsoleDimensions()
188
    {
189
        s_previousWindowWidth = Console.WindowWidth;
190
        s_previousWindowHeight = Console.WindowHeight;
191
    }
192

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

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

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

253
    #endregion
254

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

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

383
            if (Console.KeyAvailable)
384
            {
385
                ConsoleKeyInfo keyPressed = Console.ReadKey(true);
386
                if (keyPressed.Key == ConsoleKey.Enter || keyPressed.Key == ConsoleKey.Escape)
387
                {
388
                    break;
389
                }
390
            }
391
        }
392
        WritePositionedString(str.ResizeString(length, align, default), align, negative, line);
393
        Thread.Sleep(additionalTime);
394
    }
395

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

428
            for (int i = 0; i < margin; i++)
429
                WritePositionedString(
430
                    "".ResizeString(width ?? Console.WindowWidth, align),
431
                    align,
432
                    negative,
433
                    (line ?? Window.GetLineAvailable(align.ToPlacement())) + i,
434
                    true
435
                );
436
            for (int i = 0; i < text.Length; i++)
437
                WritePositionedString(
438
                    text[i].ResizeString(width ?? Console.WindowWidth, align),
439
                    align,
440
                    negative,
441
                    (line ?? Window.GetLineAvailable(align.ToPlacement())) + margin + i,
442
                    true
443
                );
444
            for (int i = 0; i < margin; i++)
445
                WritePositionedString(
446
                    "".ResizeString(width ?? Console.WindowWidth, align),
447
                    align,
448
                    negative,
449
                    (line ?? Window.GetLineAvailable(align.ToPlacement()))
450
                        + margin
451
                        + text.Length
452
                        + i,
453
                    true
454
                );
455
        }
456
    }
457

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

504
    #endregion
505

506
    #region Extensions
507
    /// <summary>
508
    /// This method builds a string with a specific size and a specific placement.
509
    /// </summary>
510
    /// <param name="str">The string to build.</param>
511
    /// <param name="size">The size of the string.</param>
512
    /// <param name="align">The alignment of the string.</param>
513
    /// <param name="truncate">If true, the string is truncated if it is too long.</param>
514
    /// <returns>The built string.</returns>
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 string ResizeString(
523
        this string str,
524
        int size,
525
        TextAlignment align = TextAlignment.Center,
526
        bool truncate = true
527
    )
528
    {
529
        int padding = size - str.Length;
164✔
530
        if (truncate && padding < 0)
164✔
531
            switch (align)
532
            {
533
                case TextAlignment.Left:
534
                    return str.Substring(0, size);
2✔
535
                case TextAlignment.Center:
536
                    return str.Substring((-padding) / 2, size);
2✔
537
                case TextAlignment.Right:
538
                    return str.Substring(-padding, size);
2✔
539
            }
540
        else if (padding > 0)
158✔
541
            switch (align)
542
            {
543
                case TextAlignment.Left:
544
                    return str.PadRight(size);
2✔
545
                case TextAlignment.Center:
546
                    return str.PadLeft(padding / 2 + padding % 2 + str.Length)
146✔
547
                        .PadRight(padding + str.Length);
146✔
548
                case TextAlignment.Right:
549
                    return str.PadLeft(size);
2✔
550
            }
551
        return str;
8✔
552
    }
553

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

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

620
        string leftAndCenter = banner.Item1 + centeredText.Substring(banner.Item1.Length);
621

622
        string fullBanner =
623
            leftAndCenter.Substring(0, leftAndCenter.Length - banner.Item3.Length) + banner.Item3;
624

625
        return " " + fullBanner + " ";
626
    }
627

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

647
        negEnd -= NEGATIVE_ANCHOR.Length;
4✔
648
        string newStr = str.Replace(NEGATIVE_ANCHOR, "");
4✔
649
        return (newStr, (negStart, negEnd));
4✔
650
    }
651

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

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