minhhungit / ConsoleTableExt

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

Minimal format but with spaces between the columns #34

Open dmoonfire opened 2 years ago

dmoonfire commented 2 years ago

I like the minimal format, except I find the solid bar for the headers to be difficult to parse the columns.

DefaultString DefaultInt32 HiddenString 
----------------------------------------
Row 1                    1 Hidden 1     
Row 2                   10 Hidden 2     
Row 3                  100 Hidden 3     

So, I thought I would try to make a version that had spaces in it:

.WithHeaderCharMapDefinition(
    new Dictionary<HeaderCharMapPositions, char>
    {
        { HeaderCharMapPositions.BottomCenter, ' ' },
        { HeaderCharMapPositions.Divider, ' ' },
        { HeaderCharMapPositions.BorderBottom, '-' },
    })

But what I found was if both BottomCenter and Divider are spaces (' '), then the space is collapsed between the two of them which squishes the columns together. I ended up needing to put something between the two to make it work:

.WithCharMapDefinition(
    new Dictionary<CharMapPositions, char>
    {
        { CharMapPositions.DividerY, ' ' },
    })
.WithHeaderCharMapDefinition(
    new Dictionary<HeaderCharMapPositions, char>
    {
        { HeaderCharMapPositions.BottomCenter, '+' },
        { HeaderCharMapPositions.Divider, ' ' },
        { HeaderCharMapPositions.BorderBottom, '-' },
    });

This produces a table like this:

 DefaultString   DefaultInt32   HiddenString 
---------------+--------------+--------------
 Row 1                      1   Hidden 1     
 Row 2                     10   Hidden 2     
 Row 3                    100   Hidden 3     

Adding that adds extra columns to the right and left of the headers, which isn't ideal but workable. But, being able to have spaces between the columns would be greatly appreciated. Is there a way of doing that?

minhhungit commented 2 years ago

Not sure I understand your question, are you asking about 2 bellow char image

you can set column padding (space left and right of column) using WithPaddingLeft and WithPaddingRight another option I think you can config Header Char Map for TopLeft, `BorderLeft,BottomLeftandTopRight,BorderRight, andBottomRightto empty string using.WithHeaderCharMapDefinition`

image

dmoonfire commented 2 years ago

Thank you, that covers one of the two questions. The other is the space divider between columns without having padding. In a unit test, this is what I expected would work but didn't.

        [Test]
        public void DefaultFormatWithDataTable()
        {
            var strBuilder =
                ConsoleTableBuilder
                .From(SampleData.SampleTableData)
                .WithPaddingLeft(string.Empty)
                .WithPaddingRight(string.Empty)
                .WithCharMapDefinition(
                    new Dictionary<CharMapPositions, char>
                    {
                        { CharMapPositions.DividerY, ' ' },
                    })
                .WithHeaderCharMapDefinition(
                    new Dictionary<HeaderCharMapPositions, char>
                    {
                        { HeaderCharMapPositions.BottomCenter, ' ' },
                        { HeaderCharMapPositions.Divider, ' ' },
                        { HeaderCharMapPositions.BorderBottom, '-' },
                    });
                .Export();

            Assert.IsTrue(strBuilder.ToString() == @"Name           Position                      Office        Age Start Date
-------------- ----------------------------- ------------- --- ---------------------
Airi Satou     Accountant                    Tokyo         33  5/9/2017 12:00:00 AM   
Angelica Ramos Chief Executive Officer (CEO) New York      47  1/12/2017 12:00:00 AM  
Ashton Cox     Junior Technical Author       London        46  4/2/2017 12:00:00 AM   
Bradley Greer  Software Engineer             San Francisco 28  11/15/2017 12:00:00 AM 
");
            var lines = strBuilder.ToString().Split('\n');
            Assert.IsTrue(lines.Length == 7);
        }

It appears that when I have this:

                        { HeaderCharMapPositions.BottomCenter, ' ' },
                        { HeaderCharMapPositions.Divider, ' ' },

It collapses the divider space entirely instead of giving me a blank line. So I get this divider:

--------------------------------------------------------------------------------

If I change the above BottomCenter to '+', I still get the divider space:

--------------+-----------------------------+-------------+---+---------------------

I would like to know how I could get that unit test output (I might be off on the dashes though on the header).

minhhungit commented 2 years ago

It's because it has fallback from CharMapDefinition, but char on CharMapDefinition is empty

I will try to open a toggle to block the fallback