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

Centered Header Column with Left Aligned Column Rows #18

Closed REJack closed 3 years ago

REJack commented 3 years ago

Thx for your amazing library 😍

It is possible to align the header in center but align the content rows to the left?

Actual behavior: img

Ideal behavior: img

P.S. The images are demo images created fast in VSCode 🤣

regards REJack

minhhungit commented 3 years ago

hi @REJack version 3.1.5 has just released and support it, now you can use .WithHeaderTextAlignment(...)

This is demo code:

ConsoleTableBuilder
     .From(SampleTableData())
     .WithFormat(ConsoleTableBuilderFormat.Alternative)
     .WithColumnFormatter(1, (text) => $"[ {text.ToUpper()} ]")
     .WithFormatter(1, (text) => $"<{text}>")
     .WithMinLength(new Dictionary<int, int> {
          { 1, 30 } // increase column width to look easier
     })
     .WithTextAlignment(new Dictionary<int, TextAligntment>{
          { 1, TextAligntment.Right } // align text of 2nd column to Right
     })
     .WithHeaderTextAlignment(new Dictionary<int, TextAligntment> {
          {1, TextAligntment.Center } // alight header text of 2nd column to Center
     })
     .WithTitle("MY TABLE", ConsoleColor.DarkRed, ConsoleColor.Gray, TextAligntment.Right)
     .ExportAndWriteLine(TableAligntment.Center);
static DataTable SampleTableData()
{
    DataTable table = new DataTable();
    table.Columns.Add("Name", typeof(string));
    table.Columns.Add("Position", typeof(string));
    table.Columns.Add("Office", typeof(string));
    table.Columns.Add("Age", typeof(int));
    table.Columns.Add("Start Date", typeof(DateTime));

    table.Rows.Add("Airi Satou", "Accountant", "Tokyo", 33, new DateTime(2017, 05, 09));
    table.Rows.Add("Angelica Ramos", "Chief Executive Officer (CEO)", "New York", 47, new DateTime(2017, 01, 12));
    table.Rows.Add("Ashton Cox", "Junior Technical Author", "London", 46, new DateTime(2017, 04, 02));
    table.Rows.Add("Bradley Greer", "Software Engineer", "San Francisco", 28, new DateTime(2017, 11, 15));

    return table;
}

Screenshot

image

REJack commented 3 years ago

Sweet 😄 thx for the demo code, I will adapt it.