Skip to content

Commit

Permalink
#1 finish dictionary option, including html
Browse files Browse the repository at this point in the history
  • Loading branch information
JordiCorbilla committed Mar 30, 2021
1 parent 626713d commit a302789
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
44 changes: 44 additions & 0 deletions table.lib/TableDic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,50 @@ public void ToMarkDown(string fileName, bool consoleVerbose = false)
Console.WriteLine(stringBuilder.ToString());
}

public void ToHtml(string fileName)
{
var stringBuilder = new StringBuilder();
if (Items.Count == 0) return;
stringBuilder.AppendLine("<table style=\"border-collapse: collapse; width: 100%;\">");
stringBuilder.AppendLine("<tr>");

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

stringBuilder.AppendLine(
$"<th style=\"text-align: center; background-color: #052a3d; color: white;padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;\">{headerName.ToHtml()}</th>");
}

stringBuilder.AppendLine("</tr>");

var rowNumber = 1;
for (var index = 0; index < Items.Count; index++)
{
var row = Items[index];
stringBuilder.AppendLine("<tr>");
foreach (var property in filteredPropertyNames)
{
var color = rowNumber % 2 == 0 ? "#f2f2f2" : "white";
var value = "";
value = property.Name == Options.KeyName ? ObjectToString(Keys[index]) : GetValue(row, property);

stringBuilder.AppendLine($"<td style=\"text-align: right; color: black; background-color: {color};padding: 4px;border: 1px solid #dddddd; font-family:monospace; font-size: 14px;\">{value.ToHtml()}</td>");
}

rowNumber++;
stringBuilder.AppendLine("</tr>");
}

stringBuilder.AppendLine("</table>");

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

public static TableDic<TV, T> Add(Dictionary<TV, T> dictionary)
{
return new TableDic<TV, T>(dictionary);
Expand Down
1 change: 1 addition & 0 deletions table.runner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ private static void Main()
Samples.SimpleConsoleOutputWithHighlighterForDictionary();
Samples.ComplexMarkDownOutputFilteringForDictionary();
Samples.SimpleCsvOutputForDictionary();
Samples.SimpleHtmlOutputForDictionary();
}
}
}
5 changes: 5 additions & 0 deletions table.runner/Samples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,5 +329,10 @@ public static void ComplexMarkDownOutputFilteringForDictionary()
.ColumnContentTextJustification(new Dictionary<string, TextJustification>
{{"Field3", TextJustification.Right}}).ToMarkDown(@"C:\temp\testDic.md", true);
}

public static void SimpleHtmlOutputForDictionary()
{
TableDic<string, TestClass>.Add(GetSimpleDictionary()).ToHtml(@"C:\temp\test-list-dic.html");
}
}
}

0 comments on commit a302789

Please sign in to comment.