Skip to content

Commit

Permalink
Yeet in a bunch of additional spectre console functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaunLawrie committed Aug 16, 2024
1 parent dca82c1 commit 31a3ffa
Show file tree
Hide file tree
Showing 15 changed files with 319 additions and 29 deletions.
44 changes: 22 additions & 22 deletions PwshSpectreConsole/PwshSpectreConsole.format.ps1xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<!-- Generated with EZOut 2.0.6: Install-Module EZOut or https://github.com/StartAutomating/EZOut -->
<Configuration>
<ViewDefinitions>
<View>
<Name>Spectre.Console.Rendering.Renderable</Name>
<ViewSelectedBy>
<TypeName>Spectre.Console.Rendering.Renderable</TypeName>
</ViewSelectedBy>
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<ExpressionBinding>
<!-- Generated with EZOut 2.0.6: Install-Module EZOut or https://github.com/StartAutomating/EZOut -->
<Configuration>
<ViewDefinitions>
<View>
<Name>Spectre.Console.Rendering.Renderable</Name>
<ViewSelectedBy>
<TypeName>Spectre.Console.Rendering.Renderable</TypeName>
</ViewSelectedBy>
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<ExpressionBinding>
<ScriptBlock>
# Work out if the current object is being piped to another command, there isn't access to the pipeline in the format view script block so it's using a janky regex workaround
try {
Expand All @@ -33,14 +33,14 @@
if ($targetIsInPipeline -and -not $targetIsPipedToSpectreFunction) {
$_
} else {
Write-AnsiConsole $_
$_ | Out-SpectreHost
}
</ScriptBlock>
</ExpressionBinding>
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</View>
</ViewDefinitions>
</ScriptBlock>
</ExpressionBinding>
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</View>
</ViewDefinitions>
</Configuration>
5 changes: 3 additions & 2 deletions PwshSpectreConsole/PwshSpectreConsole.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ FunctionsToExport = 'Add-SpectreJob', 'Format-SpectreBarChart',
'New-SpectreChartItem', 'Invoke-SpectreScriptBlockQuietly',
'Get-SpectreDemoColors', 'Get-SpectreDemoEmoji', 'Format-SpectreJson',
'Write-SpectreCalendar', 'Start-SpectreRecording', 'Stop-SpectreRecording',
'Format-SpectreColumns', 'Format-SpectreRows',
'Out-SpectreHost'
'Format-SpectreColumns', 'Format-SpectreRows', 'Format-SpectrePadded',
'Format-SpectreGrid', 'New-SpectreGridRow', 'Format-SpectreTextPath',
'New-SpectreLayout', 'Format-SpectreAligned', 'Out-SpectreHost'

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ Write-FormatView -TypeName "Spectre.Console.Rendering.Renderable" -Action {
if ($targetIsInPipeline -and -not $targetIsPipedToSpectreFunction) {
$_
} else {
Write-AnsiConsole $_
$_ | Out-SpectreHost
}
}
27 changes: 27 additions & 0 deletions PwshSpectreConsole/private/ConvertTo-Renderable.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function ConvertTo-Renderable {
param (
[object] $InputData
)

# These objects are already renderable
if ($InputData -is [Spectre.Console.Rendering.Renderable]) {
return $InputData
}

$renderableItems = @()

if ($InputData -is [array]) {
foreach ($column in $InputData) {
$renderableItems += ConvertTo-Renderable $column
}
} else {
# For others just dump them as either strings formatted with markup which are easy to identify by the closing tag [/] or as plain text
if ($InputData -like "*[/]*") {
$renderableItems += [Spectre.Console.Markup]::new($InputData)
} else {
$renderableItems += [Spectre.Console.Text]::new(($InputData | Out-String -NoNewline))
}
}

return $renderableItems
}
14 changes: 14 additions & 0 deletions PwshSpectreConsole/private/completions/Completers.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ class SpectreConsoleJustify : IValidateSetValuesGenerator {
}
}

class SpectreConsoleHorizontalAlignment : IValidateSetValuesGenerator {
[String[]] GetValidValues() {
$lookup = [Spectre.Console.HorizontalAlignment].GetEnumNames()
return $lookup
}
}

class SpectreConsoleVerticalAlignment : IValidateSetValuesGenerator {
[String[]] GetValidValues() {
$lookup = [Spectre.Console.VerticalAlignment].GetEnumNames()
return $lookup
}
}

class SpectreConsoleSpinner : IValidateSetValuesGenerator {
[String[]] GetValidValues() {
$lookup = [Spectre.Console.Spinner+Known] | Get-Member -Static -MemberType Properties | Select-Object -ExpandProperty Name
Expand Down
21 changes: 21 additions & 0 deletions PwshSpectreConsole/private/completions/Transformers.psm1
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using module "..\models\SpectreChartItem.psm1"
using module "..\models\SpectreGridRow.psm1"
using namespace System.Management.Automation

class ColorTransformationAttribute : ArgumentTransformationAttribute {
Expand Down Expand Up @@ -95,3 +96,23 @@ class ChartItemTransformationAttribute : ArgumentTransformationAttribute {
return $outputData
}
}

class GridRowTransformationAttribute : ArgumentTransformationAttribute {

static [object] TransformItem([object]$inputData) {
# These objects are already renderable
if ($InputData -is [SpectreGridRow]) {
return $InputData
}

return [SpectreGridRow]::new($inputData)
}

[object] Transform([EngineIntrinsics]$engine, [object]$inputData) {
$outputData = @()
foreach ($dataItem in $inputData) {
$outputData += [GridRowTransformationAttribute]::TransformItem($dataItem)
}
return $outputData
}
}
24 changes: 24 additions & 0 deletions PwshSpectreConsole/private/models/SpectreGridRow.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class SpectreGridRow {

hidden [Spectre.Console.Rendering.Renderable[]] $InternalColumns = @()

[int] Count() {
return $this.InternalColumns.Count
}

[Spectre.Console.GridRow] ToGridRow() {
return [Spectre.Console.GridRow]::new([Spectre.Console.Rendering.Renderable[]]$this.InternalColumns)
}

SpectreGridRow([object[]] $Columns) {
foreach ($column in $Columns) {
if ($column -is [Spectre.Console.Rendering.Renderable]) {
$this.InternalColumns += $column
} elseif ($column -like "*[/]*") {
$this.InternalColumns += [Spectre.Console.Markup]::new($column)
} else {
$this.InternalColumns += [Spectre.Console.Text]::new($column.ToString().TrimEnd())
}
}
}
}
30 changes: 30 additions & 0 deletions PwshSpectreConsole/public/formatting/Format-SpectreAligned.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using module "..\..\private\completions\Completers.psm1"
using module "..\..\private\completions\Transformers.psm1"

function Format-SpectreAligned {
<#
.SYNOPSIS
Wraps a renderable object in a Spectre Console Aligned object.
.DESCRIPTION
TODO Description
#>
[Reflection.AssemblyMetadata("title", "Format-SpectreAligned")]
param(
[Parameter(ValueFromPipeline, Mandatory)]
[RenderableTransformationAttribute()]
[object] $Data,
[ValidateSet([SpectreConsoleHorizontalAlignment], ErrorMessage = "Value '{0}' is invalid. Try one of: {1}")]
[string] $HorizontalAlignment = "Center",
[ValidateSet([SpectreConsoleVerticalAlignment], ErrorMessage = "Value '{0}' is invalid. Try one of: {1}")]
[string] $VerticalAlignment = "Middle"
)

$aligned = [Spectre.Console.Align]::new(
$Data,
[Spectre.Console.HorizontalAlignment]::$HorizontalAlignment,
[Spectre.Console.VerticalAlignment]::$VerticalAlignment
)

return $aligned
}
58 changes: 58 additions & 0 deletions PwshSpectreConsole/public/formatting/Format-SpectreGrid.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using module "..\..\private\completions\Completers.psm1"
using module "..\..\private\completions\Transformers.psm1"

function Format-SpectreGrid {
<#
.SYNOPSIS
TODO - Add synopsis
.DESCRIPTION
TODO - Add description
#>
[Reflection.AssemblyMetadata("title", "Format-SpectreGrid")]
# two parameter sets, one for padding evenly and one for specifing tlbr separately
param (
[Parameter(ValueFromPipeline, Mandatory)]
[GridRowTransformationAttribute()]
[object]$Data,
[ValidateSet([SpectreConsoleJustify], ErrorMessage = "Value '{0}' is invalid. Try one of: {1}")]
[string] $Justify = 'Left',
[int] $Width
)

begin {
$grid = [Spectre.Console.Grid]::new()
$columnsSet = $false
if ($Width) {
$grid.Width = $Width
}
$grid.Alignment = [Spectre.Console.Justify]::$Justify
$grid = $grid.AddColumn()
}

process {
if ($Data -is [array]) {
foreach ($row in $Data) {
if (!$columnsSet) {
0..($row.Count() - 1) | ForEach-Object {
$grid = $grid.AddColumn()
}
$columnsSet = $true
}
$grid = $grid.AddRow($row.ToGridRow())
}
} else {
if (!$columnsSet) {
0..($row.Count() - 1) | ForEach-Object {
$grid = $grid.AddColumn()
}
$columnsSet = $true
}
$grid = $grid.AddRow($Data.ToGridRow())
}
}

end {
return $grid
}
}
46 changes: 46 additions & 0 deletions PwshSpectreConsole/public/formatting/Format-SpectrePadded.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using module "..\..\private\completions\Transformers.psm1"

function Format-SpectrePadded {
<#
.SYNOPSIS
Renders a collection of renderables in rows to the console.
.DESCRIPTION
This function creates a spectre rows widget that renders a collection of renderables in autosized rows to the console.
Rows can contain renderable items, see https://spectreconsole.net/widgets/rows for more information.
.PARAMETER Data
An array of objects containing the data to be displayed in the rows.
#>
[Reflection.AssemblyMetadata("title", "Format-SpectrePadded")]
# two parameter sets, one for padding evenly and one for specifing tlbr separately
param (
[Parameter(ValueFromPipeline, Mandatory)]
[RenderableTransformationAttribute()]
[object] $Data,
[Parameter(ParameterSetName = 'Global', Mandatory)]
[int] $Padding,
[Parameter(ParameterSetName = 'Specific', Mandatory)]
[int] $Top,
[Parameter(ParameterSetName = 'Specific', Mandatory)]
[int] $Left,
[Parameter(ParameterSetName = 'Specific', Mandatory)]
[int] $Bottom,
[Parameter(ParameterSetName = 'Specific', Mandatory)]
[int] $Right,
[Parameter(ParameterSetName = 'Expand', Mandatory)]
[switch] $Expand
)

$paddedRenderable = [Spectre.Console.Padder]::new($Data)

if ($PSCmdlet.ParameterSetName -eq 'Expand') {
$paddedRenderable.Expand = $true
} elseif ($PSCmdlet.ParameterSetName -eq 'Global') {
$paddedRenderable.Padding = [Spectre.Console.Padding]::new($Padding)
} elseif ($PSCmdlet.ParameterSetName -eq 'Specific') {
$paddedRenderable.Padding = [Spectre.Console.Padding]::new($Left, $Top, $Right, $Bottom)
}

return $paddedRenderable
}
3 changes: 1 addition & 2 deletions PwshSpectreConsole/public/formatting/Format-SpectreTable.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ function Format-SpectreTable {
[int] $Width,
[switch] $HideHeaders,
[String] $Title,
[switch] $AllowMarkup,
[switch] $PassThru
[switch] $AllowMarkup
)
begin {
Write-Debug "Module: $($ExecutionContext.SessionState.Module.Name) Command: $($MyInvocation.MyCommand.Name) Param: $($PSBoundParameters.GetEnumerator())"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using module "..\..\private\completions\Completers.psm1"
using module "..\..\private\completions\Transformers.psm1"

function Format-SpectrePath {
function Format-SpectreTextPath {
<#
.SYNOPSIS
Formats a path into a Spectre Console Path which supports highlighting and truncating.
Expand All @@ -15,7 +15,7 @@ function Format-SpectrePath {
.EXAMPLE
TODO Example
#>
[Reflection.AssemblyMetadata("title", "Format-SpectrePath")]
[Reflection.AssemblyMetadata("title", "Format-SpectreTextPath")]
param(
[Parameter(ValueFromPipeline, Mandatory)]
[string] $Path,
Expand Down
23 changes: 23 additions & 0 deletions PwshSpectreConsole/public/formatting/New-SpectreGridRow.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<#
.SYNOPSIS
TODO - Add synopsis
.DESCRIPTION
TODO - Add description
#>
function New-SpectreGridRow {
[Reflection.AssemblyMetadata("title", "New-SpectreGridRow")]
param (
[Parameter(Mandatory)]
[array]$Data
)

$renderableColumns = @()
foreach ($column in $Data) {
$renderableColumns += ConvertTo-Renderable $column
}

$gridRow = [SpectreGridRow]::new($renderableColumns)

return $gridRow
}
Loading

0 comments on commit 31a3ffa

Please sign in to comment.