Skip to content

Commit

Permalink
Fully qualify .net classes, the using statements make them shorter bu…
Browse files Browse the repository at this point in the history
…t you can't copy paste chunks for testing easily. Add format-spectrerows
  • Loading branch information
ShaunLawrie committed Aug 12, 2024
1 parent 5314161 commit dca82c1
Show file tree
Hide file tree
Showing 46 changed files with 663 additions and 603 deletions.
12 changes: 5 additions & 7 deletions PwshSpectreConsole.Tests/TestHelpers.psm1
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using namespace Spectre.Console

function Get-RandomColor {
$type = 1 # Get-Random -Minimum 0 -Maximum 2
switch ($type) {
Expand Down Expand Up @@ -53,30 +51,30 @@ function Get-RandomBoxBorder {
param (
[switch] $MustNotBeNone
)
$lookup = [BoxBorder] | Get-Member -Static -MemberType Properties | Select-Object -ExpandProperty Name
$lookup = [Spectre.Console.BoxBorder] | Get-Member -Static -MemberType Properties | Select-Object -ExpandProperty Name
if ($MustNotBeNone) {
$lookup = $lookup | Where-Object { $_ -ne 'None' }
}
return $lookup[$(Get-Random -Minimum 0 -Maximum $lookup.Count)]
}

function Get-RandomJustify {
$lookup = [Justify].GetEnumNames()
$lookup = [Spectre.Console.Justify].GetEnumNames()
return $lookup[$(Get-Random -Minimum 0 -Maximum $lookup.Count)]
}

function Get-RandomSpinner {
$lookup = [Spinner+Known] | Get-Member -Static -MemberType Properties | Select-Object -ExpandProperty Name
$lookup = [Spectre.Console.Spinner+Known] | Get-Member -Static -MemberType Properties | Select-Object -ExpandProperty Name
return $lookup[$(Get-Random -Minimum 0 -Maximum $lookup.Count)]
}

function Get-RandomTreeGuide {
$lookup = [TreeGuide] | Get-Member -Static -MemberType Properties | Select-Object -ExpandProperty Name
$lookup = [Spectre.Console.TreeGuide] | Get-Member -Static -MemberType Properties | Select-Object -ExpandProperty Name
return $lookup[$(Get-Random -Minimum 0 -Maximum $lookup.Count)]
}

function Get-RandomTableBorder {
$lookup = [TableBorder] | Get-Member -Static -MemberType Properties | Select-Object -ExpandProperty Name
$lookup = [Spectre.Console.TableBorder] | Get-Member -Static -MemberType Properties | Select-Object -ExpandProperty Name
return $lookup[$(Get-Random -Minimum 0 -Maximum $lookup.Count)]
}

Expand Down
90 changes: 45 additions & 45 deletions PwshSpectreConsole.Tests/writing/Write-SpectreHost.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
Remove-Module PwshSpectreConsole -Force -ErrorAction SilentlyContinue
Import-Module "$PSScriptRoot\..\..\PwshSpectreConsole\PwshSpectreConsole.psd1" -Force
Import-Module "$PSScriptRoot\..\TestHelpers.psm1" -Force

Describe "Write-SpectreHost" {
InModuleScope "PwshSpectreConsole" {
BeforeEach {
$testConsole = [Spectre.Console.Testing.TestConsole]::new()
$testConsole.EmitAnsiSequences = $true
$testMessage = Get-RandomString
$testMessage | Out-Null
Mock Write-SpectreHostInternalMarkup {
$Message | Should -Be $testMessage
[AnsiConsoleExtensions]::Markup($testConsole, $Message)
}
Mock Write-SpectreHostInternalMarkupLine {
$Message | Should -Be $testMessage
[AnsiConsoleExtensions]::MarkupLine($testConsole, $Message)
}
}

It "writes a message" {
Write-SpectreHost -Message $testMessage
Assert-MockCalled -CommandName "Write-SpectreHostInternalMarkupLine" -Times 1 -Exactly
$testConsole.Output.Split("`n").Count | Should -Be 2
}

It "accepts pipeline input" {
$testMessage | Write-SpectreHost
Assert-MockCalled -CommandName "Write-SpectreHostInternalMarkupLine" -Times 1 -Exactly
$testConsole.Output.Split("`n").Count | Should -Be 2
}

It "handles nonewline" {
Write-SpectreHost -Message $testMessage -NoNewline
Assert-MockCalled -CommandName "Write-SpectreHostInternalMarkup" -Times 1 -Exactly
$testConsole.Output.Split("`n").Count | Should -Be 1
}

It "Should match the snapshot" {
$testMessage = "[#00ff00]Hello[/], [DeepSkyBlue3_1]World![/] :smiling_face_with_sunglasses: Yay!"
Write-SpectreHost $testMessage
{ Assert-OutputMatchesSnapshot -SnapshotName "Write-SpectreHost" -Output $testConsole.Output } | Should -Not -Throw
}
}
Remove-Module PwshSpectreConsole -Force -ErrorAction SilentlyContinue
Import-Module "$PSScriptRoot\..\..\PwshSpectreConsole\PwshSpectreConsole.psd1" -Force
Import-Module "$PSScriptRoot\..\TestHelpers.psm1" -Force

Describe "Write-SpectreHost" {
InModuleScope "PwshSpectreConsole" {
BeforeEach {
$testConsole = [Spectre.Console.Testing.TestConsole]::new()
$testConsole.EmitAnsiSequences = $true
$testMessage = Get-RandomString
$testMessage | Out-Null
Mock Write-SpectreHostInternalMarkup {
$Message | Should -Be $testMessage
[Spectre.Console.AnsiConsoleExtensions]::Markup($testConsole, $Message)
}
Mock Write-SpectreHostInternalMarkupLine {
$Message | Should -Be $testMessage
[Spectre.Console.AnsiConsoleExtensions]::MarkupLine($testConsole, $Message)
}
}

It "writes a message" {
Write-SpectreHost -Message $testMessage
Assert-MockCalled -CommandName "Write-SpectreHostInternalMarkupLine" -Times 1 -Exactly
$testConsole.Output.Split("`n").Count | Should -Be 2
}

It "accepts pipeline input" {
$testMessage | Write-SpectreHost
Assert-MockCalled -CommandName "Write-SpectreHostInternalMarkupLine" -Times 1 -Exactly
$testConsole.Output.Split("`n").Count | Should -Be 2
}

It "handles nonewline" {
Write-SpectreHost -Message $testMessage -NoNewline
Assert-MockCalled -CommandName "Write-SpectreHostInternalMarkup" -Times 1 -Exactly
$testConsole.Output.Split("`n").Count | Should -Be 1
}

It "Should match the snapshot" {
$testMessage = "[#00ff00]Hello[/], [DeepSkyBlue3_1]World![/] :smiling_face_with_sunglasses: Yay!"
Write-SpectreHost $testMessage
{ Assert-OutputMatchesSnapshot -SnapshotName "Write-SpectreHost" -Output $testConsole.Output } | Should -Not -Throw
}
}
}
4 changes: 2 additions & 2 deletions PwshSpectreConsole/PwshSpectreConsole.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ FunctionsToExport = 'Add-SpectreJob', 'Format-SpectreBarChart',
'Write-SpectreHost', 'Write-SpectreRule', 'Read-SpectreConfirm',
'New-SpectreChartItem', 'Invoke-SpectreScriptBlockQuietly',
'Get-SpectreDemoColors', 'Get-SpectreDemoEmoji', 'Format-SpectreJson',
'Write-SpectreCalendar', 'Start-SpectreRecording',
'Stop-SpectreRecording', 'Format-SpectreColumns', 'Write-AnsiConsole',
'Write-SpectreCalendar', 'Start-SpectreRecording', 'Stop-SpectreRecording',
'Format-SpectreColumns', 'Format-SpectreRows',
'Out-SpectreHost'

# 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.
Expand Down
9 changes: 4 additions & 5 deletions PwshSpectreConsole/PwshSpectreConsole.psm1
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using module ".\private\completions\Completers.psm1"
using namespace Spectre.Console

$script:AccentColor = [Color]::Blue
$script:DefaultValueColor = [Color]::Grey
$script:DefaultTableHeaderColor = [Color]::Default
$script:DefaultTableTextColor = [Color]::Default
$script:AccentColor = [Spectre.Console.Color]::Blue
$script:DefaultValueColor = [Spectre.Console.Color]::Grey
$script:DefaultTableHeaderColor = [Spectre.Console.Color]::Default
$script:DefaultTableTextColor = [Spectre.Console.Color]::Default

foreach ($directory in @('private', 'public')) {
Get-ChildItem -Path "$PSScriptRoot\$directory\*.ps1" -Recurse | ForEach-Object {
Expand Down
5 changes: 2 additions & 3 deletions PwshSpectreConsole/private/Add-SpectreTreeNode.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using namespace Spectre.Console

<#
.SYNOPSIS
Expand All @@ -19,7 +18,7 @@ See Format-SpectreTree for usage.
function Add-SpectreTreeNode {
param (
[Parameter(Mandatory)]
[IHasTreeNodes] $Node,
[Spectre.Console.IHasTreeNodes] $Node,
[Parameter(Mandatory)]
[array] $Children
)
Expand All @@ -32,7 +31,7 @@ function Add-SpectreTreeNode {
$child.Remove("Label")
}

$newNode = [HasTreeNodeExtensions]::AddNode($Node, $child.Value)
$newNode = [Spectre.Console.HasTreeNodeExtensions]::AddNode($Node, $child.Value)
if ($child.Children.Count -gt 0) {
Add-SpectreTreeNode -Node $newNode -Children $child.Children
}
Expand Down
11 changes: 5 additions & 6 deletions PwshSpectreConsole/private/Add-TableColumns.ps1
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using namespace Spectre.Console


function Add-TableColumns {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[Table] $table,
[Spectre.Console.Table] $table,
[Collections.Specialized.OrderedDictionary] $FormatData,
[String] $Title,
[Color] $Color = [Color]::Default,
[Spectre.Console.Color] $Color = [Spectre.Console.Color]::Default,
[Switch] $Scalar,
[Switch] $Wrap
)
Expand All @@ -28,13 +27,13 @@ function Add-TableColumns {
$lookup = $FormatData[$key]
Write-Debug "Adding column from formatdata: $($lookup.GetEnumerator())"
$table.AddColumn("[$($Color.ToMarkup())]$($lookup.Label)[/]") | Out-Null
$table.Columns[-1].Padding = [Padding]::new(1, 0, 1, 0)
$table.Columns[-1].Padding = [Spectre.Console.Padding]::new(1, 0, 1, 0)
if ($lookup.width -gt 0) {
# width 0 is autosize, select the last entry in the column list
$table.Columns[-1].Width = $lookup.Width
}
if ($lookup.Alignment -ne 'undefined') {
$table.Columns[-1].Alignment = [Justify]::$lookup.Alignment
$table.Columns[-1].Alignment = [Spectre.Console.Justify]::$lookup.Alignment
}
if (-Not $Wrap) {
# https://github.com/spectreconsole/spectre.console/issues/1185
Expand Down
87 changes: 43 additions & 44 deletions PwshSpectreConsole/private/Convert-ToSpectreColor.ps1
Original file line number Diff line number Diff line change
@@ -1,44 +1,43 @@
using namespace Spectre.Console

<#
.SYNOPSIS
Converts a string representation of a color to a Color object.
.DESCRIPTION
This function takes a string representation of a color and converts it to a Color object. The input color can be in the form of a named color or a hexadecimal color code.
.PARAMETER Color
The color to convert. This parameter is mandatory and accepts input from the pipeline.
.EXAMPLE
'red' | Convert-ToSpectreColor
.EXAMPLE
'#FF0000' | Convert-ToSpectreColor
.EXAMPLE
[Color]::Salmon1 | Convert-ToSpectreColor
#>
function Convert-ToSpectreColor {
param (
[Parameter(ValueFromPipeline, Mandatory)]
[object] $Color
)
try {
# Just return the console color object
if ($Color -is [Color]) {
return $Color
}
# Already validated in validation attribute
if ($Color.StartsWith("#")) {
$hexString = $Color -replace '^#', ''
$hexBytes = [System.Convert]::FromHexString($hexString)
return [Color]::new($hexBytes[0], $hexBytes[1], $hexBytes[2])
}

# Validated in attribute as a real color already
return [Color]::$Color
} catch {
return $script:AccentColor
}
}

<#
.SYNOPSIS
Converts a string representation of a color to a Color object.
.DESCRIPTION
This function takes a string representation of a color and converts it to a Color object. The input color can be in the form of a named color or a hexadecimal color code.
.PARAMETER Color
The color to convert. This parameter is mandatory and accepts input from the pipeline.
.EXAMPLE
'red' | Convert-ToSpectreColor
.EXAMPLE
'#FF0000' | Convert-ToSpectreColor
.EXAMPLE
[Spectre.Console.Color]::Salmon1 | Convert-ToSpectreColor
#>
function Convert-ToSpectreColor {
param (
[Parameter(ValueFromPipeline, Mandatory)]
[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 '^#', ''
$hexBytes = [System.Convert]::FromHexString($hexString)
return [Spectre.Console.Color]::new($hexBytes[0], $hexBytes[1], $hexBytes[2])
}

# Validated in attribute as a real color already
return [Spectre.Console.Color]::$Color
} catch {
return $script:AccentColor
}
}
21 changes: 10 additions & 11 deletions PwshSpectreConsole/private/ConvertTo-SpectreDecoration.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using namespace Spectre.Console

function ConvertTo-SpectreDecoration {
param(
Expand All @@ -9,9 +8,9 @@ function ConvertTo-SpectreDecoration {
Write-Debug "Module: $($ExecutionContext.SessionState.Module.Name) Command: $($MyInvocation.MyCommand.Name) Param: $($PSBoundParameters.GetEnumerator())"
$lookup = [PwshSpectreConsole.VTCodes.Parser]::Parse($String)
$ht = @{
decoration = [Decoration]::None
fg = [Color]::Default
bg = [Color]::Default
decoration = [Spectre.Console.Decoration]::None
fg = [Spectre.Console.Color]::Default
bg = [Spectre.Console.Color]::Default
}
foreach ($item in $lookup) {
# Write-Debug "Type: $($item.type) Value: $($item.value) Position: $($item.position) Color: $($item.color)"
Expand All @@ -21,22 +20,22 @@ function ConvertTo-SpectreDecoration {
$conversion = switch ($item.type) {
'4bit' {
if ($item.value -gt 0 -and $item.value -le 15) {
[Color]::FromConsoleColor($item.value)
[Spectre.Console.Color]::FromConsoleColor($item.value)
} else {
# spectre doesn't appear to have a way to convert from 4bit.
# e.g all $PSStyle colors 30-37, 40-47 and 90-97, 100-107
# this will return the closest color in 8bit.
[Color]::FromConsoleColor((ConvertFrom-ConsoleColor $item.value))
[Spectre.Console.Color]::FromConsoleColor((ConvertFrom-ConsoleColor $item.value))
}
}
'8bit' {
[Color]::FromInt32($item.value)
[Spectre.Console.Color]::FromInt32($item.value)
}
'24bit' {
[Color]::new($item.value.Red, $item.value.Green, $item.value.Blue)
[Spectre.Console.Color]::new($item.value.Red, $item.value.Green, $item.value.Blue)
}
'decoration' {
[Decoration]::Parse([Decoration], $item.Value, $true)
[Spectre.Console.Decoration]::Parse([Spectre.Console.Decoration], $item.Value, $true)
}
}
if ($item.type -eq 'decoration') {
Expand All @@ -51,7 +50,7 @@ function ConvertTo-SpectreDecoration {
$String = [System.Management.Automation.Host.PSHostUserInterface]::GetOutputString($String, $false)
Write-Debug "Clean: '$String' deco: '$($ht.decoration)' fg: '$($ht.fg)' bg: '$($ht.bg)'"
if ($AllowMarkup) {
return [Markup]::new($String, [Style]::new($ht.fg, $ht.bg, $ht.decoration))
return [Spectre.Console.Markup]::new($String, [Spectre.Console.Style]::new($ht.fg, $ht.bg, $ht.decoration))
}
return [Text]::new($String, [Style]::new($ht.fg, $ht.bg, $ht.decoration))
return [Spectre.Console.Text]::new($String, [Spectre.Console.Style]::new($ht.fg, $ht.bg, $ht.decoration))
}
3 changes: 1 addition & 2 deletions PwshSpectreConsole/private/Get-SpectreProfile.ps1
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using namespace Spectre.Console

function Get-SpectreProfile {
[CmdletBinding()]
param ()
$object = [AnsiConsole]::Profile
$object = [Spectre.Console.AnsiConsole]::Profile
return [PSCustomObject]@{
Enrichers = $object.Enrichers -join ', '
ColorSystem = $object.Capabilities.ColorSystem
Expand Down
Loading

0 comments on commit dca82c1

Please sign in to comment.