Open mklabs opened 8 years ago
http://powertab.codeplex.com/ http://powertab.codeplex.com/wikipage?title=Tab%20Expansion%20Handlers&referringTitle=Home
Register-TabExpansion "Get-Service" -Type Command {
param($Context, [ref]$TabExpansionHasOutput, [ref]$QuoteSpaces) # 1:
$Argument = $Context.Argument
switch -exact ($Context.Parameter) {
'DisplayName' {
$TabExpansionHasOutput.Value = $true # 2:
Get-Service -DisplayName "*$Argument*" | Select-Object -ExpandProperty DisplayName # 3:
}
'Name' {
$TabExpansionHasOutput.Value = $true # 2:
Get-Service -Name "$Argument*" | Select-Object -ExpandProperty Name # 3:
}
}
}
param handler
Register-TabExpansion "PSDrive" -Type Parameter {
param($Argument, [ref]$TabExpansionHasOutput, [ref]$QuoteSpaces) # 1:
if ($Argument -notlike '^\$') {
$TabExpansionHasOutput.Value = $true # 2:
Get-PSDrive "$Argument*" | Select-Object -ExpandProperty Name # 3:
}
}
register
Register-TabExpansion [-Name] <String> [-Handler] <ScriptBlock> [-Type <String>] [-Force]
PowerShell Core natively includes Register-ArgumentCompleter
to register completions for external binaries.
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/register-argumentcompleter?view=powershell-6
It can return an array of CompletionResult
instances, which describe the completion text, label text, description, etc.
PowerShell can natively emit and parse JSON, so it should be easy to pass all the necessary data to and from tabtab without the need to write much PowerShell syntax. Tabtab can do all the work in JS and write JSON to stdout.
Register-ArgumentCompleter -Native -CommandName tabtab-test -ScriptBlock {
$argsEncoded = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes(($args | convertto-json)))
$results = tabtab-test completion -- $argsEncoded | convertfrom-json
foreach($result in $results) {
[Management.Automation.CompletionResult]::new($result[0], $result[1], $result[2], $result[3])
}
}
Do note that PowerShell is now also supported on Unix‑based operating systems, so this is no longer just for Windows.
Here is how Cobra, a CLI framework for Go, supports it: https://github.com/spf13/cobra/blob/main/powershell_completions.go
Base on that, user @jcwillox has written a script to support PowerShell completion for pnpm: https://gist.github.com/jcwillox/027b6c105d190abfa0333c13836f5bec
http://www.leeholmes.com/blog/2012/09/13/bash-like-tab-completion-in-powershell/
powershell became a very capable terminal emulator, see how we could support windows completion.