Skip to content

Commit

Permalink
Update to better way of doing completions from trackd
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaunLawrie committed Nov 26, 2023
1 parent b392af2 commit e7a98cf
Show file tree
Hide file tree
Showing 22 changed files with 137 additions and 107 deletions.
7 changes: 7 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "PowerShell Launch Current File",
"type": "PowerShell",
"request": "launch",
"script": "${file}",
"cwd": "${cwd}"
},
{
"command": "./node_modules/.bin/astro dev",
"cwd": "${workspaceFolder}/PwshSpectreConsole.Docs",
Expand Down
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
{
"powershell.codeFormatting.preset": "OTBS"
}
4 changes: 1 addition & 3 deletions PwshSpectreConsole/PwshSpectreConsole.psm1
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using module ".\private\attributes\ColorAttributes.psm1"
using module ".\private\attributes\BorderAttributes.psm1"
using module ".\private\attributes\SpinnerAttributes.psm1"
using module ".\private\completions\Completers.psm1"

$script:AccentColor = [Spectre.Console.Color]::Blue
$script:DefaultValueColor = [Spectre.Console.Color]::Grey
Expand Down
2 changes: 1 addition & 1 deletion PwshSpectreConsole/private/Convert-ToSpectreColor.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using module ".\attributes\ColorAttributes.psm1"
using module ".\completions\Completers.psm1"

<#
.SYNOPSIS
Expand Down
20 changes: 0 additions & 20 deletions PwshSpectreConsole/private/attributes/BorderAttributes.psm1

This file was deleted.

33 changes: 0 additions & 33 deletions PwshSpectreConsole/private/attributes/ColorAttributes.psm1

This file was deleted.

20 changes: 0 additions & 20 deletions PwshSpectreConsole/private/attributes/SpinnerAttributes.psm1

This file was deleted.

60 changes: 60 additions & 0 deletions PwshSpectreConsole/private/completions/Completers.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using namespace Spectre.Console
using namespace System.Management.Automation

class ValidateSpectreColor : ValidateArgumentsAttribute {
ValidateSpectreColor() : base() { }
[void]Validate([object] $Color, [EngineIntrinsics]$EngineIntrinsics) {
# Handle hex colors
if ($Color -match '^#[A-Fa-f0-9]{6}$') {
return
}
$spectreColors = [Color] | Get-Member -Static -Type Properties | Select-Object -ExpandProperty Name
$result = $spectreColors -contains $Color
if ($result -eq $false) {
throw "'$Color' is not in the list of valid Spectre colors ['$($spectreColors -join ''', ''')']"
}
}
}

class ArgumentCompletionsSpectreColors : ArgumentCompleterAttribute {
ArgumentCompletionsSpectreColors() : base({
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
$options = [Color] | Get-Member -Static -Type Properties | Select-Object -ExpandProperty Name
return $options | Where-Object { $_ -like "$wordToComplete*" }
}) { }
}

class SpectreConsoleTableBorder : IValidateSetValuesGenerator {
[String[]] GetValidValues() {
$lookup = [TableBorder] | Get-Member -Static -MemberType Properties | Select-Object -ExpandProperty Name
return $lookup
}
}

class SpectreConsoleBoxBorder : IValidateSetValuesGenerator {
[String[]] GetValidValues() {
$lookup = [BoxBorder] | Get-Member -Static -MemberType Properties | Select-Object -ExpandProperty Name
return $lookup
}
}

class SpectreConsoleJustify : IValidateSetValuesGenerator {
[String[]] GetValidValues() {
$lookup = [Justify].GetEnumNames()
return $lookup
}
}

class SpectreConsoleSpinner : IValidateSetValuesGenerator {
[String[]] GetValidValues() {
$lookup = [Spinner+Known] | Get-Member -Static -MemberType Properties | Select-Object -ExpandProperty Name
return $lookup
}
}

class SpectreConsoleTreeGuide : IValidateSetValuesGenerator {
[String[]] GetValidValues() {
$lookup = [TreeGuide] | Get-Member -Static -MemberType Properties | Select-Object -ExpandProperty Name
return $lookup
}
}
2 changes: 1 addition & 1 deletion PwshSpectreConsole/public/config/Set-SpectreColors.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using module "..\..\private\attributes\ColorAttributes.psm1"
using module "..\..\private\completions\Completers.psm1"

function Set-SpectreColors {
<#
Expand Down
44 changes: 44 additions & 0 deletions PwshSpectreConsole/public/demo/Get-SpectreColorDemo.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
function Get-SpectreColorDemo {
[Reflection.AssemblyMetadata("title", "Start-SpectreDemo")]
param ()

$colors = [Spectre.Console.Color] | Get-Member -Static -Type Properties | Select-Object -ExpandProperty Name
$colors = $colors | ForEach-Object {
$prefix = ($_ -replace '[_0-9]+', '')
$numeric = ($_ -replace '^[^0-9]+', '')
$value = 0
if([string]::IsNullOrEmpty($numeric)) {
$value = 0.0
} else {
$numericParts = $numeric.Split('_')
if($numericParts.Count -lt 2) {
$value = [double]"$($numericParts[0]).9"
} else {
$value = [double]"$($numericParts[0]).$($numericParts[1])"
}
}
return [pscustomobject]@{
Name = $_
Prefix = $prefix
Numeric = $value
}
} | Sort-Object -Property @{Expression = "Prefix"}, @{Expression = "Numeric"} | Select-Object -ExpandProperty Name

$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
$textColor = "white"
if($total -gt 280) {
$textColor = "black"
}

Write-SpectreHost -NoNewline "[$textColor on $color] $($color.PadRight($maxLength)) [/] "
Write-SpectreHost ("[$color]$color[/]")
}

Write-Host "`nThe colors can be passed as the -Color parameter for most commands or used in Spectre Console markup like so:`n"
Write-Host " Input: I am [Red]colored text[/] using [Yellow1 on Turquoise4]Spectre markdown[/]!"
Write-SpectreHost " Output: I am [Red]colored text[/] using [Yellow1 on Turquoise4]Spectre markdown[/]!"
Write-SpectreHost "`nFor more markdown hints see [link]https://spectreconsole.net/markup[/]`n"
}
10 changes: 4 additions & 6 deletions PwshSpectreConsole/public/formatting/Format-SpectrePanel.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using module "..\..\private\attributes\ColorAttributes.psm1"
using module "..\..\private\attributes\BorderAttributes.psm1"
using module "..\..\private\completions\Completers.psm1"

function Format-SpectrePanel {
<#
Expand Down Expand Up @@ -33,16 +32,15 @@ function Format-SpectrePanel {
[Parameter(ValueFromPipeline, Mandatory)]
[string] $Data,
[string] $Title,
[ValidateSpectreBorder()]
[ArgumentCompletionsSpectreBorders()]
[ValidateSet([SpectreConsoleBoxBorder], ErrorMessage = "Value '{0}' is invalid. Try one of: {1}")]
[string] $Border = "Rounded",
[switch] $Expand,
[switch] $Expand,
[ValidateSpectreColor()]
[ArgumentCompletionsSpectreColors()]
[string] $Color = $script:AccentColor.ToMarkup()
)
$panel = [Spectre.Console.Panel]::new($Data)
if($Title) {
if ($Title) {
$panel.Header = [Spectre.Console.PanelHeader]::new($Title)
}
$panel.Expand = $Expand
Expand Down
6 changes: 2 additions & 4 deletions PwshSpectreConsole/public/formatting/Format-SpectreTable.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using module "..\..\private\attributes\ColorAttributes.psm1"
using module "..\..\private\attributes\BorderAttributes.psm1"
using module "..\..\private\completions\Completers.psm1"

function Format-SpectreTable {
<#
Expand Down Expand Up @@ -30,8 +29,7 @@ function Format-SpectreTable {
param (
[Parameter(ValueFromPipeline, Mandatory)]
[array] $Data,
[ValidateSpectreBorder()]
[ArgumentCompletionsSpectreBorders()]
[ValidateSet([SpectreConsoleTableBorder],ErrorMessage = "Value '{0}' is invalid. Try one of: {1}")]
[string] $Border = "Double",
[ValidateSpectreColor()]
[ArgumentCompletionsSpectreColors()]
Expand Down
9 changes: 4 additions & 5 deletions PwshSpectreConsole/public/formatting/Format-SpectreTree.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using module "..\..\private\attributes\ColorAttributes.psm1"
using module "..\..\private\attributes\BorderAttributes.psm1"
using module "..\..\private\completions\Completers.psm1"

function Format-SpectreTree {
<#
Expand Down Expand Up @@ -49,15 +48,15 @@ function Format-SpectreTree {
param (
[Parameter(ValueFromPipeline, Mandatory)]
[hashtable] $Data,
[ValidateSpectreBorder()]
[ArgumentCompletionsSpectreBorders()]
[string] $Border = "Rounded",
[ValidateSet([SpectreConsoleTreeGuide],ErrorMessage = "Value '{0}' is invalid. Try one of: {1}")]
[string] $Guide = "Line",
[ValidateSpectreColor()]
[ArgumentCompletionsSpectreColors()]
[string] $Color = $script:AccentColor.ToMarkup()
)

$tree = [Spectre.Console.Tree]::new($Data.Label)
$tree.Guide = [Spectre.Console.TreeGuide]::$Guide

Add-SpectreTreeNode -Node $tree -Children $Data.Children

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using module "..\..\private\models\SpectreChartItem.psm1"
using module "..\..\private\attributes\ColorAttributes.psm1"
using module "..\..\private\completions\Completers.psm1"

<#
.SYNOPSIS
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using module "..\..\private\attributes\ColorAttributes.psm1"
using module "..\..\private\attributes\SpinnerAttributes.psm1"
using module "..\..\private\completions\Completers.psm1"

function Invoke-SpectreCommandWithStatus {
<#
Expand All @@ -19,7 +18,7 @@ function Invoke-SpectreCommandWithStatus {
The title to display above the spinner.
.PARAMETER Color
The color of the spinner. Valid values are "black", "red", "green", "yellow", "blue", "magenta", "cyan", "white", "gray", "brightRed", "brightGreen", "brightYellow", "brightBlue", "brightMagenta", "brightCyan", "brightWhite".
The color of the spinner. Valid values can be found with Get-SpectreColorDemo.
.EXAMPLE
# Starts a Spectre status spinner with the "dots" spinner type, a yellow color, and the title "Waiting for process to complete". The spinner will continue to spin for 5 seconds.
Expand All @@ -29,8 +28,7 @@ function Invoke-SpectreCommandWithStatus {
param (
[Parameter(Mandatory)]
[scriptblock] $ScriptBlock,
[ValidateSpectreSpinner()]
[ArgumentCompletionsSpectreSpinners()]
[ValidateSet([SpectreConsoleSpinner],ErrorMessage = "Value '{0}' is invalid. Try one of: {1}")]
[string] $Spinner = "Dots",
[Parameter(Mandatory)]
[string] $Title,
Expand Down
2 changes: 1 addition & 1 deletion PwshSpectreConsole/public/prompts/Read-SpectreConfirm.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using module "..\..\private\attributes\ColorAttributes.psm1"
using module "..\..\private\completions\Completers.psm1"

function Read-SpectreConfirm {
<#
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using module "..\..\private\attributes\ColorAttributes.psm1"
using module "..\..\private\completions\Completers.psm1"

function Read-SpectreMultiSelection {
<#
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using module "..\..\private\attributes\ColorAttributes.psm1"
using module "..\..\private\completions\Completers.psm1"

function Read-SpectreMultiSelectionGrouped {
<#
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using module "..\..\private\attributes\ColorAttributes.psm1"
using module "..\..\private\completions\Completers.psm1"

function Read-SpectreSelection {
<#
Expand Down
4 changes: 2 additions & 2 deletions PwshSpectreConsole/public/writing/Write-SpectreFigletText.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using module "..\..\private\attributes\ColorAttributes.psm1"
using module "..\..\private\completions\Completers.psm1"

function Write-SpectreFigletText {
<#
Expand All @@ -24,7 +24,7 @@ function Write-SpectreFigletText {
[Reflection.AssemblyMetadata("title", "Write-SpectreFigletText")]
param (
[string] $Text = "Hello Spectre!",
[ValidateSet("Left", "Right", "Center")]
[ValidateSet([SpectreConsoleJustify], ErrorMessage = "Value '{0}' is invalid. Try one of: {1}")]
[string] $Alignment = "Left",
[ValidateSpectreColor()]
[ArgumentCompletionsSpectreColors()]
Expand Down
4 changes: 2 additions & 2 deletions PwshSpectreConsole/public/writing/Write-SpectreRule.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using module "..\..\private\attributes\ColorAttributes.psm1"
using module "..\..\private\completions\Completers.psm1"

function Write-SpectreRule {
<#
Expand All @@ -25,7 +25,7 @@ function Write-SpectreRule {
param (
[Parameter(Mandatory)]
[string] $Title,
[ValidateSet("Left", "Right", "Center")]
[ValidateSet([SpectreConsoleJustify], ErrorMessage = "Value '{0}' is invalid. Try one of: {1}")]
[string] $Alignment = "Left",
[ValidateSpectreColor()]
[ArgumentCompletionsSpectreColors()]
Expand Down

0 comments on commit e7a98cf

Please sign in to comment.