Skip to content

Commit a84bdf6

Browse files
committed
🐛 fix console alert multiline issue
Alert have multiline issue #17
1 parent b8005f0 commit a84bdf6

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

src/ConsoleR/Alert/ConsoleAlert.cs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,19 @@ public static void Alert(string message, string title, ConsoleMessageType messag
99
internal static class ConsoleAlert {
1010
public static void Create(string message, string title, ConsoleMessageType type = ConsoleMessageType.Info)
1111
{
12-
var maxLength = message.Length + 4; // 4 for borders and spaces around
12+
var stringLength = message.Length + 4; // 4 for borders and spaces around
13+
var maxLength = Math.Min(stringLength, System.Console.WindowWidth); // Set a maximum length for each line
14+
var wrappedMessage = WrapText(message, maxLength - 4); // 4 for borders and spaces around
15+
1316

1417
title ??= "";
1518
Console.WriteLine(BuildHeader(title,type, maxLength), (ConsoleColor)type);
16-
Console.Write("│", (ConsoleColor)type);
17-
Console.Write($" {message} ");
18-
Console.Write("│\n", (ConsoleColor)type);
19+
foreach (var line in wrappedMessage)
20+
{
21+
Console.Write("│", (ConsoleColor)type);
22+
Console.Write($" {line.PadRight(maxLength - 4)} ");
23+
Console.Write("│\n", (ConsoleColor)type);
24+
}
1925
Console.WriteLine(BuildFooter(type, maxLength), (ConsoleColor)type);
2026
}
2127

@@ -34,6 +40,34 @@ private static string BuildFooter(ConsoleMessageType type, int maxLength)
3440
{
3541
return $"└{'─'.Repeat(maxLength - 2)}┘";
3642
}
43+
44+
private static List<string> WrapText(string text, int maxLength)
45+
{
46+
var words = text.Split(' ');
47+
var lines = new List<string>();
48+
var currentLine = "";
49+
50+
foreach (var word in words)
51+
{
52+
if ((currentLine + word).Length > maxLength)
53+
{
54+
lines.Add(currentLine);
55+
currentLine = word;
56+
}
57+
else
58+
{
59+
currentLine += (currentLine.Length > 0 ? " " : "") + word;
60+
}
61+
}
62+
63+
if (currentLine.Length > 0)
64+
{
65+
lines.Add(currentLine);
66+
}
67+
68+
return lines;
69+
}
70+
3771
}
3872

3973
public enum ConsoleMessageType: int {

src/ConsoleR/ConsoleR.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1414
</PropertyGroup>
1515
<PropertyGroup>
16-
<Version>0.2.1</Version>
16+
<Version>0.2.3</Version>
1717
<PackageId>Doroudi.ConsoleR</PackageId>
1818
<Authors>Saeid Doroudi</Authors>
1919
<Description>ConsoleR is set of utilities to make awesome console apps in .Net</Description>

tests/ConsoleR.TestApp/Program.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Console = ConsoleR.Console;
33

44

5-
65
string[] frontEndFrameworks = ["Blazor", "Angular", "Vue", "React", "VanillaJs"];
76
var selectedItem = Console.Menu("Please Select One beloved frontend framework", true, frontEndFrameworks).Select();
87
Console.AsciiArt(frontEndFrameworks[selectedItem], GetFrameworkColor(frontEndFrameworks[selectedItem]));

0 commit comments

Comments
 (0)