-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from trackd/main
added Format-SpectreJson and refactor Format-SpectreTable
- Loading branch information
Showing
14 changed files
with
566 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
PwshSpectreConsole/private/Add-PwshSpectreConsole.VTCodes.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
function Add-PwshSpectreConsole.VTCodes { | ||
if (-Not ('PwshSpectreConsole.VTCodes.Parser' -as [type])) { | ||
Add-Type -Path (Join-Path $PSScriptRoot classes 'PwshSpectreConsole.VTCodes.cs') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
PwshSpectreConsole/private/ConvertTo-SpectreDecoration.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
function ConvertTo-SpectreDecoration { | ||
param( | ||
[Parameter(Mandatory)] | ||
[String]$String, | ||
[switch]$AllowMarkup | ||
) | ||
if (-Not ('PwshSpectreConsole.VTCodes.Parser' -as [type])) { | ||
Add-PwshSpectreConsole.VTCodes | ||
} | ||
Write-Debug "ANSI String: $String '$($String -replace '\x1B','e')'" | ||
$lookup = [PwshSpectreConsole.VTCodes.Parser]::Parse($String) | ||
$ht = @{} | ||
foreach ($item in $lookup) { | ||
if ($item.value -eq 'reset') { | ||
continue | ||
} | ||
$conversion = switch ($item.type) { | ||
'4bit' { | ||
if ($item.value -gt 0 -and $item.value -le 15) { | ||
[Spectre.Console.Color]::FromConsoleColor($item.value) | ||
} | ||
else { | ||
[Spectre.Console.Color]::FromInt32($item.value) | ||
} | ||
} | ||
'8bit' { | ||
[Spectre.Console.Color]::FromInt32($item.value) | ||
} | ||
'24bit' { | ||
[Spectre.Console.Color]::new($item.value.Red, $item.value.Green, $item.value.Blue) | ||
} | ||
'decoration' { | ||
[Spectre.Console.Decoration]::Parse([Spectre.Console.Decoration], $item.Value, $true) | ||
} | ||
} | ||
if ($item.type -eq 'decoration') { | ||
$ht.decoration = $conversion | ||
} | ||
if ($item.position -eq 'foreground') { | ||
$ht.fg = $conversion | ||
} | ||
elseif ($item.position -eq 'background') { | ||
$ht.bg = $conversion | ||
} | ||
} | ||
$String = $String -replace '\x1B\[[0-?]*[ -/]*[@-~]' | ||
Write-Debug "Clean: '$String' deco: '$($ht.decoration)' fg: '$($ht.fg)' bg: '$($ht.bg)'" | ||
if($AllowMarkup) { | ||
return [Spectre.Console.Markup]::new($String,[Spectre.Console.Style]::new($ht.fg,$ht.bg,$ht.decoration)) | ||
} | ||
[Spectre.Console.Text]::new($String,[Spectre.Console.Style]::new($ht.fg,$ht.bg,$ht.decoration)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
function Get-DefaultDisplayMembers { | ||
<# | ||
.SYNOPSIS | ||
Get the default display members for an object using the formatdata. | ||
.NOTES | ||
rewrite, borrowed some code from chrisdents gist. | ||
.LINK | ||
https://raw.githubusercontent.com/PowerShell/GraphicalTools/master/src/Microsoft.PowerShell.ConsoleGuiTools/TypeGetter.cs | ||
https://gist.github.com/indented-automation/834284b6c904339b0454199b4745237e | ||
#> | ||
param( | ||
[Parameter(Mandatory, ValueFromPipeline)] | ||
[Object]$Object | ||
) | ||
try { | ||
Write-Debug "getting formatdata for $($Object[0].PSTypeNames)" | ||
$formatData = Get-FormatData -TypeName $Object[0].PSTypeNames | Select-Object -First 1 | ||
Write-Debug "formatData: $($formatData.count)" | ||
} | ||
catch { | ||
# no formatdata found | ||
return $null | ||
} | ||
if ($formatData) { | ||
$properties = [ordered]@{} | ||
$labels = @{} | ||
# $regex = [regex]::New('(?x)\$_\.(?<Property>[^\s,]+)') | ||
$viewDefinition = $formatData.FormatViewDefinition | Where-Object { $_.Control -match 'TableControl' } | Select-Object -First 1 | ||
Write-Debug "viewDefinition: $($viewDefinition.Name)" | ||
$format = for ($i = 0; $i -lt $viewDefinition.Control.Headers.Count; $i++) { | ||
$name = $viewDefinition.Control.Headers[$i].Label | ||
$displayEntry = $viewDefinition.Control.Rows.Columns[$i].DisplayEntry | ||
if (-not $name) { | ||
$name = $displayEntry.Value | ||
} | ||
if ($labels.ContainsKey($name)) { | ||
Write-Debug 'duplicate label found' | ||
# im not sure why this is needed, but for filesystem we get both 'Mode' and 'ModeWithoutHardLink' with "label" Mode. | ||
continue | ||
} | ||
$labels[$name] = $true | ||
switch ($displayEntry.ValueType) { | ||
'Property' { | ||
$expression = $displayEntry.Value | ||
# $property = $displayEntry.Value | ||
} | ||
'ScriptBlock' { | ||
$expression = [ScriptBlock]::Create($displayEntry.Value) | ||
# $property = $regex.matches($displayEntry.Value).foreach({ $_.Groups['Property'].Value }) | Select-Object -Unique | ||
} | ||
} | ||
$properties[$name] = @{ | ||
Label = $name | ||
Width = $viewDefinition.Control.headers[$i].width | ||
Alignment = $viewDefinition.Control.headers[$i].alignment | ||
# Property = $property | ||
# Expression = $expression | ||
# PropertyType = $Object.PSObject.Properties[$property].TypeNameOfValue | ||
# Type = $displayEntry.ValueType | ||
} | ||
@{ Name = $name; Expression = $expression } | ||
} | ||
# we still need the properties to create the columns, but this function can be simplified. | ||
# temporarily leaving it commented out for testing. | ||
return [PSCustomObject]@{ | ||
Properties = $properties | ||
Format = $format | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Required for unit test mocking | ||
function Get-HostWidth { | ||
return $Host.UI.RawUI.Width | ||
return $Host.UI.RawUI.BufferSize.Width | ||
} |
Oops, something went wrong.