Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: corrected the incorrect format in ToMinimalString #74

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions ConsoleTables.Tests/ConsoleTableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,22 @@ public void TestDataTable()

}

[Fact]
public void TestMinimalFormating()
{
var table = new ConsoleTable("one", "two", "three")
.AddRow(1, 2, 3)
.AddRow("this line should be longer 哈哈哈哈", "yes it is", "oh")
.ToMinimalString();
Assert.Equal("""
one two three
------------------------------------------------------
1 2 3
this line should be longer 哈哈哈哈 yes it is oh

""", table);
}

class User
{
public string Name { get; set; }
Expand Down
12 changes: 8 additions & 4 deletions src/ConsoleTables/ConsoleTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ public override string ToString()


private void SetFormats(List<int> columnLengths, List<string> columnAlignment)
{
SetFormats(columnLengths, columnAlignment, "|");
}
private void SetFormats(List<int> columnLengths, List<string> columnAlignment, string delimiterStr)
{
var allLines = new List<object[]>();
allLines.Add(Columns.ToArray());
Expand All @@ -196,9 +200,9 @@ private void SetFormats(List<int> columnLengths, List<string> columnAlignment)
{
var value = d[i]?.ToString() ?? "";
var length = columnLengths[i] - (GetTextWidth(value) - value.Length);
return " | {" + i + "," + columnAlignment[i] + length + "}";
return " " + delimiterStr + " {" + i + "," + columnAlignment[i] + length + "}";
})
.Aggregate((s, a) => s + a) + " |";
.Aggregate((s, a) => s + a) + " " + delimiterStr;
}).ToList();
}

Expand Down Expand Up @@ -282,9 +286,9 @@ private string Format(List<int> columnLengths, char delimiter = '|')
.Select(GetNumberAlignment)
.ToList();

SetFormats(columnLengths, columnAlignment);

var delimiterStr = delimiter == char.MinValue ? string.Empty : delimiter.ToString();
SetFormats(columnLengths, columnAlignment, delimiterStr);

var format = (Enumerable.Range(0, Columns.Count)
.Select(i => " " + delimiterStr + " {" + i + "," + columnAlignment[i] + columnLengths[i] + "}")
.Aggregate((s, a) => s + a) + " " + delimiterStr).Trim();
Expand Down