Last active
November 27, 2023 22:01
-
-
Save ninmonkey/36922d365f1d979b6f9e560d3e123c6c to your computer and use it in GitHub Desktop.
Minimum Native Completer Template for Pwsh 7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using namespace System.Collections | |
using namespace System.Collections.Generic | |
using namespace System.Management | |
using namespace System.Management.Automation | |
using namespace System.Management.Automation.Language | |
function New.CompletionResult { | |
[Alias('New.CR')] | |
param( | |
# original base text | |
[Alias('Item', 'Text')] | |
[Parameter(Mandatory)] | |
[ValidateNotNullOrEmpty()] | |
[ValidateNotNullOrWhiteSpace()] | |
[string]$ListItemText, | |
# actual value used in replacement, if not the same as ListItem | |
[AllowEmptyString()] | |
[AllowNull()] | |
[Alias('Replacement', 'Replace')] | |
[Parameter()] | |
[string]$CompletionText, | |
# Is there a better default? | |
[Parameter()] | |
[CompletionResultType] | |
$ResultType = ([CompletionResultType]::ParameterValue), | |
# multi-line text displayed when using listcompletion | |
[Parameter()] | |
[Alias('Description', 'Help', 'RenderText')] | |
[string[]]$Tooltip | |
) | |
[System.ArgumentException]::ThrowIfNullOrWhiteSpace( $ListItemText , 'ListItemText' ) | |
$Tooltip = $Tooltip -join "`n" | |
if( [string]::IsNullOrEmpty( $Tooltip )) { | |
$Tooltip = '[⋯]' | |
} | |
if( [string]::IsNullOrEmpty( $CompletionText )) { | |
$CompletionText = $ListItemText | |
} | |
[CompletionResult]::new( | |
<# completionText: #> $completionText, | |
<# listItemText : #> $listItemText, | |
<# resultType : #> $resultType, | |
<# toolTip : #> $toolTip) | |
} | |
function __SortIt.WithoutPrefix { | |
# future: can completer take arguments to the sorting interface? else make it work with one of them | |
param( | |
[ArgumentCompletions( | |
'ListItemText', | |
'CompletionText')] | |
[string]$PropertyName = 'ListItemText' | |
) | |
$Input | Sort-Object { $_.$PropertyName -replace '^-+', '' } | |
} | |
function __module.Wsl.buildCompletions {@( | |
New.CompletionResult -Text '--distribution' -Replacement '--distribution ''distro''' -ResultType ParameterValue -Tooltip ' | |
--distribution, -d <Distro>', 'Run the specified distribution.' | |
New.CompletionResult -Text '--system' -Replacement '' -ResultType ParameterValue -Tooltip '--system', 'Launches a shell for the system distirbution' | |
) | |
| Sort-Object 'ListItemText' -Unique | |
| __SortIt.WithoutPrefix 'ListItemText' | |
} | |
$scriptBlock = { | |
# param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters) | |
param($wordToComplete, $commandAst, $cursorPosition) | |
[List[CompletionResult]]$items = @( __module.Wsl.buildCompletions ) | |
$FilterProp = | |
'ListItemText' | |
# 'CompletionText' | |
[List[CompletionResult]]$selected = | |
$items | ?{ | |
$matchesAny = ( | |
$_.ListItemText -match $wordToComplete -or | |
$_.CompletionText -match $wordToComplete -or | |
$_.ListItemText -match [regex]::escape( $wordToComplete ) -or | |
$_.CompletionText -match [regex]::escape( $wordToComplete ) ) | |
return $MatchesAny | |
} | |
return $selected | |
} | |
Register-ArgumentCompleter -CommandName 'wsl' -Native -ScriptBlock $ScriptBlock -Verbose |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment