Skip to content

Instantly share code, notes, and snippets.

@joerodgers
Last active September 15, 2023 07:45
Show Gist options
  • Save joerodgers/c08a911e545575d382b7d0625699ab08 to your computer and use it in GitHub Desktop.
Save joerodgers/c08a911e545575d382b7d0625699ab08 to your computer and use it in GitHub Desktop.
Set-Location -Path "C:\_temp\Spectre.Console.PoC"
$packages = @(
"System.Buffers",
"System.Numerics.Vectors",
"System.Runtime.CompilerServices.Unsafe",
"System.Memory",
"Spectre.Console"
)
foreach( $package in $packages )
{
if( -not (Get-Package -Name $package -Destination "." ) )
{
Install-Package -Name $package -SkipDependencies -Force -Destination "."
}
}
if( $PSEdition -ne "Core" )
{
# resolve assembly binding failures on .net framework
$handler = [System.ResolveEventHandler] {
param
(
$sender,
$e
)
foreach( $package in $packages )
{
if( $e.Name -match $package )
{
$path = Resolve-Path -Path ".\$package.*\lib\netstandard2.0\$package.dll"
return [System.Reflection.Assembly]::LoadFrom($path)
}
}
foreach($a in [System.AppDomain]::CurrentDomain.GetAssemblies())
{
if ($a.FullName -eq $e.Name)
{
return $a
}
}
return $null
}
[System.AppDomain]::CurrentDomain.add_AssemblyResolve($handler)
$path = Resolve-Path -Path ".\Spectre.Console.*\lib\netstandard2.0\Spectre.Console.dll"
$null = [System.Reflection.Assembly]::LoadFrom($path)
}
else
{
foreach( $package in $packages )
{
$path = Resolve-Path -Path ".\$package.*\lib\netstandard2.0\$package.dll"
$null = [System.Reflection.Assembly]::LoadFrom($path)
}
}
$pass = [Spectre.Console.Style]::new( [Spectre.Console.Color]::Green, $null, [Spectre.Console.Decoration]::Bold, "")
$fail1 = [Spectre.Console.Style]::new( [Spectre.Console.Color]::Red, $null, [Spectre.Console.Decoration]::Italic, "")
$fail2 = [Spectre.Console.Style]::new( [Spectre.Console.Color]::Black, [Spectre.Console.Color]::Red, [Spectre.Console.Decoration]::Italic, "")
$fail3 = [Spectre.Console.Style]::new( [Spectre.Console.Color]::Red, $null, [Spectre.Console.Decoration]::Underline, "")
$table = New-Object Spectre.Console.Table
$table.AddColumn([Spectre.Console.TableColumn]::new( "Site" ))
$table.AddColumn([Spectre.Console.TableColumn]::new( "Library" ))
$table.AddColumn([Spectre.Console.TableColumn]::new( "Server Count" ))
$table.AddColumn([Spectre.Console.TableColumn]::new( "Cloud Count" ))
$table.AddColumn([Spectre.Console.TableColumn]::new( "Result" ))
$rows = New-Object System.Collections.Generic.List[Spectre.Console.Rendering.IRenderable]
$rows.Add( [Spectre.Console.Text]::new( "sites/finance" ))
$rows.Add( [Spectre.Console.Text]::new( "Documents" ))
$rows.Add( [Spectre.Console.Text]::new( "100" ))
$rows.Add( [Spectre.Console.Text]::new( "100" ))
$rows.Add( [Spectre.Console.Text]::new( "PASS", $pass ))
$table.Rows.Add($rows)
$rows = New-Object System.Collections.Generic.List[Spectre.Console.Rendering.IRenderable]
$rows.Add( [Spectre.Console.Text]::new( "sites/finance" ))
$rows.Add( [Spectre.Console.Text]::new( "Documents" ))
$rows.Add( [Spectre.Console.Text]::new( "100" ))
$rows.Add( [Spectre.Console.Text]::new( "100" ))
$rows.Add( [Spectre.Console.Text]::new( "PASS", $pass ))
$table.Rows.Add($rows)
$rows = New-Object System.Collections.Generic.List[Spectre.Console.Rendering.IRenderable]
$rows.Add( [Spectre.Console.Text]::new( "sites/finance" ))
$rows.Add( [Spectre.Console.Text]::new( "Documents" ))
$rows.Add( [Spectre.Console.Text]::new( "100" ))
$rows.Add( [Spectre.Console.Text]::new( "100" ))
$rows.Add( [Spectre.Console.Text]::new( "PASS", $pass ))
$table.Rows.Add($rows)
$rows = New-Object System.Collections.Generic.List[Spectre.Console.Rendering.IRenderable]
$rows.Add( [Spectre.Console.Text]::new( "sites/finance" ))
$rows.Add( [Spectre.Console.Text]::new( "Documents" ))
$rows.Add( [Spectre.Console.Text]::new( "100" ))
$rows.Add( [Spectre.Console.Text]::new( "97" ))
$rows.Add( [Spectre.Console.Text]::new( "FAIL", $fail1 ))
$table.Rows.Add($rows)
$rows = New-Object System.Collections.Generic.List[Spectre.Console.Rendering.IRenderable]
$rows.Add( [Spectre.Console.Text]::new( "sites/finance" ))
$rows.Add( [Spectre.Console.Text]::new( "Documents" ))
$rows.Add( [Spectre.Console.Text]::new( "100" ))
$rows.Add( [Spectre.Console.Text]::new( "97" ))
$rows.Add( [Spectre.Console.Text]::new( "FAIL", $fail2 ))
$table.Rows.Add($rows)
$rows = New-Object System.Collections.Generic.List[Spectre.Console.Rendering.IRenderable]
$rows.Add( [Spectre.Console.Text]::new( "sites/finance" ))
$rows.Add( [Spectre.Console.Text]::new( "Documents" ))
$rows.Add( [Spectre.Console.Text]::new( "100" ))
$rows.Add( [Spectre.Console.Text]::new( "97" ))
$rows.Add( [Spectre.Console.Text]::new( "FAIL", $fail3 ))
$table.Rows.Add($rows)
[Spectre.Console.AnsiConsole]::Write($table)
if( $handler )
{
[System.AppDomain]::CurrentDomain.remove_AssemblyResolve($handler)
}
Import-Module -Name ".\SpectreConsoleTable.psm1"
$table = New-SpectreConsoleTable
New-SpectreConsoleTableColumn -Table $table -Name "Site", "Library", "Server Count", "Cloud Count", "Result"
$pass = New-SpectreConsoleStyle -Result Pass
$fail1 = New-SpectreConsoleStyle -Result Fail1
$fail2 = New-SpectreConsoleStyle -Result Fail2
$fail3 = New-SpectreConsoleStyle -Result Fail3
$rows = @()
$rows += New-SpectreConsoleTableRow -Text "sites/finance"
$rows += New-SpectreConsoleTableRow -Text "Documents 1"
$rows += New-SpectreConsoleTableRow -Text "100"
$rows += New-SpectreConsoleTableRow -Text "100"
$rows += New-SpectreConsoleTableRow -Text "Pass" -Style $pass
Add-SpectreConsoleTableRows -Table $table -Row $rows
$rows = @()
$rows += New-SpectreConsoleTableRow -Text "sites/finance"
$rows += New-SpectreConsoleTableRow -Text "Documents 2"
$rows += New-SpectreConsoleTableRow -Text "100"
$rows += New-SpectreConsoleTableRow -Text "98"
$rows += New-SpectreConsoleTableRow -Text "Pass" -Style $fail1
Add-SpectreConsoleTableRows -Table $table -Row $rows
$rows = @()
$rows += New-SpectreConsoleTableRow -Text "sites/finance"
$rows += New-SpectreConsoleTableRow -Text "Documents 3 "
$rows += New-SpectreConsoleTableRow -Text "100"
$rows += New-SpectreConsoleTableRow -Text "50"
$rows += New-SpectreConsoleTableRow -Text "Pass" -Style $fail2
Add-SpectreConsoleTableRows -Table $table -Row $rows
$rows = @()
$rows += New-SpectreConsoleTableRow -Text "sites/finance"
$rows += New-SpectreConsoleTableRow -Text "Documents 4"
$rows += New-SpectreConsoleTableRow -Text "100"
$rows += New-SpectreConsoleTableRow -Text "90"
$rows += New-SpectreConsoleTableRow -Text "Pass" -Style $fail3
Add-SpectreConsoleTableRows -Table $table -Row $rows
Write-SpectreConsoleTable -Table $table
function New-SpectreConsoleTable
{
return New-Object Spectre.Console.Table
}
function New-SpectreConsoleTableColumn
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true)]
[Spectre.Console.Table]
$Table,
[Parameter(Mandatory=$true)]
[string[]]
$Name
)
foreach( $n in $name )
{
$column = [Spectre.Console.TableColumn]::new( $n )
$table.AddColumn( $column )
}
}
function New-SpectreConsoleTableRow
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true)]
[string]
$Text,
[Parameter(Mandatory=$false)]
[Spectre.Console.Style]
$Style
)
if( $PSBoundParameters.ContainsKey( "Style" ) )
{
[Spectre.Console.Text]::new( $Text, $Style)
}
else
{
[Spectre.Console.Text]::new( $Text )
}
}
function Add-SpectreConsoleTableRows
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true)]
[Spectre.Console.Table]
$Table,
[Parameter(Mandatory=$true)]
[Spectre.Console.Rendering.IRenderable[]]
$Row
)
$rows = New-Object System.Collections.Generic.List[Spectre.Console.Rendering.IRenderable]
foreach( $r in $Row )
{
$rows.Add($r)
}
$Table.Rows.Add($rows)
}
function New-SpectreConsoleStyle
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true)]
[ValidateSet("Pass", "Fail1", "Fail2", "Fail3")]
[string]
$Result
)
switch( $Result )
{
"Fail1"
{
return [Spectre.Console.Style]::new( [Spectre.Console.Color]::Red, $null, [Spectre.Console.Decoration]::Italic, "")
}
"Fail2"
{
return [Spectre.Console.Style]::new( [Spectre.Console.Color]::Black, [Spectre.Console.Color]::Red, [Spectre.Console.Decoration]::Italic, "")
}
"Fail3"
{
return [Spectre.Console.Style]::new( [Spectre.Console.Color]::Red, $null, [Spectre.Console.Decoration]::Underline, "")
}
default
{
return [Spectre.Console.Style]::new( [Spectre.Console.Color]::Green, $null, [Spectre.Console.Decoration]::Bold, "")
}
}
}
function Write-SpectreConsoleTable
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true)]
[Spectre.Console.Table]
$Table
)
[Spectre.Console.AnsiConsole]::Write($Table)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment