Skip to content

Commit

Permalink
#1 Adding markdown option
Browse files Browse the repository at this point in the history
  • Loading branch information
JordiCorbilla committed Mar 30, 2021
1 parent 6a7f1ef commit 626713d
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 3 deletions.
3 changes: 2 additions & 1 deletion table.lib/Base.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ public void ConsoleRender(string value, string column)
{
switch (Operation[column].Operation)
{
case HighlightOperation.Differences when value.Trim() != Operation[column].StringValue:
case HighlightOperation.Differences
when value.Trim() != Operation[column].StringValue:
Console.BackgroundColor = Operation[column].BackgroundColorIfDifferent;
Console.ForegroundColor = Operation[column].ForegroundColorIfDifferent;
break;
Expand Down
80 changes: 80 additions & 0 deletions table.lib/TableDic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,86 @@ public void ToCsv(string fileName)
file.WriteLine(stringBuilder.ToString());
}

public void ToMarkDown(string fileName, bool consoleVerbose = false)
{
if (Items.Count == 0) return;
var stringBuilder = new StringBuilder();
var s = "|";

var filteredPropertyNames = FilterProperties();

foreach (var property in filteredPropertyNames)
{
var headerName = property.Name;
if (ColumnNameOverrides.ContainsKey(property.Name))
headerName = ColumnNameOverrides[property.Name];

var length = MaxWidth[property.Name] - headerName.Length;
s += $" {headerName.ToValidOutput()}{new string(' ', length)} |";
}

stringBuilder.AppendLine(s);

s = "|";
foreach (var property in filteredPropertyNames)
{
var columnSeparator = $" {new string('-', MaxWidth[property.Name])} |";
if (ColumnTextJustification.ContainsKey(property.Name))
switch (ColumnTextJustification[property.Name])
{
case TextJustification.Centered:
columnSeparator = columnSeparator.Replace("- ", ": ");
columnSeparator = columnSeparator.Replace(" -", " :");
break;
case TextJustification.Right:
columnSeparator = columnSeparator.Replace("- ", ": ");
break;
case TextJustification.Left:
columnSeparator = columnSeparator.Replace(" -", " :");
break;
case TextJustification.Justified:
break;
default:
throw new ArgumentOutOfRangeException();
}

s += columnSeparator;
}

stringBuilder.AppendLine(s);

for (var i = 0; i < Items.Count; i++)
{
var row = Items[i];
s = "|";
foreach (var property in filteredPropertyNames)
{
if (property.Name == Options.KeyName)
{
var keyValueParsed = ObjectToString(Keys[i]);
var length = MaxWidth[property.Name] - keyValueParsed.Length;
s += $" {keyValueParsed.ToValidOutput()}{new string(' ', length)} |";
}
else
{
var value = GetValue(row, property);
var length = MaxWidth[property.Name] - value.Length;
s += $" {value.ToValidOutput()}{new string(' ', length)} |";
}
}

stringBuilder.AppendLine(s);
}

stringBuilder.AppendLine();

using var file = new StreamWriter(fileName);
file.WriteLine(stringBuilder.ToString());

if (consoleVerbose)
Console.WriteLine(stringBuilder.ToString());
}

public static TableDic<TV, T> Add(Dictionary<TV, T> dictionary)
{
return new TableDic<TV, T>(dictionary);
Expand Down
2 changes: 2 additions & 0 deletions table.runner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ private static void Main()

Samples.SimpleConsoleForDictionary();
Samples.SimpleConsoleOutputWithHighlighterForDictionary();
Samples.ComplexMarkDownOutputFilteringForDictionary();
Samples.SimpleCsvOutputForDictionary();
}
}
}
20 changes: 18 additions & 2 deletions table.runner/Samples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,29 @@ public static void SimpleConsoleOutputWithHighlighterForDictionary()
{
TableDic<string, TestClass>.Add(GetSimpleDictionary())
.HighlightValue(new HighlightOperator
{Field = "Field3", Type = HighlightType.Decimal, DecimalValue = 2121.32m, Operation = HighlightOperation.Equality})
{
Field = "Field3", Type = HighlightType.Decimal, DecimalValue = 2121.32m,
Operation = HighlightOperation.Equality
})
.HighlightValue(new HighlightOperator
{ Field = "Field6", Type = HighlightType.Decimal, DecimalValue = 34.43m, Operation = HighlightOperation.Equality })
{
Field = "Field6", Type = HighlightType.Decimal, DecimalValue = 34.43m,
Operation = HighlightOperation.Equality
})
.ToConsole();
}

public static void SimpleCsvOutputForDictionary()
{
TableDic<string, TestClass>.Add(GetSimpleDictionary())
.ToCsv(@"C:\temp\testDic.csv");
}

public static void ComplexMarkDownOutputFilteringForDictionary()
{
TableDic<string, TestClass>.Add(GetSimpleDictionary())
.ColumnContentTextJustification(new Dictionary<string, TextJustification>
{{"Field3", TextJustification.Right}}).ToMarkDown(@"C:\temp\testDic.md", true);
}
}
}

0 comments on commit 626713d

Please sign in to comment.