Alba.CsConsoleFormat
0.9.1
See the version list below for details.
dotnet add package Alba.CsConsoleFormat --version 0.9.1
NuGet\Install-Package Alba.CsConsoleFormat -Version 0.9.1
<PackageReference Include="Alba.CsConsoleFormat" Version="0.9.1" />
paket add Alba.CsConsoleFormat --version 0.9.1
#r "nuget: Alba.CsConsoleFormat, 0.9.1"
// Install Alba.CsConsoleFormat as a Cake Addin #addin nuget:?package=Alba.CsConsoleFormat&version=0.9.1 // Install Alba.CsConsoleFormat as a Cake Tool #tool nuget:?package=Alba.CsConsoleFormat&version=0.9.1
CsConsoleFormat: advanced formatting of console output for .NET
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
Install NuGet package
Alba.CsConsoleFormat
using Package Manager:PM> Install-Package Alba.CsConsoleFormat
or .NET CLI:
> dotnet add package Alba.CsConsoleFormat
Add
using Alba.CsConsoleFormat;
to your .cs file.If you're going to use ASCII graphics on Windows, set
Console.OutputEncoding = Encoding.UTF8;
.If you want to use XAML:
- Add XAML file to your project. Set its build action to "Embedded Resource".
- Load XAML using
ConsoleRenderer.ReadDocumentFromResource
.
If you want to use pure C#:
- Build a document in code starting with
Document
element as a root.
- Build a document in code starting with
Call
ConsoleRenderer.RenderDocument
on the generated document.
Links
TODO
Product | Versions 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. |
-
.NETFramework 4.0
- No dependencies.
-
.NETStandard 1.3
- Microsoft.CSharp (>= 4.3.0)
- NETStandard.Library (>= 1.6.1)
- Portable.Xaml (>= 0.18.0)
- System.Reflection.TypeExtensions (>= 4.3.0)
-
.NETStandard 2.0
- Microsoft.CSharp (>= 4.3.0)
- Portable.Xaml (>= 0.18.0)
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 (2)
Showing the top 2 popular GitHub repositories that depend on Alba.CsConsoleFormat:
Repository | Stars |
---|---|
fedarovich/qbittorrent-cli
Command line interface for QBittorrent
|
|
ppy/osu-tools
command line tools to get stuff done
|