Skip to content

Format-SpectreTable

Description

This function takes an array of objects and formats them into a table using the Spectre Console library. The table can be customized with a border style and color.
Thanks to trackd and fmotion1 for the updates to support markdown and color in the table contents.
See https://spectreconsole.net/widgets/table for more information.


Examples

Example 1
This example demonstrates how to format an array of objects into a Spectre Console table.

$data = @(
    [pscustomobject]@{Name="John"; Age=25; City="New York"},
    [pscustomobject]@{Name="Jane"; Age=30; City="Los Angeles"}
)
Format-SpectreTable -Data $data

Example 2
This example demonstrates how to format an array of objects into a Spectre Console table with custom properties generated by scriptblock expressions.

$Properties = @(
    # foreground + background
    @{'Name'='FileName'; Expression={ "[white on DeepSkyBlue3_1]" + $_.Name + "[/]" }},
    # foreground
    @{'Name'='Last Updated'; Expression={ "[DeepSkyBlue3_1]" + $_.LastWriteTime.ToString() + "[/]" }},
    # background
    @{'Name'='Drive'; Expression={ "[black on LightGreen_1]" + $_.PSDrive.Root + "[/]" }}
)
Get-ChildItem | Format-SpectreTable -Property $Properties -AllowMarkup

Example 3
This example demonstrates how to format an array of scalar objects into a Spectre Console table.

1..10 | Format-SpectreTable -Title Numbers

Example 4
This example demonstrates how to nest other renderable objects inside a table.

$calendar = Write-SpectreCalendar -Date (Get-Date) -PassThru
$fruits = @(
    (New-SpectreChartItem -Label "Bananas" -Value 2.2 -Color Yellow),
    (New-SpectreChartItem -Label "Oranges" -Value 6.6 -Color Orange1),
    (New-SpectreChartItem -Label "Apples" -Value 1 -Color Red)
) | Format-SpectreBarChart -Width 45

@{
    Calendar = $calendar
    Fruits = $fruits
} | Format-SpectreTable -Color Cyan1

Parameters

Data

The array of objects to be formatted into a table. Takes pipeline input.

TypeRequiredPositionPipelineInputAliases
[Object]truenamedtrue (ByValue)InputObject

Property

Specifies the object properties that appear in the display and the order in which they appear. Type one or more property names, separated by commas, or use a hash table to display a calculated property. Wildcards are permitted. The Property parameter is optional. You can’t use the Property and View parameters in the same command. The value of the Property parameter can be a new calculated property. The calculated property can be a script block or a hash table. Valid key-value pairs are:

  • Name (or Label) <string>
  • Expression - <string> or <script block>
  • FormatString - <string>
  • Width - <int32> - must be greater than 0
  • Alignment - value can be Left, Center, or Right
TypeRequiredPositionPipelineInput
[Object[]]false1false

Wrap

Displays text that exceeds the column width on the next line. By default, text that exceeds the column width is truncated Currently there is a bug with this, spectre.console/issues/1185

TypeRequiredPositionPipelineInput
[Switch]falsenamedfalse

View

The View parameter lets you specify an alternate format or custom view for the table.

TypeRequiredPositionPipelineInput
[String]falsenamedfalse

Border

The border style of the table. Default is “Rounded”.

Valid Values:

  • Ascii
  • Ascii2
  • AsciiDoubleHead
  • Double
  • DoubleEdge
  • Heavy
  • HeavyEdge
  • HeavyHead
  • Horizontal
  • Markdown
  • Minimal
  • MinimalDoubleHead
  • MinimalHeavyHead
  • None
  • Rounded
  • Simple
  • SimpleHeavy
  • Square
TypeRequiredPositionPipelineInput
[String]falsenamedfalse

Color

The color of the table border. Default is the accent color of the script.

TypeRequiredPositionPipelineInput
[Color]falsenamedfalse

HeaderColor

The color of the table header text. Default is the DefaultTableHeaderColor.

TypeRequiredPositionPipelineInput
[Color]falsenamedfalse

TextColor

The color of the table text. Default is the DefaultTableTextColor.

TypeRequiredPositionPipelineInput
[Color]falsenamedfalse

Width

The width of the table.

TypeRequiredPositionPipelineInput
[Int32]falsenamedfalse

HideHeaders

Hides the headers of the table.

TypeRequiredPositionPipelineInput
[Switch]falsenamedfalse

Title

The title of the table.

TypeRequiredPositionPipelineInput
[String]falsenamedfalse

AllowMarkup

Allow Spectre markup in the table elements e.g. [green]message[/].

TypeRequiredPositionPipelineInput
[Switch]falsenamedfalse

Syntax

Format-SpectreTable -Data <Object> [-Wrap] [-Border <String>] [-Color <Color>] [-HeaderColor <Color>] [-TextColor <Color>] [-Width <Int32>] [-HideHeaders] [-Title <String>] [-AllowMarkup] [<CommonParameters>]
Format-SpectreTable -Data <Object> [[-Property] <Object[]>] [-Wrap] [-Border <String>] [-Color <Color>] [-HeaderColor <Color>] [-TextColor <Color>] [-Width <Int32>] [-HideHeaders] [-Title <String>] [-AllowMarkup] [<CommonParameters>]
Format-SpectreTable -Data <Object> [-Wrap] [-View <String>] [-Border <String>] [-Color <Color>] [-HeaderColor <Color>] [-TextColor <Color>] [-Width <Int32>] [-HideHeaders] [-Title <String>] [-AllowMarkup] [<CommonParameters>]