in src/ResultEncoding/Table.cs [156:201]
public EncodedData? Encode(object displayable)
{
if (displayable is ITable table)
{
var headers = table.Headers;
var cells = table.Cells;
// For the text, we need to find how wide each column is.
var widths = headers.Select(column => 0).ToArray();
var nCols = widths.Length;
foreach (var row in cells)
{
foreach (var idx in Enumerable.Range(0, nCols))
{
if (row[idx].Length > widths[idx])
{
widths[idx] = row[idx].Length;
}
}
}
var text = new StringBuilder();
text.Append(String.Join(" ",
headers.Select((header, idxCol) =>
header.PadCell(widths[idxCol], TableCellAlignment)
)
).TrimEnd());
text.Append(Environment.NewLine);
text.Append(String.Join(" ",
widths.Select(width => new String('-', width))
).TrimEnd());
text.Append(Environment.NewLine);
foreach (var row in cells)
{
text.Append(String.Join(" ",
row.Select(
(cell, idxCol) => cell.PadCell(widths[idxCol], TableCellAlignment)
)
).TrimEnd());
text.Append(Environment.NewLine);
}
return text.ToString().ToEncodedData();
} else return null;
}