Patrik Svensson 003e15686f Add documentation for tables
Also moves "Colors" and "Styles" sections to appendix.
2020-09-08 18:21:52 +02:00

2.2 KiB

Title: Tables Order: 3

Tables are a perfect way of displaying tabular data in a terminal.

Spectre.Console is super smart about rendering tables and will adjust all columns to fit whatever is inside them. Anything that implements IRenderable can be used as a column header or column cell, even another table!

Usage

To render a table, create a Table instance, add the number of columns that you need, and then add the rows. Finish by passing the table to a console's Render method.

// Create a table
var table = new Table();

// Add some columns
table.AddColumn("Foo");
table.AddColumn(new TableColumn("Bar").Centered());

// Add some rows
table.AddRow("Baz", "[green]Qux[/]");
table.AddRow(new Markup("[blue]Corgi[/]"), new Panel("Waldo"));

// Render the table to the console
AnsiConsole.Render(table);

This will render the following output:

Table

Table appearance

Borders

// Sets the border kind
table.SetBorderKind(BorderKind.None);
table.SetBorderKind(BorderKind.Ascii);
table.SetBorderKind(BorderKind.Square);
table.SetBorderKind(BorderKind.Rounded);

Expand / Collapse

// Table will take up as much space as it can
// with respect to other things.
table.Expand();

// Table will take up minimal width
table.Collapse();

Hide headers

// Hides all column headers
table.HideHeaders();

Set table width

// Sets the table width to 50 cells
table.SetWidth(50);

Column appearance

Alignment

// Set the alignment explicitly
column.SetAlignment(Justify.Right);

Padding

// Set left and right padding
column.SetPadding(left: 3, right: 5);

// Set padding individually.
column.PadLeft(3);
column.PadRight(5);

Disable column wrapping

// Disable column wrapping
column.NoWrap();

Set column width

// Set the column width (no fluent extension method for this yet)
column.Width = 15;