From efe095842b4d29469312d46f7d769cafb3fb9eba Mon Sep 17 00:00:00 2001 From: Shaun Lawrie Date: Thu, 22 Feb 2024 02:41:52 +1300 Subject: [PATCH] Add the table colors for fmotion and don't publish test dll --- .gitignore | 1 + .../@init/Start-SpectreDemo.tests.ps1 | 4 ++++ PwshSpectreConsole/Build.ps1 | 15 ++++++++++----- PwshSpectreConsole/PwshSpectreConsole.psd1 | 3 +-- PwshSpectreConsole/PwshSpectreConsole.psm1 | 4 ++-- PwshSpectreConsole/private/Add-TableColumns.ps1 | 7 ++++--- PwshSpectreConsole/private/New-TableCell.ps1 | 13 +++++++------ PwshSpectreConsole/private/New-TableRow.ps1 | 7 ++++--- .../public/config/Set-SpectreColors.ps1 | 4 ++-- .../public/formatting/Format-SpectreTable.ps1 | 14 ++++++++++---- 10 files changed, 45 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index d19a6b65..f0ca34b6 100644 --- a/.gitignore +++ b/.gitignore @@ -23,6 +23,7 @@ pnpm-debug.log* # Exclude packages folder PwshSpectreConsole/packages +PwshSpectreConsole.Tests/packages # Exclude working folder Working/ diff --git a/PwshSpectreConsole.Tests/@init/Start-SpectreDemo.tests.ps1 b/PwshSpectreConsole.Tests/@init/Start-SpectreDemo.tests.ps1 index b50f79e7..00323695 100644 --- a/PwshSpectreConsole.Tests/@init/Start-SpectreDemo.tests.ps1 +++ b/PwshSpectreConsole.Tests/@init/Start-SpectreDemo.tests.ps1 @@ -8,6 +8,10 @@ try { Import-Module "$PSScriptRoot\..\..\PwshSpectreConsole\PwshSpectreConsole.psd1" -Force +if (-not ([System.AppDomain]::CurrentDomain.GetAssemblies().FullName | Where-Object { $_ -like "*Spectre.Console.Testing*" })) { + Add-Type -Path "$PSScriptRoot\..\packages\Spectre.Console.Testing\lib\netstandard2.0\Spectre.Console.Testing.dll" +} + Describe "Start-SpectreDemo" { InModuleScope "PwshSpectreConsole" { diff --git a/PwshSpectreConsole/Build.ps1 b/PwshSpectreConsole/Build.ps1 index a46e66db..c0880fdd 100644 --- a/PwshSpectreConsole/Build.ps1 +++ b/PwshSpectreConsole/Build.ps1 @@ -5,22 +5,23 @@ param ( function Install-SpectreConsole { param ( [string] $InstallLocation, + [string] $TestingInstallLocation, [string] $Version ) New-Item -Path $InstallLocation -ItemType "Directory" -Force | Out-Null - $libPath = Join-Path $InstallLocation "Spectre.Console" + $libPath = Join-Path $TestingInstallLocation "Spectre.Console.Testing" 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/$Version" -OutFile $downloadLocation -UseBasicParsing + Invoke-WebRequest "https://www.nuget.org/api/v2/package/Spectre.Console.Testing/$Version" -OutFile $downloadLocation -UseBasicParsing Expand-Archive $downloadLocation $libPath -Force Remove-Item $downloadLocation - $libPath = Join-Path $InstallLocation "Spectre.Console.Testing" + $libPath = Join-Path $InstallLocation "Spectre.Console" 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.Testing/$Version" -OutFile $downloadLocation -UseBasicParsing + Invoke-WebRequest "https://www.nuget.org/api/v2/package/Spectre.Console/$Version" -OutFile $downloadLocation -UseBasicParsing Expand-Archive $downloadLocation $libPath -Force Remove-Item $downloadLocation @@ -52,7 +53,11 @@ function Install-SpectreConsole { Write-Host "Downloading Spectre.Console version $Version" $installLocation = (Join-Path $PSScriptRoot "packages") +$testingInstallLocation = (Join-Path $PSScriptRoot ".." "PwshSpectreConsole.Tests" "packages") if(Test-Path $installLocation) { Remove-Item $installLocation -Recurse -Force } -Install-SpectreConsole -InstallLocation $installLocation -Version $Version +if(Test-Path $testingInstallLocation) { + Remove-Item $testingInstallLocation -Recurse -Force +} +Install-SpectreConsole -InstallLocation $installLocation -TestingInstallLocation $testingInstallLocation -Version $Version diff --git a/PwshSpectreConsole/PwshSpectreConsole.psd1 b/PwshSpectreConsole/PwshSpectreConsole.psd1 index c007681f..725372ca 100644 --- a/PwshSpectreConsole/PwshSpectreConsole.psd1 +++ b/PwshSpectreConsole/PwshSpectreConsole.psd1 @@ -58,8 +58,7 @@ 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\Spectre.Console.Json\lib\netstandard2.0\Spectre.Console.Json.dll', - '.\packages\Spectre.Console.Testing\lib\netstandard2.0\Spectre.Console.Testing.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 = @() diff --git a/PwshSpectreConsole/PwshSpectreConsole.psm1 b/PwshSpectreConsole/PwshSpectreConsole.psm1 index 817303cc..7dcd18ce 100644 --- a/PwshSpectreConsole/PwshSpectreConsole.psm1 +++ b/PwshSpectreConsole/PwshSpectreConsole.psm1 @@ -3,8 +3,8 @@ using namespace Spectre.Console $script:AccentColor = [Color]::Blue $script:DefaultValueColor = [Color]::Grey -$script:DefaultTableHeaderColor = [Color]::Grey82 -$script:DefaultTableTextColor = [Color]::Grey39 +$script:DefaultTableHeaderColor = [Color]::Default +$script:DefaultTableTextColor = [Color]::Default foreach ($directory in @('private', 'public')) { Get-ChildItem -Path "$PSScriptRoot\$directory\*.ps1" -Recurse | ForEach-Object { diff --git a/PwshSpectreConsole/private/Add-TableColumns.ps1 b/PwshSpectreConsole/private/Add-TableColumns.ps1 index 3cc8404e..ef3a1290 100644 --- a/PwshSpectreConsole/private/Add-TableColumns.ps1 +++ b/PwshSpectreConsole/private/Add-TableColumns.ps1 @@ -7,6 +7,7 @@ function Add-TableColumns { [Table] $table, [Collections.Specialized.OrderedDictionary] $FormatData, [String] $Title, + [Color] $Color = [Color]::Default, [Switch] $Scalar, [Switch] $Wrap ) @@ -14,11 +15,11 @@ function Add-TableColumns { if ($Scalar) { if ($Title) { Write-Debug "Adding column with title: $Title" - $table.AddColumn($Title) | Out-Null + $table.AddColumn("[$($Color.ToMarkup())]$Title[/]") | Out-Null } else { Write-Debug "Adding column with title: Value" - $table.AddColumn("Value") | Out-Null + $table.AddColumn("[$($Color.ToMarkup())]Value[/]") | Out-Null } if (-Not $Wrap) { $table.Columns[-1].NoWrap = $true @@ -28,7 +29,7 @@ function Add-TableColumns { foreach ($key in $FormatData.keys) { $lookup = $FormatData[$key] Write-Debug "Adding column from formatdata: $($lookup.GetEnumerator())" - $table.AddColumn($lookup.Label) | Out-Null + $table.AddColumn("[$($Color.ToMarkup())]$($lookup.Label)[/]") | Out-Null $table.Columns[-1].Padding = [Padding]::new(1, 0, 1, 0) if ($lookup.width -gt 0) { # width 0 is autosize, select the last entry in the column list diff --git a/PwshSpectreConsole/private/New-TableCell.ps1 b/PwshSpectreConsole/private/New-TableCell.ps1 index b1c052f8..43c11c2b 100644 --- a/PwshSpectreConsole/private/New-TableCell.ps1 +++ b/PwshSpectreConsole/private/New-TableCell.ps1 @@ -4,28 +4,29 @@ function New-TableCell { [cmdletbinding()] param( [Object] $String, + [Color] $Color = [Color]::Default, [Switch] $AllowMarkup ) Write-Debug "Module: $($ExecutionContext.SessionState.Module.Name) Command: $($MyInvocation.MyCommand.Name) Param: $($PSBoundParameters.GetEnumerator())" if ([String]::IsNullOrEmpty($String)) { if ($AllowMarkup) { - return [Markup]::new(' ') + return [Markup]::new(' ', [Style]::new($Color)) } - return [Text]::new(' ') + return [Text]::new(' ', [Style]::new($Color)) } if (-Not [String]::IsNullOrEmpty($String.ToString())) { if ($AllowMarkup) { Write-Debug "New-TableCell ToString(), Markup, $($String.ToString())" - return [Markup]::new($String.ToString()) + return [Markup]::new($String.ToString(), [Style]::new($Color)) } Write-Debug "New-TableCell ToString(), Text, $($String.ToString())" - return [Text]::new($String.ToString()) + return [Text]::new($String.ToString(), [Style]::new($Color)) } # just coerce to string. if ($AllowMarkup) { Write-Debug "New-TableCell [String], markup, $([String]$String)" - return [Markup]::new([String]$String) + return [Markup]::new([String]$String, [Style]::new($Color)) } Write-Debug "New-TableCell [String], Text, $([String]$String)" - return [Text]::new([String]$String) + return [Text]::new([String]$String, [Style]::new($Color)) } diff --git a/PwshSpectreConsole/private/New-TableRow.ps1 b/PwshSpectreConsole/private/New-TableRow.ps1 index e7ba25fc..941c020c 100644 --- a/PwshSpectreConsole/private/New-TableRow.ps1 +++ b/PwshSpectreConsole/private/New-TableRow.ps1 @@ -2,6 +2,7 @@ function New-TableRow { param( [Parameter(Mandatory)] [Object] $Entry, + [Color] $Color = [Color]::Default, [Switch] $AllowMarkup, [Switch] $Scalar ) @@ -10,21 +11,21 @@ function New-TableRow { AllowMarkup = $AllowMarkup } if ($scalar) { - New-TableCell -String $Entry @opts + New-TableCell -String $Entry -Color $Color @opts } else { # simplified, should be faster. $detectVT = '\x1b' $rows = foreach ($cell in $Entry) { if ([String]::IsNullOrEmpty($cell)) { - New-TableCell @opts + New-TableCell -Color $Color @opts continue } if ($cell -match $detectVT) { ConvertTo-SpectreDecoration -String $cell @opts continue } - New-TableCell -String $cell @opts + New-TableCell -String $cell -Color $Color @opts } return $rows } diff --git a/PwshSpectreConsole/public/config/Set-SpectreColors.ps1 b/PwshSpectreConsole/public/config/Set-SpectreColors.ps1 index 6253cad5..a4a1f518 100644 --- a/PwshSpectreConsole/public/config/Set-SpectreColors.ps1 +++ b/PwshSpectreConsole/public/config/Set-SpectreColors.ps1 @@ -39,10 +39,10 @@ function Set-SpectreColors { [Color] $DefaultValueColor = "Grey", [ColorTransformationAttribute()] [ArgumentCompletionsSpectreColors()] - [Color] $DefaultTableHeaderColor = "Grey82", + [Color] $DefaultTableHeaderColor = [Color]::Default, [ColorTransformationAttribute()] [ArgumentCompletionsSpectreColors()] - [Color] $DefaultTableTextColor = "Grey39" + [Color] $DefaultTableTextColor = [Color]::Default ) $script:AccentColor = $AccentColor $script:DefaultValueColor = $DefaultValueColor diff --git a/PwshSpectreConsole/public/formatting/Format-SpectreTable.ps1 b/PwshSpectreConsole/public/formatting/Format-SpectreTable.ps1 index b252c433..0c78d93b 100644 --- a/PwshSpectreConsole/public/formatting/Format-SpectreTable.ps1 +++ b/PwshSpectreConsole/public/formatting/Format-SpectreTable.ps1 @@ -36,6 +36,9 @@ function Format-SpectreTable { .PARAMETER HeaderColor The color of the table header text. Default is the DefaultTableHeaderColor. + .PARAMETER TextColor + The color of the table text. Default is the DefaultTableTextColor. + .PARAMETER Width The width of the table. @@ -97,6 +100,9 @@ function Format-SpectreTable { [ColorTransformationAttribute()] [ArgumentCompletionsSpectreColors()] [Color] $HeaderColor = $script:DefaultTableHeaderColor, + [ColorTransformationAttribute()] + [ArgumentCompletionsSpectreColors()] + [Color] $TextColor = $script:DefaultTableTextColor, [ValidateScript({ $_ -gt 0 -and $_ -le (Get-HostWidth) }, ErrorMessage = "Value '{0}' is invalid. Cannot be negative or exceed console width.")] [int] $Width, [switch] $HideHeaders, @@ -144,24 +150,24 @@ function Format-SpectreTable { if (-Not $collector.shapeInfo) { # scalar array, no header $rowoptions.scalar = $tableoptions.scalar = $true - $table = Add-TableColumns -Table $table @tableoptions + $table = Add-TableColumns -Table $table @tableoptions -Color $HeaderColor } else { # grab the FormatStartData $Headers = Get-TableHeader $collector[0] if ($Headers) { - $table = Add-TableColumns -Table $table -formatData $Headers + $table = Add-TableColumns -Table $table -formatData $Headers -Color $HeaderColor } else { return } } foreach ($item in $collector.FormatEntryInfo) { if ($rowoptions.scalar) { - $row = New-TableRow -Entry $item.Text @rowoptions + $row = New-TableRow -Entry $item.Text -Color $TextColor @rowoptions } else { if ($null -eq $item.FormatPropertyFieldList.propertyValue) { continue } - $row = New-TableRow -Entry $item.FormatPropertyFieldList.propertyValue @rowoptions + $row = New-TableRow -Entry $item.FormatPropertyFieldList.propertyValue -Color $TextColor @rowoptions } if ($AllowMarkup) { $table = [TableExtensions]::AddRow($table, [Markup[]]$row)