@@ -9,13 +9,19 @@ public static void Alert(string message, string title, ConsoleMessageType messag
9
9
internal static class ConsoleAlert {
10
10
public static void Create ( string message , string title , ConsoleMessageType type = ConsoleMessageType . Info )
11
11
{
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
+
13
16
14
17
title ??= "" ;
15
18
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
+ }
19
25
Console . WriteLine ( BuildFooter ( type , maxLength ) , ( ConsoleColor ) type ) ;
20
26
}
21
27
@@ -34,6 +40,34 @@ private static string BuildFooter(ConsoleMessageType type, int maxLength)
34
40
{
35
41
return $ "└{ '─' . Repeat ( maxLength - 2 ) } ┘";
36
42
}
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
+
37
71
}
38
72
39
73
public enum ConsoleMessageType : int {
0 commit comments