Skip to content
This repository has been archived by the owner on Jul 21, 2020. It is now read-only.

Commit

Permalink
consider header also when computing table column width
Browse files Browse the repository at this point in the history
  • Loading branch information
sriv committed Aug 12, 2016
1 parent 4e305bc commit 036544e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
10 changes: 5 additions & 5 deletions Lib.UnitTests/TableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ public void ShouldBeAbleToAccessRowValuesUsingColumnNames()
[Test]
public void ShouldGetTableAsMarkdownString()
{
var headers = new List<string> { "foo", "bar" };
var headers = new List<string> { "foo", "bar_with_big_header" };
var table = new Table(headers);
table.AddRow(new List<string> { "foo_val", "bar_val" });
table.AddRow(new List<string> { "foo_val1", "bar_val1" });

const string expected = "|foo |bar |\n" +
"|--------|--------|\n" +
"|foo_val |bar_val |\n" +
"|foo_val1|bar_val1|";
const string expected = "|foo |bar_with_big_header|\n" +
"|--------|-------------------|\n" +
"|foo_val |bar_val |\n" +
"|foo_val1|bar_val1 |";

Assert.AreEqual(expected, table.ToString());
}
Expand Down
4 changes: 2 additions & 2 deletions Lib/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ public override string ToString()
IEnumerable<string> columnStrings = new string[_rows.Count + 2];
foreach (var header in GetColumnNames())
{
var columnValues = GetColumnValues(header).ToArray();
var columnWidth = columnValues.Max(s => s.Length);
var columnValues = GetColumnValues(header).ToList();
var columnWidth = columnValues.Concat(new [] {header}).Max(s => s.Length);
Func<string, string> formatCellValue = s => string.Format("|{0}", s.PadRight(columnWidth, ' '));
var paddedColumn = new[] {header, new string('-', columnWidth)}.Concat(columnValues).Select(formatCellValue);
columnStrings = columnStrings.Zip(paddedColumn, string.Concat);
Expand Down

0 comments on commit 036544e

Please sign in to comment.