ConsoleTableExt

repository·master·Indexed 43 days ago

https://github.com/minhhungit/consoletableext

A C# library for printing customizable, formatted tables in console applications. It features support for box-drawing characters, custom color schemes, and advanced character mapping via CharMapDefinition. Key functionality includes the ConsoleTableBuilder for defining titles, column headers, minimum lengths, and text alignment.

Tokens
1.5K
Snippets
5
Records
5
Agent score
6%

What's inside ConsoleTableExt

  1. Use alternative table formats and alignment

    master

    You can change the table style using .WithFormat(ConsoleTableBuilderFormat.Alternative) and control the overall table alignment (left, right, or center) by passing a TableAligntment value to ExportAndWriteLine().

    ConsoleTableBuilder
        .From(tableData)
        .WithFormat(ConsoleTableBuilderFormat.Alternative)
        .ExportAndWriteLine(TableAligntment.Center);
  2. Print a simple table with default formatting

    master

    To print a table using the default format, use ConsoleTableBuilder.From(data) where data is a collection of data (such as List<List<object>>) and call ExportAndWriteLine() to output it to the console.

    var tableData = new List<List<object>>
    {
        new List<object>{ "Sakura Yamamoto", "Support Engineer", "London", 46},
        new List<object>{ "Serge Baldwin", "Data Coordinator", "San Francisco", 28, "something else" },
        new List<object>{ "Shad Decker", "Regional Director", "Edinburgh"},
    };
    
    ConsoleTableBuilder
        .From(tableData)
        .ExportAndWriteLine();
  3. Customize table borders using CharMapDefinition

    master

    You can completely redefine the characters used to draw the table borders and dividers using .WithCharMapDefinition() and .WithHeaderCharMapDefinition(). This allows for high customization of the table's visual structure.

    • WithCharMapDefinition: Controls the main table border characters using CharMapPositions.
    • WithHeaderCharMapDefinition: Controls the header area border characters using HeaderCharMapPositions.
    ConsoleTableBuilder
        .From(tableData)
        .WithCharMapDefinition(new Dictionary<CharMapPositions, char> {
            {CharMapPositions.BottomLeft, '=' },
            {CharMapPositions.BottomCenter, '=' },
            {CharMapPositions.BottomRight, '=' },
            {CharMapPositions.BorderTop, '=' },
            {CharMapPositions.BorderBottom, '=' },
            {CharMapPositions.BorderLeft, '|' },
            {CharMapPositions.BorderRight, '|' },
            {CharMapPositions.DividerY, '|' },
        })
        .WithHeaderCharMapDefinition(new Dictionary<HeaderCharMapPositions, char> {
            {HeaderCharMapPositions.TopLeft, '=' },
            {HeaderCharMapPositions.TopCenter, '=' },
            {HeaderCharMapPositions.TopRight, '=' },
            {HeaderCharMapPositions.BottomLeft, '|' },
            {HeaderCharMapPositions.BottomCenter, '-' },
            {HeaderCharMapPositions.BottomRight, '|' },
            {HeaderCharMapPositions.Divider, '|' },
            {HeaderCharMapPositions.BorderTop, '=' },
            {HeaderCharMapPositions.BorderBottom, '-' },
            {HeaderCharMapPositions.BorderLeft, '|' },
            {HeaderCharMapPositions.BorderRight, '|' },
        })
        .ExportAndWriteLine();
  4. Customize tables with titles, columns, and alignment

    master

    For advanced customization, ConsoleTableBuilder provides several methods:

    • .WithTitle(string title, ConsoleColor foreground, ConsoleColor background): Adds a title with specific colors.
    • .WithColumn(params string[] columnNames): Defines specific column headers.
    • .WithMinLength(Dictionary<int, int> columnMinLengths): Sets a minimum width for specific columns (using 1-based column indices).
    • .WithTextAlignment(Dictionary<int, TextAligntment> columnAlignments): Sets text alignment (Left, Right, Center) for specific columns (using 1-based column indices).
    • .ExportAndWriteLine(TableAligntment alignment): Renders the table with a specific overall alignment.
    ConsoleTableBuilder
        .From(tableData)
        .WithTitle("CONTACTS ", ConsoleColor.Yellow, ConsoleColor.DarkGray)
        .WithColumn("Id", "First Name", "Sur Name")
        .WithMinLength(new Dictionary<int, int> {
            { 1, 25 },
            { 2, 25 }
        })
        .WithTextAlignment(new Dictionary<int, TextAligntment>
        {
            {2, TextAligntment.Right }
        })
        .ExportAndWriteLine(TableAligntment.Right);