Underscore
Underscore
Explore posts from servers
CC#
Created by Underscore on 9/4/2023 in #help
❔ How does CsConsoleFormat work?
CsConsoleFormat and ConsoleTable are two different libraries to output C# objects into a formatted table in the terminal. CsConsoleFormat is more complex than consoletable. As a beginner to the language I'm kind of confused by the README on the CsConsoleFormat. https://github.com/Athari/CsConsoleFormat/blob/master/ReadMe.md
using static System.ConsoleColor;

var headerThickness = new LineThickness(LineWidth.Double, LineWidth.Single);

var doc = new Document(
new Span("Order #") { Color = Yellow }, Order.Id, "\n",
new Span("Customer: ") { Color = Yellow }, Order.Customer.Name,
new Grid {
Color = Gray,
Columns = { GridLength.Auto, GridLength.Star(1), GridLength.Auto },
Children = {
new Cell("Id") { Stroke = headerThickness },
new Cell("Name") { Stroke = headerThickness },
new Cell("Count") { Stroke = headerThickness },
Order.OrderItems.Select(item => new[] {
new Cell(item.Id),
new Cell(item.Name),
new Cell(item.Count) { Align = Align.Right },
})
}
}
);

ConsoleRenderer.RenderDocument(doc);
using static System.ConsoleColor;

var headerThickness = new LineThickness(LineWidth.Double, LineWidth.Single);

var doc = new Document(
new Span("Order #") { Color = Yellow }, Order.Id, "\n",
new Span("Customer: ") { Color = Yellow }, Order.Customer.Name,
new Grid {
Color = Gray,
Columns = { GridLength.Auto, GridLength.Star(1), GridLength.Auto },
Children = {
new Cell("Id") { Stroke = headerThickness },
new Cell("Name") { Stroke = headerThickness },
new Cell("Count") { Stroke = headerThickness },
Order.OrderItems.Select(item => new[] {
new Cell(item.Id),
new Cell(item.Name),
new Cell(item.Count) { Align = Align.Right },
})
}
}
);

ConsoleRenderer.RenderDocument(doc);
Unlike the README for ConsoleTables https://github.com/khalidabuhakmeh/ConsoleTables
// using ConsoleTables;
static void Main(String[] args)
{
var table = new ConsoleTable("one", "two", "three");
table.AddRow(1, 2, 3)
.AddRow("this line should be longer", "yes it is", "oh");

table.Write();
Console.WriteLine();

var rows = Enumerable.Repeat(new Something(), 10);

ConsoleTable
.From<Something>(rows)
.Configure(o => o.NumberAlignment = Alignment.Right)
.Write(Format.Alternative);

Console.ReadKey();
}
// using ConsoleTables;
static void Main(String[] args)
{
var table = new ConsoleTable("one", "two", "three");
table.AddRow(1, 2, 3)
.AddRow("this line should be longer", "yes it is", "oh");

table.Write();
Console.WriteLine();

var rows = Enumerable.Repeat(new Something(), 10);

ConsoleTable
.From<Something>(rows)
.Configure(o => o.NumberAlignment = Alignment.Right)
.Write(Format.Alternative);

Console.ReadKey();
}
I don't really see where CsConsoleFormat takes in data and when it is printed. I also can't find any more documentation on this library. How would I uses CsConsoleFormat to print a list of class instances and some of their attributes?
20 replies