Alba.CsConsoleFormat 0.9.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Alba.CsConsoleFormat --version 0.9.0
NuGet\Install-Package Alba.CsConsoleFormat -Version 0.9.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Alba.CsConsoleFormat" Version="0.9.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Alba.CsConsoleFormat --version 0.9.0
#r "nuget: Alba.CsConsoleFormat, 0.9.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Alba.CsConsoleFormat as a Cake Addin
#addin nuget:?package=Alba.CsConsoleFormat&version=0.9.0

// Install Alba.CsConsoleFormat as a Cake Tool
#tool nuget:?package=Alba.CsConsoleFormat&version=0.9.0

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

CsConsoleFormat: advanced formatting of console output for .NET

AppVeyor Build master

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()
    .AddChildren(
        new Span("Hello") { Color = ConsoleColor.Red },
        "\n",
        new Span("world!") { Color = ConsoleColor.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 usual Order, OrderItem and Customer classes. Let's create a document which prints the order. There're two 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 Wide" Color="White">Id</Cell>
        <Cell Stroke="Single Wide" Color="White">Name</Cell>
        <Cell Stroke="Single Wide" 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.Single, LineWidth.Wide);

var doc = new Document()
    .AddChildren(
        new Span("Order #") { Color = Yellow },
        Order.Id,
        "\n",
        new Span("Customer: ") { Color = Yellow },
        Order.Customer.Name,

        new Grid { Color = Gray }
            .AddColumns(
                new Column { Width = GridLength.Auto },
                new Column { Width = GridLength.Star(1) },
                new Column { Width = GridLength.Auto }
            )
            .AddChildren(
                new Cell { Stroke = headerThickness }
                    .AddChildren("Id"),
                new Cell { Stroke = headerThickness }
                    .AddChildren("Name"),
                new Cell { Stroke = headerThickness }
                    .AddChildren("Count"),
                Order.OrderItems.Select(item => new[] {
                    new Cell()
                        .AddChildren(item.Id),
                    new Cell()
                        .AddChildren(item.Name),
                    new Cell { Align = HorizontalAlignment.Right }
                        .AddChildren(item.Count),
                })
            )
    );

ConsoleRenderer.RenderDocument(doc);

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):
    • Like WPF: XAML with one-time bindings, resources, converters, attached properties, loading documents from assembly resources.
    • Like LINQ to XML: C# with object initializers, setting attached properties via extension methods or indexers, adding children elements by collapsing enumerables and converting objects and strings to elements.
  • Drawing: geometric primitives (lines, rectangles) using box-drawing characters, color transformations (dark, light), text, images.
  • Internationalization: cultures are respected on every level and can be customized per-element.
  • Export to many formats: ANSI text, unformatted text, HTML; RTF, XPF, WPF FixedDocument, WPF FlowDocument.
  • JetBrains ReSharper annotations: CanBeNull, NotNull, ValueProvider, Pure etc.
  • WPF document control, document converter.

Getting started

  1. Install NuGet package Alba.CsConsoleFormat using Package Manager:

     PM> Install-Package Alba.CsConsoleFormat
    

    or .NET CLI:

     > dotnet add package Alba.CsConsoleFormat
    
  2. Add using Alba.CsConsoleFormat; to your .cs file.

  3. If you're going to use ASCII graphics on Windows, set Console.OutputEncoding = Encoding.UTF8;.

  4. If you want to use XAML:

    1. Add XAML file to your project. Set its build action to "Embedded Resource".
    2. Load XAML using ConsoleRenderer.ReadDocumentFromResource.
  5. If you want to use pure C#:

    1. Build a document in code starting with Document element as a root.
  6. Call ConsoleRenderer.RenderDocument on the generated document.

TODO

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp1.0 was computed.  netcoreapp1.1 was computed.  netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.3 is compatible.  netstandard1.4 was computed.  netstandard1.5 was computed.  netstandard1.6 was computed.  netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net40 is compatible.  net403 was computed.  net45 was computed.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen30 was computed.  tizen40 was computed.  tizen60 was computed. 
Universal Windows Platform uap was computed.  uap10.0 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on Alba.CsConsoleFormat:

Package Downloads
Alba.CsConsoleFormat.ColorfulConsole

Colorful.Console adapter of Alba.CsConsoleFormat. Supports rendering FIGlet fonts.

Alba.CsConsoleFormat.Presentation

WPF module of Alba.CsConsoleFormat. WPF control for rendering console documents. Output images to console. Export to FixedDocument, FlowDocument, RTF and XPS.

ArgParser.Styles.Extensions

Extensions for the built in styles for the ArgParser project

dexcmd

Azure Data Explorer Command Line utility

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on Alba.CsConsoleFormat:

Repository Stars
fedarovich/qbittorrent-cli
Command line interface for QBittorrent
Version Downloads Last updated
1.0.0 104,973 3/15/2018
0.10.0 1,617 3/7/2018
0.9.1 1,560 2/28/2018
0.9.0 1,454 2/25/2018