khalidabuhakmeh / ConsoleTables

Print out a nicely formatted table in a console application C#
MIT License
944 stars 158 forks source link

Array support for AddRow #31

Closed KajdeMunter closed 1 year ago

KajdeMunter commented 5 years ago

Currently the AddRow function takes params object[] values:

public ConsoleTable AddRow(params object[] values)

And afterwards checks the length:

if (Columns.Count != values.Length)

But when we want to add a row from an array like so:

table.AddRow(new[] {1, 2, 3})

values.Length will return 1, which will result in the exception The number columns in the row (x) does not match the values (x).

It would be nice to be able to use an array like in my example so that the table can be filled more dynamically.

khalidabuhakmeh commented 5 years ago

There is an overload that takes an IEnumerable<object>. So you should be able to. I'm confused I guess. The issue you seem to be running into is that you haven't actually defined your columns.

https://github.com/khalidabuhakmeh/ConsoleTables/blob/8601de2a82298687d5c9f62986b5e499a17e8be3/src/ConsoleTables.Sample/Program.cs#L33-L37

KajdeMunter commented 5 years ago

Yes, but this would require creating a entire new object that fits the table markup, right?

For example I have a class with UserID and a Dictionary that contains an articleID and a Rating. When I add this to the table I get something like this:

 Count: 7
 --------------------------------------------------------------------------------------
 | userId | ratings                                                                   |
 --------------------------------------------------------------------------------------
 | 1      | System.Collections.Generic.SortedDictionary`2[System.Int32,System.Single] |
 --------------------------------------------------------------------------------------

And something like this:

ConsoleTable.From(new[] {1, 2, 3, 4, 5, 6, 7}).Write();

Does not work, because the columns aren't defined.

And because it is a static method we cannot do something like this:

ConsoleTable table = new ConsoleTable();
table.AddColumn(new[] {"userid", "1", "2", "3"});
table.From(new[] {"1", "3.5", "5.0", "1.1"})