Skip to content

Commit

Permalink
using external nuget for printing tables.
Browse files Browse the repository at this point in the history
  • Loading branch information
dasatomic committed Nov 23, 2021
1 parent 3fd9f54 commit 7d52094
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 75 deletions.
95 changes: 20 additions & 75 deletions bgdbRepl/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using bgdbRepl;
using CommandLine;
using ConsoleTables;
using DataStructures;
using ImageProcessing;
using PageManager;
Expand Down Expand Up @@ -29,26 +30,6 @@ public class Options
public bool UseListFormat { get; set; }
}

static int GetColumnWidth(ColumnInfo ci)
{
if (ci.ColumnType == ColumnType.Double)
{
return 10;
}
else if (ci.ColumnType == ColumnType.Int)
{
return 10;
}
else if (ci.ColumnType == ColumnType.String)
{
return ci.RepCount + 2;
}
else
{
throw new ArgumentException();
}
}

static string GetValAsString(ColumnInfo ci, RowHolder row, int columnPosition)
{
string strVal = null;
Expand All @@ -70,15 +51,6 @@ static string GetValAsString(ColumnInfo ci, RowHolder row, int columnPosition)
return strVal;
}

static string GetColumnString(ColumnInfo ci, int tableWidth, RowHolder row, int columnPosition)
{
string strVal = GetValAsString(ci, row, columnPosition);

int missingWhiteSpace = Math.Max(tableWidth - strVal.Length, 0);
char[] ws =Enumerable.Repeat(' ', missingWhiteSpace).ToArray();
return (new string(ws)) + strVal;
}

static async Task PrintResultsFormatList(RowProvider rowProvider)
{
Console.WriteLine("---------------------");
Expand All @@ -99,58 +71,31 @@ await foreach (var row in rowProvider.Enumerator)

static async Task PrintResultsFormatTable(RowProvider rowProvider)
{
int totalWidth = 0;
foreach (var ci in rowProvider.ColumnInfo)
{
int width = GetColumnWidth(ci.ColumnType);

int whitespaceCount = width - ci.ColumnName.Length;
totalWidth += width + whitespaceCount + 2;
Console.Write("|");
var table = new ConsoleTable(rowProvider.ColumnInfo.Select(ci => ci.ColumnName).ToArray());

for (int i = 0; i < whitespaceCount; i++)
{
Console.Write(" ");
}

Console.Write(ci.ColumnName);
Console.Write(" ");
}

Console.Write("|");

Console.WriteLine();
Console.WriteLine(new string(Enumerable.Repeat('-', totalWidth).ToArray()));
int totalCount = 0;
await foreach (var row in rowProvider.Enumerator)
{
totalCount++;
List<string> tableRow = new List<string>();

int totalCount = 0;
await foreach (var row in rowProvider.Enumerator)
for (int i = 0; i < rowProvider.ColumnInfo.Length; i++)
{
totalCount++;

int columnPos = 0;
for (int i = 0; i < rowProvider.ColumnInfo.Length; i++)
{
ColumnInfo columnInfo = rowProvider.ColumnInfo[i].ColumnType;
int width = GetColumnWidth(columnInfo);
string valToPrint = GetColumnString(columnInfo, width, row, columnPos);

Console.Write("|");
Console.Write(valToPrint);
Console.Write(" ");

columnPos++;
}

Console.Write("|");

Console.WriteLine();
Console.WriteLine(new string(Enumerable.Repeat('-', totalWidth).ToArray()));
ColumnInfo columnInfo = rowProvider.ColumnInfo[i].ColumnType;
string val = GetValAsString(columnInfo, row, i);
tableRow.Add(val);
}

Console.WriteLine();
Console.WriteLine(new string(Enumerable.Repeat('-', totalWidth).ToArray()));
table.AddRow(tableRow.ToArray());
}

if (table.Rows.Count > 0)
{
table.Write();
}

Console.WriteLine($"Total rows returned {totalCount}");
Console.WriteLine("----------------------");
Console.WriteLine($"Total rows returned {totalCount}");
}

private static string GetTempFolderPath()
Expand Down
1 change: 1 addition & 0 deletions bgdbRepl/bgdbRepl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.8.0" />
<PackageReference Include="ConsoleTables" Version="2.4.2" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 7d52094

Please sign in to comment.