Comments (2)
@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.
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)
- Extension methods to color strings like in npm.colors for JS
- Extend line characters
- Integrate with popular NuGet packages for parsing command line HOT 4
- Improve printing images to console
- MaxWidth can be ignored if parent element is stretched
- Support CsConsoleFormat-specific DSL
- RenderDocumentToText adds an extra newline HOT 9
- Render using default foreground and background colors HOT 6
- Optimize number of API calls in ConsoleRenderTarget
- Markdown support? HOT 1
- Text Color not working on Linux HOT 1
- Can CsConsoleFormat support output chinese words? HOT 4
- Rendered output is always empty on the Rider IDE unit test HOT 2
- Dotnet Core? HOT 1
- ConsoleTest in new Windows Terminal HOT 5
- Grid Element Does Not Support No Border HOT 2
- Using a Negative Margin on a BlockElement Breaks Height Calculation HOT 4
- Can CsConsoleFormat write the table to a string instead of the console? HOT 2
- Can CsConsoleFormat write the table with pagination?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from csconsoleformat.