PwshSpectreConsole
repository·prerelease·Indexed 42 days ago
https://github.com/shaunlawrie/pwshspectreconsoleA PowerShell module that wraps the Spectre.Console .NET library, providing a simplified interface for creating beautiful, interactive console applications. It enables advanced visual features like styled text, emojis, charts, and live-updating UIs (TUIs) using idiomatic PowerShell commands such as Read-SpectreSelection and Invoke-SpectreLive.
What's inside PwshSpectreConsole
- PwshSpectreConsole is a PowerShell wrapper for the Spectre.Console .NET library. It provides access to all Spectre.Console features while simplifying the verbose C# types and extension methods into a more idiomatic PowerShell experience.
What is PwshSpectreConsole?
prereleasePwshSpectreConsole is a PowerShell wrapper for Spectre Console, a .NET library designed for creating highly visual and styled console applications.
While the original Spectre Console is written in C#, PwshSpectreConsole is implemented in PowerShell to make its core features more accessible to PowerShell users without requiring deep C# knowledge. It aims to bridge the gap between the advanced language features used in the C# library and the ease of use required for common PowerShell scripting use-cases.
Use Spectre Console markup for text styling
prereleaseMost PwshSpectreConsole functions support a special markup language. This allows you to include colored text and emojis directly within your strings to enhance the console output. For the full syntax specification, refer to the Spectre Console markup documentation at https://spectreconsole.net/markup.How to combine multiple items in a single panel
prereleaseBecause
Format-SpectrePanelonly accepts a single renderable object, you must use layout containers to group multiple items before passing them to the panel function.Vertical Layout (Rows)
Use
Format-SpectreRowsto stack items vertically within a panel:$message = "Thanks Jeff!" $image = Get-SpectreImage -ImagePath ".\private\images\smiley.png" -MaxWidth 25 @($message, $image) | Format-SpectreRows | Format-SpectrePanelHorizontal Layout (Columns)
Use
Format-SpectreColumnsto place items side-by-side within a panel:$message = "Thanks Jeff!" $image = Get-SpectreImage -ImagePath ".\private\images\smiley.png" -MaxWidth 25 @($message, $image) | Format-SpectreColumns | Format-SpectrePanel@($message, $image) | Format-SpectreRows | Format-SpectrePanelTrack parallel jobs with progress bars
prereleaseYou can combine
Invoke-SpectreCommandWithProgresswithAdd-SpectreJobandWait-SpectreJobsto manage multiple background tasks while showing their progress in parallel.For jobs with an estimated duration, you can use the
-EstimatedDurationSecondsparameter withWait-SpectreJobs. If the estimated time passes without the job completing, the progress bar will automatically switch to an indeterminate state.$result = Invoke-SpectreCommandWithProgress -ScriptBlock { param ( [Spectre.Console.ProgressContext] $Context ) $job = Add-SpectreJob -Context $Context -JobName "Doing some work" -Job ( Start-Job { Start-Sleep -Seconds 10; return 1234 } ) Wait-SpectreJobs -Context $Context -Jobs $job -EstimatedDurationSeconds 5 $result = Receive-Job -Job $job.Job return $result }Update nested layout contents using Name
prereleaseWhen creating a layout, you can assign a
-Nameto it. This name allows you to access and update specific nested layouts within a larger root layout structure.If you have a root layout
$rootand a nested layout named"row1", you can update the contents of that specific row using the indexer syntax and the.Update()method.# Example of updating a nested layout named 'row1' within $root $root = $root["row1"].Update(("hello row 1 again" | Format-SpectrePanel))Use Renderables to build complex layouts
prereleaseA Renderable is an object that represents a visual element. Unlike previous versions that wrote directly to the console, most functions in v2 now return a renderable object.
This allows you to:
- Assign output to variables.
- Use renderables as inputs to other functions (e.g., nesting a chart inside a table).
- Redirect output to a file.
To return the object instead of writing it immediately, use the
-PassThruswitch onWrite-*commandlets.# Example of using renderable objects inside a table $calendar = Write-SpectreCalendar -Date (Get-Date) -PassThru $files = Get-ChildItem | Format-SpectreTable $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 Files = $files Fruits = $fruits } | Format-SpectreTable -Color Cyan1Install PwshSpectreConsole
prereleaseInstall the
PwshSpectreConsolemodule from the PowerShell Gallery usingInstall-Module. After installation, you can runStart-SpectreDemoto see a demonstration of the module's capabilities.Install-Module PwshSpectreConsole -Scope CurrentUser Start-SpectreDemoAccess the Canvas widget via C#
prereleaseThe Canvas widget is not directly implemented as a PowerShell function in this module. To use it, you must interact with the underlying C# library directly. After creating and drawing on the canvas, use
Out-SpectreHostto render it.# Create a canvas $canvas = [Spectre.Console.Canvas]::new(16, 16) # Draw some shapes for($i = 0; $i -lt $canvas.Width; $i++) { # Cross $canvas = $canvas.SetPixel($i, $i, [Spectre.Console.Color]::White) $canvas = $canvas.SetPixel($canvas.Width - $i - 1, $i, [Spectre.Console.Color]::White) # Border $canvas = $canvas.SetPixel($i, 0, [Spectre.Console.Color]::Red) $canvas = $canvas.SetPixel(0, $i, [Spectre.Console.Color]::Green) $canvas = $canvas.SetPixel($i, $canvas.Height - 1, [Spectre.Console.Color]::Blue) $canvas = $canvas.SetPixel($canvas.Width - 1, $i, [Spectre.Console.Color]::Yellow) } # Render the canvas $canvas | Out-SpectreHostFormat comment-based help for documentation generation
prereleaseThe documentation site is generated from the comment-based help within module functions. To ensure correct Markdown generation, follow these rules:
Supported Fields
DESCRIPTION: Used as the main description on the documentation site.PARAMETER: Documentation is kept and grouped under a 'Parameters' section.EXAMPLE: Used to generate code examples. You can provide multiple examples.
Critical Constraints
- No Leading Comments: The first line of an
.EXAMPLEblock cannot be a comment line. Doing so will break the Markdown generation process. - Self-Contained Examples: Examples must be valid, runnable PowerShell statements. If a function requires data setup (e.g., creating objects), include that setup within the
.EXAMPLEblock so a user can copy and paste the entire block into a console.
<# .EXAMPLE $data = @() $data += New-SpectreChartItem -Label "Apples" -Value 10 -Color "Green" Format-SpectreBarChart -Data $data -Label "Fruit Sales" -Width 50 #>Remove whitespace padding with Out-SpectreHost
prereleaseIn version 2, PwshSpectreConsole uses PowerShell formatting to render objects. By default, when rendered to the terminal, PowerShell adds a blank line before and after the item. To render an item without this additional whitespace, pipe the output to
Out-SpectreHost.$data | Format-SpectrePanel | Out-SpectreHostAccess PwshSpectreConsole documentation
prereleaseYou can find the full documentation for the module at the official website. For testing the latest features, use the prerelease documentation site.
- Full Documentation: https://pwshspectreconsole.com/
- Prerelease Documentation: https://prerelease.pwshspectreconsole.com/