Skip to content

Commit

Permalink
Enable color completion everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaykul committed Sep 10, 2024
1 parent 9c97965 commit 4484546
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Source/Pansies.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ FunctionsToExport = @()
CmdletsToExport = @('New-Text', 'New-Hyperlink', 'Write-Host', 'Get-Gradient', 'Get-Complement', 'Get-ColorWheel')

# Variables to export from this module
# VariablesToExport = '*'
VariablesToExport = 'RgbColorCompleter'

# Aliases 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 aliases to export.
AliasesToExport = 'Text', 'Url'
Expand Down Expand Up @@ -137,5 +137,6 @@ PrivateData = @{
# Minimum version of the Windows PowerShell engine required by this module
PowerShellVersion = '5.1'
CompatiblePSEditions = @('Core','Desktop')

}

52 changes: 48 additions & 4 deletions Source/Private/_init.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
using namespace PoshCode.Pansies
using namespace PoshCode.Pansies.Palettes
using namespace System.Collections.Generic
using namespace System.Collections
using namespace ColorMine.ColorSpaces
using namespace System.Management.Automation
using namespace System.Management.Automation.Language

# On first import, if HostPreference doesn't exist, set it and strongly type it
if (!(Test-Path Variable:HostPreference) -or $null -eq $HostPreference) {
Expand All @@ -19,11 +24,32 @@ if(Get-Command Add-MetadataConverter -ErrorAction Ignore) {
}
}

$xlr8r = [psobject].assembly.gettype("System.Management.Automation.TypeAccelerators")
@{
$Accelerators = @{
"RGBColor" = [PoshCode.Pansies.RgbColor]
"Entities" = [PoshCode.Pansies.Entities]
}.GetEnumerator().ForEach({
}

# IArgumentCompleterFactory only available on PS7+
if ("System.Management.Automation.IArgumentCompleterFactory" -as [type]) {
Add-Type @"
using System.Management.Automation;
using PoshCode.Pansies.Palettes;
namespace PoshCode.Pansies {
public class ColorCompleterAttribute : ArgumentCompleterAttribute, IArgumentCompleterFactory {
public ColorCompleterAttribute(){}
public IArgumentCompleter Create() {
return new X11Palette();
}
}
}
"@ -ReferencedAssemblies ([psobject].Assembly), ([PoshCode.Pansies.RgbColor].Assembly), "netstandard" -CompilerOptions "-NoWarn:1701"

$Accelerators["ColorCompleterAttribute"] = [PoshCode.Pansies.ColorCompleterAttribute]

}

$xlr8r = [psobject].assembly.gettype("System.Management.Automation.TypeAccelerators")
$Accelerators.GetEnumerator().ForEach({
$Name = $_.Key
$Type = $_.Value
if ($xlr8r::AddReplace) {
Expand All @@ -41,4 +67,22 @@ $xlr8r = [psobject].assembly.gettype("System.Management.Automation.TypeAccelerat
}
throw
}
})
})

$script:X11Palette = [X11Palette]::new()
$RgbColorCompleter = {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
$script:X11Palette.CompleteArgument($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
}

$global:PansiesColorCompleterRegistration = Register-EngineEvent -SourceIdentifier PowerShell.OnIdle {
foreach ($command in Get-Command -ParameterType RgbColor) {
foreach ($parameter in $command.Parameters.Values.Where{ $_.ParameterType -eq [RgbColor] }) {
Register-ArgumentCompleter -CommandName $command.Name -ParameterName $parameter.Name -ScriptBlock $RgbColorCompleter
}
}
Stop-Job $global:PansiesColorCompleterRegistration # This removes the event
Remove-Variable PansiesColorCompleterRegistration -Scope global
}

Export-ModuleMember -Variable RgbColorCompleter -Function *-* -Cmdlet * -Alias *

0 comments on commit 4484546

Please sign in to comment.