Skip to content

Commit 1289808

Browse files
authored
Table rewrite (#7)
* Increment version number * Added Create rule method * Added get max column width method * Added AddNewLine method * Replaced PrintTable with GenerateTable, new version num
1 parent 36b08b0 commit 1289808

File tree

2 files changed

+75
-39
lines changed

2 files changed

+75
-39
lines changed

CommonsLibrary/CommonsLibrary.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<ImplicitUsings>disable</ImplicitUsings>
66
<Title>C# Commons Library</Title>
7-
<Version>7.1.0</Version>
7+
<Version>8.0.0</Version>
88
<Authors>Alex O'Brien</Authors>
99
<PackageProjectUrl>https://github.com/alex8obrien/CommonsLibrary</PackageProjectUrl>
1010
<PackageReadmeFile>README.md</PackageReadmeFile>

CommonsLibrary/StdOut.cs

+74-38
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
24

35
namespace CommonsLibrary
46
{
@@ -22,73 +24,107 @@ public static void WriteColourMessage(string msg, ConsoleColor colour)
2224
public static void WriteLineColourMessage(string msg, ConsoleColor colour) =>
2325
WriteColourMessage($"{msg}\n", colour);
2426

25-
/// <summary>Used to build and display data</summary>
27+
/// <summary>Used to generate an ASCII table</summary>
2628
/// <param name="headers">An array of the headers</param>
2729
/// <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)
2931
{
30-
int[] maxColumnWidth = new int[headers.Length];
32+
int[] maxColumnWidth = GetMaxColumnWidth(headers, data);
3133

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));
3737

3838
for (int i = 0; i < data.GetLength(0); i++)
3939
{
40+
41+
string[] row = new string[data.GetLength(1)];
42+
4043
for (int j = 0; j < data.GetLength(1); j++)
4144
{
42-
if (data[i, j].Length > maxColumnWidth[j])
43-
{
44-
maxColumnWidth[j] = data[i, j].Length;
45-
}
45+
row[j] = data[i, j];
4646
}
47+
48+
builder.Append(AddNewLine(row, maxColumnWidth));
4749
}
4850

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)
5064
{
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++)
5467
{
55-
Console.Write(" ");
68+
builder.Append('-');
5669
}
70+
builder.Append('-');
5771
}
5872

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;
6090

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++)
6293
{
63-
Console.Write("|-");
64-
for (int j = 0; j < maxColumnWidth[i]; j++)
94+
for (int j = 0; j < data.GetLength(1); j++)
6595
{
66-
Console.Write("-");
96+
maxWidth[j] = Math.Max(maxWidth[j], data[i, j].Length);
6797
}
68-
69-
Console.Write("-");
7098
}
7199

72-
Console.WriteLine("|");
100+
return maxWidth;
101+
}
73102

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++)
75114
{
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]} ");
80116

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;
86118

87-
Console.Write(" |");
119+
for (int j = 0; j < difference; j++)
120+
{
121+
builder.Append(' ');
88122
}
89-
90-
Console.WriteLine("");
91123
}
124+
125+
builder.AppendLine("|");
126+
127+
return builder;
92128
}
93129
}
94130
}

0 commit comments

Comments
 (0)