Skip to content

Commit

Permalink
Add the table colors for fmotion and don't publish test dll
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaunLawrie committed Feb 21, 2024
1 parent 3555e8e commit efe0958
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pnpm-debug.log*

# Exclude packages folder
PwshSpectreConsole/packages
PwshSpectreConsole.Tests/packages

# Exclude working folder
Working/
4 changes: 4 additions & 0 deletions PwshSpectreConsole.Tests/@init/Start-SpectreDemo.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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" {

Expand Down
15 changes: 10 additions & 5 deletions PwshSpectreConsole/Build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
3 changes: 1 addition & 2 deletions PwshSpectreConsole/PwshSpectreConsole.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -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 = @()
Expand Down
4 changes: 2 additions & 2 deletions PwshSpectreConsole/PwshSpectreConsole.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions PwshSpectreConsole/private/Add-TableColumns.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ function Add-TableColumns {
[Table] $table,
[Collections.Specialized.OrderedDictionary] $FormatData,
[String] $Title,
[Color] $Color = [Color]::Default,
[Switch] $Scalar,
[Switch] $Wrap
)
Write-Debug "Module: $($ExecutionContext.SessionState.Module.Name) Command: $($MyInvocation.MyCommand.Name) Param: $($PSBoundParameters.GetEnumerator())"
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
Expand All @@ -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
Expand Down
13 changes: 7 additions & 6 deletions PwshSpectreConsole/private/New-TableCell.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
7 changes: 4 additions & 3 deletions PwshSpectreConsole/private/New-TableRow.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ function New-TableRow {
param(
[Parameter(Mandatory)]
[Object] $Entry,
[Color] $Color = [Color]::Default,
[Switch] $AllowMarkup,
[Switch] $Scalar
)
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions PwshSpectreConsole/public/config/Set-SpectreColors.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 10 additions & 4 deletions PwshSpectreConsole/public/formatting/Format-SpectreTable.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit efe0958

Please sign in to comment.