1
1
using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Text ;
2
4
3
5
namespace CommonsLibrary
4
6
{
@@ -22,73 +24,107 @@ public static void WriteColourMessage(string msg, ConsoleColor colour)
22
24
public static void WriteLineColourMessage ( string msg , ConsoleColor colour ) =>
23
25
WriteColourMessage ( $ "{ msg } \n ", colour ) ;
24
26
25
- /// <summary>Used to build and display data </summary>
27
+ /// <summary>Used to generate an ASCII table </summary>
26
28
/// <param name="headers">An array of the headers</param>
27
29
/// <param name="data">A 2D array of the data you want displaying</param>
28
- public static void PrintTable ( string [ ] headers , string [ , ] data )
30
+ public static string GenerateTable ( string [ ] headers , string [ , ] data )
29
31
{
30
- int [ ] maxColumnWidth = new int [ headers . Length ] ;
32
+ int [ ] maxColumnWidth = GetMaxColumnWidth ( headers , data ) ;
31
33
32
- // Find max column width
33
- for ( int i = 0 ; i < headers . Length ; i ++ )
34
- {
35
- maxColumnWidth [ i ] = headers [ i ] . Length ;
36
- }
34
+ StringBuilder builder = new StringBuilder ( ) ;
35
+ builder . Append ( AddNewLine ( headers , maxColumnWidth ) ) ;
36
+ builder . Append ( CreateRule ( maxColumnWidth ) ) ;
37
37
38
38
for ( int i = 0 ; i < data . GetLength ( 0 ) ; i ++ )
39
39
{
40
+
41
+ string [ ] row = new string [ data . GetLength ( 1 ) ] ;
42
+
40
43
for ( int j = 0 ; j < data . GetLength ( 1 ) ; j ++ )
41
44
{
42
- if ( data [ i , j ] . Length > maxColumnWidth [ j ] )
43
- {
44
- maxColumnWidth [ j ] = data [ i , j ] . Length ;
45
- }
45
+ row [ j ] = data [ i , j ] ;
46
46
}
47
+
48
+ builder . Append ( AddNewLine ( row , maxColumnWidth ) ) ;
47
49
}
48
50
49
- foreach ( string header in headers )
51
+ return builder . ToString ( ) ;
52
+ }
53
+
54
+ /// <summary>
55
+ /// Method used to create the lines for the table
56
+ /// </summary>
57
+ /// <param name="maxWidths">Max width of each column</param>
58
+ /// <returns>A StringBuilder that contains the line to separate the headers and data</returns>
59
+ private static StringBuilder CreateRule ( IEnumerable < int > maxWidths )
60
+ {
61
+ StringBuilder builder = new StringBuilder ( ) ;
62
+
63
+ foreach ( int maxWidth in maxWidths )
50
64
{
51
- Console . Write ( $ "| { header } ") ;
52
- int diff = maxColumnWidth [ Array . IndexOf ( headers , header ) ] - header . Length ;
53
- for ( int i = 0 ; i < diff ; i ++ )
65
+ builder . Append ( "|-" ) ;
66
+ for ( int i = 0 ; i < maxWidth ; i ++ )
54
67
{
55
- Console . Write ( " " ) ;
68
+ builder . Append ( '-' ) ;
56
69
}
70
+ builder . Append ( '-' ) ;
57
71
}
58
72
59
- Console . WriteLine ( "|" ) ;
73
+ builder . AppendLine ( "|" ) ;
74
+ return builder ;
75
+ }
76
+
77
+ /// <summary>
78
+ /// Gets the maximum column width for the headers and data
79
+ /// </summary>
80
+ /// <param name="headers">Table headers</param>
81
+ /// <param name="data">Table data</param>
82
+ /// <returns>Int array of the maximum width</returns>
83
+ private static int [ ] GetMaxColumnWidth ( IReadOnlyList < string > headers , string [ , ] data )
84
+ {
85
+ int [ ] maxWidth = new int [ headers . Count ] ;
86
+
87
+ // Initialize maxWidth with header widths directly
88
+ for ( int i = 0 ; i < maxWidth . Length ; i ++ )
89
+ maxWidth [ i ] = headers [ i ] . Length ;
60
90
61
- for ( int i = 0 ; i < headers . Length ; i ++ )
91
+ // Loop through data and find a new max width
92
+ for ( int i = 0 ; i < data . GetLength ( 0 ) ; i ++ )
62
93
{
63
- Console . Write ( "|-" ) ;
64
- for ( int j = 0 ; j < maxColumnWidth [ i ] ; j ++ )
94
+ for ( int j = 0 ; j < data . GetLength ( 1 ) ; j ++ )
65
95
{
66
- Console . Write ( "-" ) ;
96
+ maxWidth [ j ] = Math . Max ( maxWidth [ j ] , data [ i , j ] . Length ) ;
67
97
}
68
-
69
- Console . Write ( "-" ) ;
70
98
}
71
99
72
- Console . WriteLine ( "|" ) ;
100
+ return maxWidth ;
101
+ }
73
102
74
- for ( int i = 0 ; i < data . GetLength ( 0 ) ; i ++ )
103
+ /// <summary>
104
+ /// Creates the row as a StringBuilder instance
105
+ /// </summary>
106
+ /// <param name="row">Row of data to use in the table</param>
107
+ /// <param name="maxWidth">Maximum width of each column in the table</param>
108
+ /// <returns>StringBuilder instance of the row in the table</returns>
109
+ private static StringBuilder AddNewLine ( IReadOnlyList < string > row , IReadOnlyList < int > maxWidth )
110
+ {
111
+ StringBuilder builder = new StringBuilder ( ) ;
112
+
113
+ for ( int i = 0 ; i < row . Count ; i ++ )
75
114
{
76
- Console . Write ( "|" ) ;
77
- for ( int j = 0 ; j < data . GetLength ( 1 ) ; j ++ )
78
- {
79
- Console . Write ( $ " { data [ i , j ] } ") ;
115
+ builder . Append ( $ "| { row [ i ] } ") ;
80
116
81
- int diff = maxColumnWidth [ j ] - data [ i , j ] . Length ;
82
- for ( int k = 0 ; k < diff ; k ++ )
83
- {
84
- Console . Write ( " " ) ;
85
- }
117
+ int difference = maxWidth [ i ] - row [ i ] . Length ;
86
118
87
- Console . Write ( " |" ) ;
119
+ for ( int j = 0 ; j < difference ; j ++ )
120
+ {
121
+ builder . Append ( ' ' ) ;
88
122
}
89
-
90
- Console . WriteLine ( "" ) ;
91
123
}
124
+
125
+ builder . AppendLine ( "|" ) ;
126
+
127
+ return builder ;
92
128
}
93
129
}
94
130
}
0 commit comments