Coder Social home page Coder Social logo

Comments (2)

Athari avatar Athari commented on May 18, 2024 1

@nikoudel The "simple fully working and copy-pastable example to start with, right on the front page" exists exactly there under the title "C# (like LINQ to XML)". Yes, it includes setting colors, alignment and borders, but you can just remove what you don't need. In your case, the code would look like this:

var colors = new[] {
    new { Name = "Blue", Code = "#0000FF" },
    new { Name = "Yellow", Code = "#FFFF00" },
    new { Name = "Cyan", Code = "#00FFFF" },
};
var doc = new Document(
    "HTML Colors",
    new Grid {
        Columns = { -1, -1 }, // -1 means GridLength.Auto - size column to content
        Children = {
            new Cell("Name"),
            new Cell("Code"),
            colors.Select(c => new [] {
                new Cell(c.Name),
                new Cell(c.Code),
            })
        }
    }
);
ConsoleRenderer.RenderDocument(doc);

This code would produce this output:

HTML Colors
╔══════╤═══════╗
║Name  │Code   ║
╟──────┼───────╢
║Blue  │#0000FF║
╟──────┼───────╢
║Yellow│#FFFF00║
╟──────┼───────╢
║Cyan  │#00FFFF║
╚══════╧═══════╝

It's more verbose than ConsoleTable library, but it's impossible to construct facades that would work for every case as there're too many options, so everyone would need something different. Normally, if you want many tables with various formatting in your console application, the best approach is probably to separate views from models, like it's done in GUI apps. So you would have a method like this:

public static Document RenderHtmlColors((string name, string code)[] colors) => new Document(
    "HTML Colors",
    new Grid {
        Columns = { -1, -1 },
        Children = {
            new Cell("Name"),
            new Cell("Code"),
            colors.Select(c => new[] {
                new Cell(c.name),
                new Cell(c.code),
            })
        }
    }
);

and would call it from the main code:

var colors = new[] {
    (name: "Blue", code: "#0000FF"),
    (name: "Yellow", code: "#FFFF00"),
    (name: "Cyan", code: "#00FFFF"),
};
ConsoleRenderer.RenderDocument(RenderHtmlColors(colors));

Alternatively, you can write a facade which outputs the tables you want with less code. A starting point would look like this:

public class ConsoleTable
{
    public string Title { get; set; }
    public IEnumerable<string> ColumnNames { get; set; }
    public IEnumerable<string> Cells { get; set; }

    public void Render()
    {
        var doc = new Document(
            Title,
            new Grid {
                Columns = { ColumnNames.Select(c => -1) },
                Children = {
                    ColumnNames.Select(c => new Cell(c)),
                    Cells.Select(c => new Cell(c)),
                }
            }
        );
        ConsoleRenderer.RenderDocument(doc);
    }
}

You can then use it like this:

var table = new ConsoleTable {
    Title = "HTML Colors",
    ColumnNames = new[] { "Name", "Code" },
    Cells = colors.SelectMany(c => new[] { c.name, c.code }).ToList()
};
table.Render();

(If you're confused by what happens with IEnumerable inside IEnumerable and how it works, please check out System.Xml.Linq documentation as the logic of collapsing enumerables is the same as in LINQ to XML.)

Even something as trivial as this is superior to ConsoleTable library already, for example, it properly handles multi-line cells.

You can easily expand this code to suit your needs. For example, instead of specifying column headers and and cell contens explicitly, you can use expressions and reflection. Overall, you write the facades that you need and then use them.

P.S. I understand that the library may look too complicated, as it burrows concepts from unrelated technologies (WPF, HTML, LINQ to XML), but I hope this clears up things a bit. If you have more questions, feel free to ask.

from csconsoleformat.

nikoudel avatar nikoudel commented on May 18, 2024

I get it now, thank you!

It was just missing a bit of context (the Order.OrderItems thing). Now it seems obvious when I have the whole model in mind but the first sight was a bit confusing.

from csconsoleformat.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.