Skip to content

Commit

Permalink
Add namespace imports to shorten class names and switch to argument p…
Browse files Browse the repository at this point in the history
…arser not validator
  • Loading branch information
ShaunLawrie committed Feb 17, 2024
1 parent c1816c2 commit da5e062
Show file tree
Hide file tree
Showing 35 changed files with 216 additions and 152 deletions.
7 changes: 5 additions & 2 deletions PwshSpectreConsole/PwshSpectreConsole.psm1
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using module ".\private\completions\Completers.psm1"
using namespace Spectre.Console

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

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

<#
.SYNOPSIS
Recursively adds child nodes to a parent node in a Spectre.Console tree.
Expand All @@ -17,13 +19,13 @@ See Format-SpectreTree for usage.
function Add-SpectreTreeNode {
param (
[Parameter(Mandatory)]
[Spectre.Console.IHasTreeNodes] $Node,
[IHasTreeNodes] $Node,
[Parameter(Mandatory)]
[array] $Children
)

foreach($child in $Children) {
$newNode = [Spectre.Console.HasTreeNodeExtensions]::AddNode($Node, $child.Label)
$newNode = [HasTreeNodeExtensions]::AddNode($Node, $child.Label)
if($child.Children.Count -gt 0) {
Add-SpectreTreeNode -Node $newNode -Children $child.Children
}
Expand Down
2 changes: 1 addition & 1 deletion PwshSpectreConsole/private/Add-TableColumns.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function Add-TableColumns {
$lookup = $FormatData[$key]
Write-Debug "Adding column from formatdata: $($lookup.GetEnumerator())"
$table.AddColumn($lookup.Label) | Out-Null
$table.Columns[-1].Padding = [Spectre.Console.Padding]::new(1, 0, 1, 0)
$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
$table.Columns[-1].Width = $lookup.Width
Expand Down
17 changes: 9 additions & 8 deletions PwshSpectreConsole/private/Convert-ToSpectreColor.ps1
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
using module ".\completions\Completers.psm1"
using namespace Spectre.Console

<#
.SYNOPSIS
Converts a string representation of a color to a Spectre.Console.Color object.
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 Spectre.Console.Color object. The input color can be in the form of a named color or a hexadecimal color code.
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
# This example converts the string 'red' to a Spectre.Console.Color object.
# This example converts the string 'red' to a Color object.
'red' | Convert-ToSpectreColor
.EXAMPLE
# This example converts the hexadecimal color code '#FF0000' to a Spectre.Console.Color object.
# This example converts the hexadecimal color code '#FF0000' to a Color object.
'#FF0000' | Convert-ToSpectreColor
.EXAMPLE
# This example passes through and returns the original color, it's needed for backwards compatibility with the older way of doing things in this library.
[Spectre.Console.Color]::Salmon1 | Convert-ToSpectreColor
[Color]::Salmon1 | Convert-ToSpectreColor
#>
function Convert-ToSpectreColor {
param (
Expand All @@ -30,18 +31,18 @@ function Convert-ToSpectreColor {
)
try {
# Just return the console color object
if($Color -is [Spectre.Console.Color]) {
if($Color -is [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])
return [Color]::new($hexBytes[0], $hexBytes[1], $hexBytes[2])
}

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

function ConvertTo-SpectreDecoration {
param(
[Parameter(Mandatory)]
Expand All @@ -10,9 +12,9 @@ function ConvertTo-SpectreDecoration {
}
$lookup = [PwshSpectreConsole.VTCodes.Parser]::Parse($String)
$ht = @{
decoration = [Spectre.Console.Decoration]::None
fg = [Spectre.Console.Color]::Default
bg = [Spectre.Console.Color]::Default
decoration = [Decoration]::None
fg = [Color]::Default
bg = [Color]::Default
}
foreach ($item in $lookup) {
# Write-Debug "Type: $($item.type) Value: $($item.value) Position: $($item.position) Color: $($item.color)"
Expand All @@ -22,23 +24,23 @@ function ConvertTo-SpectreDecoration {
$conversion = switch ($item.type) {
'4bit' {
if ($item.value -gt 0 -and $item.value -le 15) {
[Spectre.Console.Color]::FromConsoleColor($item.value)
[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.
[Spectre.Console.Color]::FromConsoleColor((ConvertFrom-ConsoleColor $item.value))
[Color]::FromConsoleColor((ConvertFrom-ConsoleColor $item.value))
}
}
'8bit' {
[Spectre.Console.Color]::FromInt32($item.value)
[Color]::FromInt32($item.value)
}
'24bit' {
[Spectre.Console.Color]::new($item.value.Red, $item.value.Green, $item.value.Blue)
[Color]::new($item.value.Red, $item.value.Green, $item.value.Blue)
}
'decoration' {
[Spectre.Console.Decoration]::Parse([Spectre.Console.Decoration], $item.Value, $true)
[Decoration]::Parse([Decoration], $item.Value, $true)
}
}
if ($item.type -eq 'decoration') {
Expand All @@ -54,7 +56,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 [Spectre.Console.Markup]::new($String, [Spectre.Console.Style]::new($ht.fg, $ht.bg, $ht.decoration))
return [Markup]::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))
return [Text]::new($String, [Style]::new($ht.fg, $ht.bg, $ht.decoration))
}
4 changes: 3 additions & 1 deletion PwshSpectreConsole/private/Get-SpectreProfile.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using namespace Spectre.Console

function Get-SpectreProfile {
[CmdletBinding()]
param ()
$object = [Spectre.Console.AnsiConsole]::Profile
$object = [AnsiConsole]::Profile
return [PSCustomObject]@{
Enrichers = $object.Enrichers -join ', '
ColorSystem = $object.Capabilities.ColorSystem
Expand Down
4 changes: 3 additions & 1 deletion PwshSpectreConsole/private/Invoke-SpectrePromptAsync.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using namespace Spectre.Console

function Invoke-SpectrePromptAsync {
<#
.SYNOPSIS
Expand All @@ -18,7 +20,7 @@ function Invoke-SpectrePromptAsync {
)
$cts = [System.Threading.CancellationTokenSource]::new()
try {
$task = $Prompt.ShowAsync([Spectre.Console.AnsiConsole]::Console, $cts.Token)
$task = $Prompt.ShowAsync([AnsiConsole]::Console, $cts.Token)
while (-not $task.AsyncWaitHandle.WaitOne(200)) {
# Waiting for the async task this way allows ctrl-c interrupts to continue to work within the single-threaded PowerShell world
}
Expand Down
14 changes: 8 additions & 6 deletions PwshSpectreConsole/private/New-TableCell.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using namespace Spectre.Console

function New-TableCell {
[cmdletbinding()]
param(
Expand All @@ -7,23 +9,23 @@ function New-TableCell {
Write-Debug "Module: $($ExecutionContext.SessionState.Module.Name) Command: $($MyInvocation.MyCommand.Name) Param: $($PSBoundParameters.GetEnumerator())"
if ([String]::IsNullOrEmpty($String)) {
if ($AllowMarkup) {
return [Spectre.Console.Markup]::new(' ')
return [Markup]::new(' ')
}
return [Spectre.Console.Text]::new(' ')
return [Text]::new(' ')
}
if (-Not [String]::IsNullOrEmpty($String.ToString())) {
if ($AllowMarkup) {
Write-Debug "New-TableCell ToString(), Markup, $($String.ToString())"
return [Spectre.Console.Markup]::new($String.ToString())
return [Markup]::new($String.ToString())
}
Write-Debug "New-TableCell ToString(), Text, $($String.ToString())"
return [Spectre.Console.Text]::new($String.ToString())
return [Text]::new($String.ToString())
}
# just coerce to string.
if ($AllowMarkup) {
Write-Debug "New-TableCell [String], markup, $([String]$String)"
return [Spectre.Console.Markup]::new([String]$String)
return [Markup]::new([String]$String)
}
Write-Debug "New-TableCell [String], Text, $([String]$String)"
return [Spectre.Console.Text]::new([String]$String)
return [Text]::new([String]$String)
}
6 changes: 4 additions & 2 deletions PwshSpectreConsole/private/Read-FigletFont.ps1
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using namespace Spectre.Console

# Read in a figlet font or just return the default built-in one
function Read-FigletFont {
param (
[string] $FigletFontPath
)
$figletFont = [Spectre.Console.FigletFont]::Default
$figletFont = [FigletFont]::Default
if($FigletFontPath) {
if(!(Test-Path $FigletFontPath)) {
throw "The specified Figlet font file '$FigletFontPath' does not exist"
}
$figletFont = [Spectre.Console.FigletFont]::Load($FigletFontPath)
$figletFont = [FigletFont]::Load($FigletFontPath)
}
return $figletFont
}
3 changes: 2 additions & 1 deletion PwshSpectreConsole/private/Start-AnsiConsoleProgress.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using namespace Spectre.Console

<#
.SYNOPSIS
Expand All @@ -22,7 +23,7 @@ function Start-AnsiConsoleProgress {
)
$resultVariableName = "AnsiConsoleProgressResult-$([guid]::NewGuid())"
New-Variable -Name $resultVariableName -Scope "Script"
[Spectre.Console.AnsiConsole]::Progress().Start({
[AnsiConsole]::Progress().Start({
param (
$ctx
)
Expand Down
8 changes: 5 additions & 3 deletions PwshSpectreConsole/private/Start-AnsiConsoleStatus.ps1
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
using namespace Spectre.Console

function Start-AnsiConsoleStatus {
param (
[Parameter(Mandatory)]
[string] $Title,
[Parameter(Mandatory)]
[Spectre.Console.Spinner] $Spinner,
[Spinner] $Spinner,
[Parameter(Mandatory)]
[Spectre.Console.Style] $SpinnerStyle,
[Style] $SpinnerStyle,
[Parameter(Mandatory)]
[scriptblock] $ScriptBlock
)
$resultVariableName = "AnsiConsoleStatusResult-$([guid]::NewGuid())"
New-Variable -Name $resultVariableName -Scope "Script"
[Spectre.Console.AnsiConsole]::Status().Start($Title, {
[AnsiConsole]::Status().Start($Title, {
param (
$ctx
)
Expand Down
10 changes: 6 additions & 4 deletions PwshSpectreConsole/private/Write-AnsiConsole.ps1
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using namespace Spectre.Console

<#
.SYNOPSIS
Writes an object to the console using [Spectre.Console.AnsiConsole]::Write()
Writes an object to the console using [AnsiConsole]::Write()
.DESCRIPTION
This function is required for mocking ansiconsole in unit tests that write objects to the console.
.PARAMETER RenderableObject
The renderable object to write to the console e.g. [Spectre.Console.BarChart]
The renderable object to write to the console e.g. [BarChart]
.EXAMPLE
Write-SpectreConsoleOutput -Object "Hello, World!" -ForegroundColor Green -BackgroundColor Black
Expand All @@ -16,7 +18,7 @@ This example writes the string "Hello, World!" to the console with green foregro
function Write-AnsiConsole {
param(
[Parameter(Mandatory)]
[Spectre.Console.Rendering.Renderable] $RenderableObject
[Rendering.Renderable] $RenderableObject
)
[Spectre.Console.AnsiConsole]::Write($RenderableObject)
[AnsiConsole]::Write($RenderableObject)
}
6 changes: 4 additions & 2 deletions PwshSpectreConsole/private/Write-SpectreHostInternal.ps1
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
using namespace Spectre.Console

# Functions required for unit testing write-spectrehost
function Write-SpectreHostInternalMarkup {
param (
[Parameter(Mandatory)]
[string] $Message
)
[Spectre.Console.AnsiConsole]::Markup($Message)
[AnsiConsole]::Markup($Message)
}

function Write-SpectreHostInternalMarkupLine {
param (
[Parameter(Mandatory)]
[string] $Message
)
[Spectre.Console.AnsiConsole]::MarkupLine($Message)
[AnsiConsole]::MarkupLine($Message)
}
17 changes: 13 additions & 4 deletions PwshSpectreConsole/public/config/Set-SpectreColors.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using module "..\..\private\completions\Completers.psm1"
using namespace Spectre.Console

function Set-SpectreColors {
<#
Expand Down Expand Up @@ -30,13 +31,21 @@ function Set-SpectreColors {
#>
[Reflection.AssemblyMetadata("title", "Set-SpectreColors")]
param (
[ValidateSpectreColor()]
[ColorTransformationAttribute()]
[ArgumentCompletionsSpectreColors()]
[string] $AccentColor = "Blue",
[ValidateSpectreColor()]
[Color] $AccentColor = "Blue",
[ColorTransformationAttribute()]
[ArgumentCompletionsSpectreColors()]
[string] $DefaultValueColor = "Grey"
[Color] $DefaultValueColor = "Grey",
[ColorTransformationAttribute()]
[ArgumentCompletionsSpectreColors()]
[Color] $DefaultTableHeaderColor = "Grey82",
[ColorTransformationAttribute()]
[ArgumentCompletionsSpectreColors()]
[Color] $DefaultTableTextColor = "Grey39"
)
$script:AccentColor = $AccentColor | Convert-ToSpectreColor
$script:DefaultValueColor = $DefaultValueColor | Convert-ToSpectreColor
$script:DefaultTableHeaderColor = $DefaultTableHeaderColor | Convert-ToSpectreColor
$script:DefaultTableTextColor = $DefaultTableTextColor | Convert-ToSpectreColor
}
6 changes: 4 additions & 2 deletions PwshSpectreConsole/public/demo/Get-SpectreDemoColors.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using namespace Spectre.Console

<#
.SYNOPSIS
Retrieves a list of Spectre Console colors and displays them with their corresponding markup.
Expand All @@ -22,7 +24,7 @@ function Get-SpectreDemoColors {
Write-SpectreRule "Colors"
Write-Host ""

$colors = [Spectre.Console.Color] | Get-Member -Static -Type Properties | Select-Object -ExpandProperty Name
$colors = [Color] | Get-Member -Static -Type Properties | Select-Object -ExpandProperty Name
$colors = $colors | ForEach-Object {
$prefix = ($_ -replace '[_0-9]+', '')
$numeric = ($_ -replace '^[^0-9]+', '')
Expand All @@ -47,7 +49,7 @@ function Get-SpectreDemoColors {
$maxLength = $colors | Measure-Object -Maximum -Property Length | Select-Object -ExpandProperty Maximum

foreach($color in $colors) {
$total = [Spectre.Console.Color]::$color | Select-Object @{ Name = "Total"; Expression = {$_.R + $_.G + $_.B} } | Select-Object -ExpandProperty Total
$total = [Color]::$color | Select-Object @{ Name = "Total"; Expression = {$_.R + $_.G + $_.B} } | Select-Object -ExpandProperty Total
$textColor = "white"
if($total -gt 280) {
$textColor = "black"
Expand Down
4 changes: 3 additions & 1 deletion PwshSpectreConsole/public/demo/Get-SpectreDemoEmoji.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using namespace Spectre.Console

<#
.SYNOPSIS
Retrieves a collection of emojis available in Spectre Console.
Expand All @@ -23,7 +25,7 @@ function Get-SpectreDemoEmoji {
Write-SpectreRule "`nGeneral"
Write-Host ""

$emojiCollection = [Spectre.Console.Emoji+Known] | Get-Member -Static -Type Properties | Select-Object -ExpandProperty Name
$emojiCollection = [Emoji+Known] | Get-Member -Static -Type Properties | Select-Object -ExpandProperty Name
$faces = @()
foreach($emoji in $emojiCollection) {
$id = ($emoji -creplace '([A-Z])', '_$1' -replace '^_', '').ToLower()
Expand Down
Loading

0 comments on commit da5e062

Please sign in to comment.