SkillAgentSearch skills...

CsConsoleFormat

.NET C# library for advanced formatting of console output [Apache]

Install / Use

/learn @Athari/CsConsoleFormat

README

<img align="right" width="128" src="Docs/Images/CsConsoleFormatIcon256.png" style="margin: 0 20px">

CsConsoleFormat: advanced formatting of console output for .NET

<!-- -->
PM> Install-Package Alba.CsConsoleFormat

GitHub license GitHub license GitHub license Badges <br> AppVeyor build master AppVeyor tests master <br> GitHub release version GitHub release date GitHub commits since release GitHub open issues GitHub closed issues GitHub pull requests <br> NuGet release version NuGet downloads MyGet pre-release version MyGet downloads <br> FOSSA license scan CII best practices Libraries.io Dependencies kandi X-Ray

CsConsoleFormat is a library for formatting text in console based on documents resembling a mix of WPF and HTML: tables, lists, paragraphs, colors, word wrapping, lines etc. Like this:

<Document>
    <Span Color="Red">Hello</Span>
    <Br/>
    <Span Color="Yellow">world!</Span>
</Document>

or like this:

new Document(
    new Span("Hello") { Color = ConsoleColor.Red },
    "\n",
    new Span("world!") { Color = ConsoleColor.Yellow }
);

or even like this:

Colors.WriteLine("Hello".Red(), "\n", "world!".Yellow());

Why?

.NET Framework includes only very basic console formatting capabilities. If you need to output a few strings, it's fine. If you want to output a table, you have to calculate column widths manually, often hardcode them. If you want to color output, you have to intersperse writing strings with setting and restoring colors. If you want to wrap words properly or combine all of the above...

The code quickly becomes an unreadable mess. It's just not fun! In GUI, we have MV*, bindings and all sorts of cool stuff. Writing console applications feels like returning to the Stone Age.

CsConsoleFormat to the rescue!

Imagine you have the usual Order, OrderItem and Customer classes. Let's create a document which prints the order. There're two major syntaxes, you can use either.

XAML (like WPF):

<Document xmlns="urn:alba:cs-console-format"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Span Background="Yellow" Text="Order #"/>
    <Span Text="{Get OrderId}"/>
    <Br/>
    <Span Background="Yellow" Text="Customer: "/>
    <Span Text="{Get Customer.Name}"/>

    <Grid Color="Gray">
        <Grid.Columns>
            <Column Width="Auto"/>
            <Column Width="*"/>
            <Column Width="Auto"/>
        </Grid.Columns>
        <Cell Stroke="Single Double" Color="White">Id</Cell>
        <Cell Stroke="Single Double" Color="White">Name</Cell>
        <Cell Stroke="Single Double" Color="White">Count</Cell>
        <Repeater Items="{Get OrderItems}">
            <Cell>
                <Span Text="{Get Id}"/>
            </Cell>
            <Cell>
                <Span Text="{Get Name}"/>
            </Cell>
            <Cell Align="Right">
                <Span Text="{Get Count}"/>
            </Cell>
        </Repeater>
    </Grid>
</Document>
// Assuming Order.xaml is stored as an Embedded Resource in the Views folder.
Document doc = ConsoleRenderer.ReadDocumentFromResource(GetType(), "Views.Order.xaml", Order);
ConsoleRenderer.RenderDocument(doc);

C# (like LINQ to XML):

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);

C# (like npm/colors):

using Alba.CsConsoleFormat.Fluent;

Colors.WriteLine(
    "Order #".Yellow(), Order.Id, "\n",
    "Customer: ".Yellow(), Order.Customer.Name,
    // the rest is the same
);

Features

  • HTML-like elements: paragraphs, spans, tables, lists, borders, separators.
  • Layouts: grid, stacking, docking, wrapping, absolute.
  • Text formatting: foreground and background colors, character wrapping, word wrapping.
  • Unicode formatting: hyphens, soft hyphens, no-break hyphens, spaces, no-break spaces, zero-width spaces.
  • Multiple syntaxes (see examples above):
    • *
View on GitHub
GitHub Stars368
CategoryDevelopment
Updated4d ago
Forks29

Languages

C#

Security Score

85/100

Audited on Mar 25, 2026

No findings