Skip to content

Table rewrite #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CommonsLibrary/CommonsLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Title>C# Commons Library</Title>
<Version>7.1.0</Version>
<Version>8.0.0</Version>
<Authors>Alex O'Brien</Authors>
<PackageProjectUrl>https://github.com/alex8obrien/CommonsLibrary</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
112 changes: 74 additions & 38 deletions CommonsLibrary/StdOut.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text;

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

/// <summary>Used to build and display data</summary>
/// <summary>Used to generate an ASCII table</summary>
/// <param name="headers">An array of the headers</param>
/// <param name="data">A 2D array of the data you want displaying</param>
public static void PrintTable(string[] headers, string[,] data)
public static string GenerateTable(string[] headers, string[,] data)
{
int[] maxColumnWidth = new int[headers.Length];
int[] maxColumnWidth = GetMaxColumnWidth(headers, data);

// Find max column width
for (int i = 0; i < headers.Length; i++)
{
maxColumnWidth[i] = headers[i].Length;
}
StringBuilder builder = new StringBuilder();
builder.Append(AddNewLine(headers, maxColumnWidth));
builder.Append(CreateRule(maxColumnWidth));

for (int i = 0; i < data.GetLength(0); i++)
{

string[] row = new string[data.GetLength(1)];

for (int j = 0; j < data.GetLength(1); j++)
{
if (data[i, j].Length > maxColumnWidth[j])
{
maxColumnWidth[j] = data[i, j].Length;
}
row[j] = data[i, j];
}

builder.Append(AddNewLine(row, maxColumnWidth));
}

foreach (string header in headers)
return builder.ToString();
}

/// <summary>
/// Method used to create the lines for the table
/// </summary>
/// <param name="maxWidths">Max width of each column</param>
/// <returns>A StringBuilder that contains the line to separate the headers and data</returns>
private static StringBuilder CreateRule(IEnumerable<int> maxWidths)
{
StringBuilder builder = new StringBuilder();

foreach (int maxWidth in maxWidths)
{
Console.Write($"| {header} ");
int diff = maxColumnWidth[Array.IndexOf(headers, header)] - header.Length;
for (int i = 0; i < diff; i++)
builder.Append("|-");
for (int i = 0; i < maxWidth; i++)
{
Console.Write(" ");
builder.Append('-');
}
builder.Append('-');
}

Console.WriteLine("|");
builder.AppendLine("|");
return builder;
}

/// <summary>
/// Gets the maximum column width for the headers and data
/// </summary>
/// <param name="headers">Table headers</param>
/// <param name="data">Table data</param>
/// <returns>Int array of the maximum width</returns>
private static int[] GetMaxColumnWidth(IReadOnlyList<string> headers, string[,] data)
{
int[] maxWidth = new int[headers.Count];

// Initialize maxWidth with header widths directly
for (int i = 0; i < maxWidth.Length; i++)
maxWidth[i] = headers[i].Length;

for (int i = 0; i < headers.Length; i++)
// Loop through data and find a new max width
for (int i = 0; i < data.GetLength(0); i++)
{
Console.Write("|-");
for (int j = 0; j < maxColumnWidth[i]; j++)
for (int j = 0; j < data.GetLength(1); j++)
{
Console.Write("-");
maxWidth[j] = Math.Max(maxWidth[j], data[i, j].Length);
}

Console.Write("-");
}

Console.WriteLine("|");
return maxWidth;
}

for (int i = 0; i < data.GetLength(0); i++)
/// <summary>
/// Creates the row as a StringBuilder instance
/// </summary>
/// <param name="row">Row of data to use in the table</param>
/// <param name="maxWidth">Maximum width of each column in the table</param>
/// <returns>StringBuilder instance of the row in the table</returns>
private static StringBuilder AddNewLine(IReadOnlyList<string> row, IReadOnlyList<int> maxWidth)
{
StringBuilder builder = new StringBuilder();

for (int i = 0; i < row.Count; i++)
{
Console.Write("|");
for (int j = 0; j < data.GetLength(1); j++)
{
Console.Write($" {data[i, j]}");
builder.Append($"| {row[i]} ");

int diff = maxColumnWidth[j] - data[i, j].Length;
for (int k = 0; k < diff; k++)
{
Console.Write(" ");
}
int difference = maxWidth[i] - row[i].Length;

Console.Write(" |");
for (int j = 0; j < difference; j++)
{
builder.Append(' ');
}

Console.WriteLine("");
}

builder.AppendLine("|");

return builder;
}
}
}