Skip to content

Instantly share code, notes, and snippets.

@doggy8088
Last active December 16, 2023 15:07
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save doggy8088/2bf2a46f7e65ae4197b6092df3835f21 to your computer and use it in GitHub Desktop.
Save doggy8088/2bf2a46f7e65ae4197b6092df3835f21 to your computer and use it in GitHub Desktop.
# winget parameter completion
Register-ArgumentCompleter -Native -CommandName winget -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)
[Console]::InputEncoding = [Console]::OutputEncoding = $OutputEncoding = [System.Text.Utf8Encoding]::new()
$Local:word = $wordToComplete.Replace('"', '""')
$Local:ast = $commandAst.ToString().Replace('"', '""')
winget complete --word="$Local:word" --commandline "$Local:ast" --position $cursorPosition | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
# PowerShell parameter completion shim for the dotnet CLI
Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock {
param($commandName, $wordToComplete, $cursorPosition)
dotnet complete --position $cursorPosition "$wordToComplete" | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
# PowerShell parameter completion shim for the npm CLI
Register-ArgumentCompleter -Native -CommandName npm -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)
$Local:ast = $commandAst.ToString().Replace(' ', '')
if ($Local:ast -eq 'npm') {
$command = 'run install start'
$array = $command.Split(' ')
$array |
Where-Object { $_ -like "$wordToComplete*" } |
ForEach-Object {
New-Object -Type System.Management.Automation.CompletionResult -ArgumentList $_
}
}
if ($Local:ast -eq 'npmrun') {
$scripts = (Get-Content .\package.json | ConvertFrom-Json).scripts
$scripts |
Get-Member -MemberType NoteProperty |
Where-Object { $_.Name -like "$wordToComplete*" } |
ForEach-Object {
New-Object -Type System.Management.Automation.CompletionResult -ArgumentList $_.Name
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment