Skip to content

Commit

Permalink
Merge pull request #15 from trackd/main
Browse files Browse the repository at this point in the history
added Format-SpectreJson and refactor Format-SpectreTable
  • Loading branch information
ShaunLawrie authored Dec 28, 2023
2 parents 2c477e5 + 7490ff0 commit e7cd260
Show file tree
Hide file tree
Showing 14 changed files with 566 additions and 32 deletions.
2 changes: 1 addition & 1 deletion PwshSpectreConsole.Docs/UpdateDocs.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ if($null -eq $module) {

$module | Save-MarkdownHelp -OutputPath "$PSScriptRoot\src\content\docs\reference\" -IncludeYamlHeader -YamlHeaderInformationType Metadata -ExcludeFile "*.gif", "*.png"

$new = @("New-SpectreChartItem.md", "Get-SpectreDemoColors.md", "Get-SpectreDemoEmoji.md", "New-SpectreTreeItem.md")
$new = @("New-SpectreChartItem.md", "Get-SpectreDemoColors.md", "Get-SpectreDemoEmoji.md", "Format-SpectreJson.md")
$experimental = @("Get-SpectreImageExperimental.md", "Invoke-SpectreScriptBlockQuietly.md")

$newTag = @"
Expand Down
7 changes: 7 additions & 0 deletions PwshSpectreConsole/Build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ function Install-SpectreConsole {
Invoke-WebRequest "https://www.nuget.org/api/v2/package/SixLabors.ImageSharp/$imageSharpVersion" -OutFile $downloadLocation -UseBasicParsing
Expand-Archive $downloadLocation $libPath -Force
Remove-Item $downloadLocation

$libPath = Join-Path $InstallLocation "Spectre.Console.Json"
New-Item -Path $libPath -ItemType "Directory" -Force | Out-Null
$downloadLocation = Join-Path $libPath "download.zip"
Invoke-WebRequest "https://www.nuget.org/api/v2/package/Spectre.Console.Json/$Version" -OutFile $downloadLocation -UseBasicParsing
Expand-Archive $downloadLocation $libPath -Force
Remove-Item $downloadLocation
}

Write-Host "Downloading Spectre.Console version $Version"
Expand Down
9 changes: 5 additions & 4 deletions PwshSpectreConsole/PwshSpectreConsole.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'PwshSpectreConsole'

# Version number of this module.
ModuleVersion = '1.2.2'
ModuleVersion = '1.4.0'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down Expand Up @@ -57,7 +57,8 @@ PowerShellVersion = '7.0'
RequiredAssemblies =
'.\packages\Spectre.Console\lib\netstandard2.0\Spectre.Console.dll',
'.\packages\Spectre.Console.ImageSharp\lib\netstandard2.0\Spectre.Console.ImageSharp.dll',
'.\packages\SixLabors.ImageSharp\lib\netstandard2.0\SixLabors.ImageSharp.dll'
'.\packages\SixLabors.ImageSharp\lib\netstandard2.0\SixLabors.ImageSharp.dll',
'.\packages\Spectre.Console.Json\lib\netstandard2.0\Spectre.Console.Json.dll'

# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @()
Expand All @@ -83,7 +84,7 @@ FunctionsToExport = 'Add-SpectreJob', 'Format-SpectreBarChart',
'Start-SpectreDemo', 'Wait-SpectreJobs', 'Write-SpectreFigletText',
'Write-SpectreHost', 'Write-SpectreRule', 'Read-SpectreConfirm',
'New-SpectreChartItem', 'Invoke-SpectreScriptBlockQuietly',
'Get-SpectreDemoColors', 'Get-SpectreDemoEmoji'
'Get-SpectreDemoColors', 'Get-SpectreDemoEmoji', 'Format-SpectreJson'

# 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 All @@ -92,7 +93,7 @@ CmdletsToExport = @()
VariablesToExport = '*'

# Aliases 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 aliases to export.
AliasesToExport = @()
AliasesToExport = 'fst', 'fsj'

# DSC resources to export from this module
# DscResourcesToExport = @()
Expand Down
5 changes: 5 additions & 0 deletions PwshSpectreConsole/private/Add-PwshSpectreConsole.VTCodes.ps1
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')
}
}
6 changes: 5 additions & 1 deletion PwshSpectreConsole/private/Convert-ToSpectreColor.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ function Convert-ToSpectreColor {
param (
[Parameter(ValueFromPipeline, Mandatory)]
[ValidateSpectreColor()]
[string] $Color
[object] $Color
)
try {
# Just return the console color object
if($Color -is [Spectre.Console.Color]) {
return $Color
}
# Already validated in validation attribute
if($Color.StartsWith("#")) {
$hexString = $Color -replace '^#', ''
Expand Down
52 changes: 52 additions & 0 deletions PwshSpectreConsole/private/ConvertTo-SpectreDecoration.ps1
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))
}
71 changes: 71 additions & 0 deletions PwshSpectreConsole/private/Get-DefaultDisplayMembers.ps1
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
}
}
}
2 changes: 1 addition & 1 deletion PwshSpectreConsole/private/Get-HostWidth.ps1
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
}
Loading

0 comments on commit e7cd260

Please sign in to comment.