minhhungit / ConsoleTableExt

A fluent library to print out a nicely formatted table in a console application C#
MIT License
333 stars 37 forks source link

Automatic format of number types #29

Open michalfita opened 3 years ago

michalfita commented 3 years ago

The ConsoleTables has a nice feature, where .Configure(o => o.NumberAlignment = Alignment.Right) sets up automatic right alignment of numbers in the table.

Would you consider adding similar mechanism to your project?

minhhungit commented 3 years ago

hi @michalfita , ConsoleTableExt treat all values as string, so it does not discriminate "number" or "string"

but ConsoleTableExt also has a feature which give us define which columns we want to align using WithTextAlignment extension, for example:

.WithTextAlignment(new Dictionary<int, TextAligntment>{
    { 1, TextAligntment.Right },
    { 3, TextAligntment.Center}
})

In above example, 1 and 3 are column indexs

Same, you can align header with extension WithHeaderTextAlignment

.WithHeaderTextAlignment(new Dictionary<int, TextAligntment> {
   {1, TextAligntment.Center }
})

Check more in example project here https://github.com/minhhungit/ConsoleTableExt/tree/master/Src/ConsoleTableApp

michalfita commented 3 years ago

I'm aware of this feature, however this require really going extra mile in comparison to ConsoleTables if you want to stick with simple From() call but want numbers right aligned. As you keep List<List<object>> some extra meta-magic would be possible figured if the original data were actually the number.

minhhungit commented 3 years ago

Support the feature is not too hard, but we will need to check every values in the table to verify if value is a "number" or not, this will cause a performance problem, it can be slower for large table. Anyway, I will consider to support it, I admit that defining text align by column index is quite bored